Idea操作Maven超级详细使用 高级篇:

347 阅读6分钟

「这是我参与2022首次更文挑战的第2天,活动详情查看:2022首次更文挑战

Idea操作Maven 高级篇:

  • 经过上面学习大致已经了解了: Maven基础篇 什么是Maven——为啥要用——基础的构建:
  • Maven 可以说是Java开发必不可少的工具了: ok!, 接下来深入了解 Maven的高级篇!

分模块构建工程[应用]

Maven 继承[依赖] 和聚合

何为聚合?

  • 项目开发通常是分组分模块开发
  • 每个模块开发完成一个单独的功能。
  • 如果: 要运行整个工程需要将每个模块聚合在 一起运行;
  • 比如: dao、service、web 三个工程最终会打一个独立的 war 运行.

聚合: 可以在 dao service web 模块之上在建立一个 父模块: 通过父模块来聚合工程, 运行只需要编译 打包父工程即可!

何为继承?

  • 和Java 很类似: 子类 继承extends 父类,便具有了父类的特性;
  • Maven继承: 是为了消除重复,如果将 dao、service、web 分开创建独立的工程则每个工程的; pom.xml 会有很多相同的依赖Jar包~ 可以将这些重复的配置,提取出来在父工程的 pom.xml 中定义统一管理声明公共 Jar
  • 父模块的打包方式必须为pom,否则无法构建项目。 子模块中通过配置来表明其继承与哪一个父模块:
  • 通常继承和聚合同时使用。

在这里插入图片描述

Maven聚合 Demo案例:

数据库:

在这里插入图片描述

父工程 bbs-parent

在这里插入图片描述

  • 创建一个普通的Maven工程删掉 src 父工程是不写代码的, 只是做了一个聚合管理的功能...
  • 添加所需要的Maven模块:
  • dao 数据层
  • entity 实体层
  • service 业务逻辑层
  • web 展示层,以上都是普通的Maven工程即可,而这里需要指定为 web Maven模板的工程...(可参考上一篇文~)

父工程 pom.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <project xmlns="http://maven.apache.org/POM/4.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
 ​
     <!-- 组 名 版本 -->
     <groupId>org.example</groupId>
     <artifactId>bbs-parent</artifactId>
     <version>1.0-SNAPSHOT</version>
 ​
     <!-- 父工程的打包方式! -->
     <!-- 我们见过打包方式为jar和war的,但是聚合模块的打包方式必须为pom,否则无法完成构建。 -->
     <packaging>pom</packaging>
 ​
     <!-- 父工程集成的子工程! -->
     <modules>
         <module>bbs-dao</module>
         <module>bbs-service</module>
         <module>bbs-entity</module>
         <module>bbs-web</module>
         <!--<module>..聚合的子工程..</module>-->
     </modules>
 ​
     <!-- 通用配置.. -->
     <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <maven.compiler.source>1.7</maven.compiler.source>
         <maven.compiler.target>1.7</maven.compiler.target>
         <spring.version>4.2.4.RELEASE</spring.version>
     </properties>
 ​
     <!-- SSM工程所需要的Jar,声明在父工程中,子工程也可以使用.... -->
     <dependencies>
         <!-- spring-命名空间:apl -->
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-context</artifactId>
             <version>${spring.version}</version>
         </dependency>
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-aop</artifactId>
             <version>${spring.version}</version>
         </dependency>
         <!-- spring-beans: Bean的Apl -->
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-beans</artifactId>
             <version>${spring.version}</version>
         </dependency>
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-core</artifactId>
             <version>${spring.version}</version>
         </dependency>
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-expression</artifactId>
             <version>${spring.version}</version>
         </dependency>
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-jdbc</artifactId>
             <version>${spring.version}</version>
         </dependency>
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-tx</artifactId>
             <version>${spring.version}</version>
         </dependency>
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-web</artifactId>
             <version>${spring.version}</version>
         </dependency>
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-webmvc</artifactId>
             <version>${spring.version}</version>
         </dependency>
         <dependency>
             <groupId>org.aspectj</groupId>
             <artifactId>aspectjrt</artifactId>
             <version>1.7.4</version>
         </dependency>
         <dependency>
             <groupId>org.aspectj</groupId>
             <artifactId>aspectjweaver</artifactId>
             <version>1.7.4</version>
         </dependency>
         <!-- 日志依赖 -->
         <dependency>
             <groupId>log4j</groupId>
             <artifactId>log4j</artifactId>
             <version>1.2.14</version>
         </dependency>
         
         <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-api</artifactId>
             <version>1.6.1</version>
         </dependency>
         <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-log4j12</artifactId>
             <version>1.6.1</version>
         </dependency>
         <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-nop</artifactId>
             <version>1.6.1</version>
         </dependency>
         <!-- JSON依赖 -->
         <dependency>
             <groupId>com.alibaba</groupId>
             <artifactId>fastjson</artifactId>
             <version>1.2.31</version>
         </dependency>
     </dependencies>
 </project>

注意:

  • <spring.version>4.2.4.RELEASE</spring.version> Spring版本 4.0以上是JDK 1.8使用的!!
  • 3.几 的版本是JDK1.7的!!哭死,这个错误找半天!!

bbs-entity 模块

实体层:定义项目所需要的pojo... 在这里插入图片描述

  • 这就特别简单了.. 定义包, 声明一个类: 对着数据库敲实体属性即可!
  • 需要注意的是:子工程的Pom.xml

Entity pom.xml 基本不需要任何配置,但有一些子工程 聚合 父工程的配置:

 <?xml version="1.0" encoding="UTF-8"?>
 <project xmlns="http://maven.apache.org/POM/4.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <!-- 子工程,指向父工程依赖配置! -->
     <parent>
         <artifactId>bbs-parent</artifactId>
         <groupId>org.example</groupId>
         <version>1.0-SNAPSHOT</version>
     </parent>
     
     <modelVersion>4.0.0</modelVersion>
     <!-- 子工程:项目名 -->
     <artifactId>bbs-entity</artifactId>
 </project>

bbs-dao

持久层:与数据库打交道负责 读写操作.. 在这里插入图片描述

  • 代码这里就不一一解读了,正常的SSM 查询全表操作!需要了解SSM可以点击这里!
  • SortMapper.Java 接口
  • SortMapper.xml Sql映射文件
  • mybatis-config.xml mybatis核心配置文件
  • applicationContext-mybatis.xml Spring核心配置文件

Dao pom.xml

子工程不仅可以,继承父工程的公共依赖 当然也可以定义一些自己特有的依赖

Dao模块 依赖 Entity模块 所有要引入其它子工程的依赖

 <?xml version="1.0" encoding="UTF-8"?>
 <project xmlns="http://maven.apache.org/POM/4.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <!-- 子工程,指向父工程依赖配置! -->
     <parent>
         <artifactId>bbs-parent</artifactId>
         <groupId>org.example</groupId>
         <version>1.0-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
      <!-- 子工程:项目名 -->
     <artifactId>bbs-dao</artifactId>
     <!-- 子工程定义特有依赖! -->
     <dependencies>
         <!-- mybatis:集成SpringJar -->
         <dependency>
             <groupId>org.mybatis</groupId>
             <artifactId>mybatis-spring</artifactId>
             <version>1.1.1</version>
         </dependency>
         <!-- c3p0数据源依赖 -->
         <dependency>
             <groupId>c3p0</groupId>
             <artifactId>c3p0</artifactId>
             <version>0.9.1.2</version>
             <type>jar</type>
         </dependency>
         <!-- mybatis依赖 -->
         <dependency>
             <groupId>org.mybatis</groupId>
             <artifactId>mybatis</artifactId>
             <version>3.2.8</version>
         </dependency>
         <!-- mysql驱动 -->
         <dependency>
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
             <version>5.1.34</version>
         </dependency>
         <!-- 添加实体模块坐标 -->
         <dependency>
             <groupId>org.example</groupId>
             <version>1.0-SNAPSHOT</version>
             <artifactId>bbs-entity</artifactId>
         </dependency>
     </dependencies>
 </project>

Spring核心配置文件:applicationContext-mybatis.xml C3P0 数据源定义:啥的也许有朋友,数据源不一样就不用单独去找了...

 <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
 ​
     <!-- Spring:扫描包下注解 -->
     <context:component-scan base-package="com.wsm.mapper"></context:component-scan>
     <!-- 数据源配置 C3P0 -->
     <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
         <!-- 连接驱动 库 用户 密码.. -->
         <property name="driverClass" value="com.mysql.jdbc.Driver"/>
         <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/bbs"></property>  <!-- 指定数据库别忘了! -->
         <property name="user" value="root"></property>
         <property name="password" value="ok"></property>
     </bean>
     <!-- 连接工厂 -->
     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
         <property name="dataSource" ref="dataSource"></property>
         <property name="configLocation" value="classpath:mybatis-config.xml"></property>
     </bean>
 ​
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
         <property name="basePackage" value="com.wsm.mapper"></property>
         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
     </bean>
 </beans>

bbs-web

视图层:主要组成 Controller控制器 和 JSP页面!负责展示效果! 在这里插入图片描述

  • SSM 的常规代码:
  • Controller控制器 SpringMVC配置! web.xml  jsp页面.... pom.xml 在这里插入图片描述

注意:

  • Maven web工程的打包方式是 war包Tomcat只能运行 war包~ 而其它模块是Jar包(默认) Maven的父工程是 pom类型打包!
  • 本这次演示的是SSM工程:Demo下载 Entity——Dao——Service——Web: 都是通过Maven依赖进行聚合的; 使用前要先上传至本地仓库install:通过依赖去本地仓库中查找... 当然可以通过父工程一键上传至本地仓库!

maven 私服 搭建:

  • 正式开发,不同的项目组开发不同的工程。 ssm_dao 工程开发完毕,发布到私服。 ssm_service 从私服下载 dao 不能在公司,拿着u盘 到处跑找人copy 依赖吧...

公司自己搭建的Maven仓库:

不同的人将自己开发好的模块,上传至私服上 , 当然需要的模块也可以去私服下载 在这里插入图片描述 上一篇已经讲解过私服的概念了...

搭建私服环境

下载 nexus

  • Nexus 是 Maven 仓库管理器
  • 通过 nexus 可以搭建 maven 仓库,同时 nexus 还提供强大的仓库管理功能,构件搜索功能等。
  • 下载 Nexus, 下载地址:www.sonatype.org/nexus/archi… 在这里插入图片描述

安装 nexus

解压 nexus-2.12.0-01-bundle.zip,本教程将它解压在D盘,进入 bin 目录: 在这里插入图片描述 执行 nexus.bat install 如果不行就管理员运行 在这里插入图片描述

在这里插入图片描述

卸载nexus

cmd 进入 nexus 的 bin 目录,执行:nexus.bat uninstall 查看 window 服务列表 nexus 已被删除。 在这里插入图片描述

启动 nexus

方法一

cmd 进入 bin 目录,执行 nexus.bat start

方法二

直接启动 nexus 服务 在这里插入图片描述

访问: http://localhost:8081/nexus/ 默认 端口8081

查看 nexus 的配置文件 conf/nexus.properties 在这里插入图片描述 常用配置:

 # Jetty section 
 application-port=8081 
 # nexus 的访问端口配置 
 application-host=0.0.0.0 
 # nexus 主机监听配置(不用修改) 
 nexus-webapp=${bundleBasedir}/nexus 
 # nexus 工程目录 
 nexus-webapp-context-path=/nexus 
 # nexus 的 web 访问路径 
 # Nexus section 
 nexus-work=${bundleBasedir}/../sonatype-work/nexus      # nexus 仓库目录 
 runtime=${bundleBasedir}/nexus/WEB-INF                  # nexus 运行程序目录

在这里插入图片描述 Nexus 内置账户admin 密码admin123

仓库类型

在这里插入图片描述

仓库分类

  • Public Repositories: 该仓库组将Policy(策略)为Release的仓库聚合并通过一个地址对外提供服务; 下载的上传的这里都会保存·
  • 3rd party: 用来部署无法从公共仓库获取的第三方发布版本的jar包
  • Apache Snapshots 用来代理Apache阿帕奇 Maven仓库的快照版本jar包 一种代理仓库,远程下载阿帕奇的依赖Jar
  • Central 该仓库代理Maven中央仓库,其Policy(策略)为Release,因此只会下载和缓存中央仓库中的发布版本jar包 和Apache 一样
  • Codehaus Snapshots 用来代理CodeHaus Maven仓库的快照版本jar包
  • Release 用户部署组织内部的发布版本的jar包
  • Snapshots 用来部署组织内部的快照(测试)版本的jar包

nexus 的仓库有 4 种类型

  • group:仓库组 用来合并多个 hosted/proxy 仓库,通常我们配置自己的 maven 连接仓 库组。
  • hosted:宿主 宿主仓库,部署自己的 jar 到这个类型的仓库 包括 releases 和 snapshot 两部分: Releases 公司内部发布版本仓库、 Snapshots 公司内部测试版本仓库
  • proxy:代理 代理仓库 用于代理远程的公共仓库 maven 中央仓库,用户连接私服,私服自动去中央仓库下载 jar 包或者插件。
  • virtual:虚拟 兼容 Maven1 版本的 jar 或者插件

仓库Format(格式)

有两种maven1和maven2,下面的仓库分类只介绍maven2

仓库Policy(策略)介绍

Release:发布版本 Snapshots:快照版本

nexus 仓库默认在 sonatype-work 目录中:

在这里插入图片描述

central:代理仓库,代理中央仓库

在这里插入图片描述

将项目发布到私服

ok,了解了私服 和 搭建好了私服, 就是将代码模块:上传至私服上并下载使用了!!

  • 企业中多个团队协作开发通常会将一些公用的组件、开发模块等发布到私服供其它团队或模块开发人员使用。
  • 本例子假设多团队分别开发 ssm_dao、ssm_service、ssm_web 某个团队开发完在 ssm_dao 会将 ssm_dao 发布到私服供 ssm_service 团队使用

在这里插入图片描述 上传/下载:当然是先上在下拉...

上传

第一步: 配置连接私服的用户和密码;

  • 需要在客户端即:要上传模块工程的电脑上配置 maven环境 指定要上传的仓库位置!修改 settings.xml

上传设备Maven的Setting.xml Maven安装目录: apache-maven-3.5.4-bin\apache-maven-3.5.4\conf 目录下 <servers> 标签下配置 </servers>

 <!-- releases 连接发布版本项目仓库 -->
 <server> 
     <id>releases</id> 
     <username>admin</username> 
     <password>admin123</password> 
 </server> 
 <!-- snapshots 连接测试版本项目仓库 -->
 <server> 
     <id>snapshots</id>
     <username>admin</username> 
     <password>admin123</password> 
 </server>
 <!-- 更多根据情况进行配置... -->

第二步: 要上传模块的 配置项目 pom.xml

  • 配置私服仓库的地址,要上传公司私服的宿主仓库
  • 根据工程的版本号决定上传到哪个宿主仓库 如果版本为 release 则上传到私服的 release 仓库 如果版本为snapshot 则上传到私服的 snapshot 仓库 在创建模块时候可以在pom.xml 中查看设置: 随便找一个模块发现它的配置版本是:<version>1.0-SNAPSHOT</version> 默认是SNAPSHOT 测试版本!

pom.xml

 <distributionManagement>
     <!-- 非测试版本上传位置配置 -->
     <repository>
         <!-- 这里的id 就是本地的Maven:Setting.xml配置的id -->
         <id>releases</id>
         <url>
             http://localhost:8081/nexus/content/repositories/releases/
         </url>
     </repository>
     <!-- 测试版本上传的位置: -->
     <snapshotRepository>
         <id>snapshots</id>
         <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
     </snapshotRepository>
 </distributionManagement>

因为本人的:上传设备 和 私服设备是同一个电脑, 所有这里就是localhost 实际开发根据公司的 端口更改! 而这个位置就是: 在这里插入图片描述

测试

将项目 dao 工程打成 jar 包发布到私服:

  • 首先启动 nexus
  • 确保本地仓库存在模块, 事先打包package 和 发布install 的本地仓库对 dao 工程执行Maven deploy上传 命令: 在这里插入图片描述 可以看到控制台。出现uploding 上传的提示:且可以在连接上查看到刚刚上传的模块. 当然本地仓库也是存在的!上传成功!
  • 根据本项目pom.xml中version定义决定发布到哪个仓库<version>1.0-SNAPSHOT</version>
  • 如果version定义为snapshot, 执行 deploy后查看 nexus 的 snapshot仓库
  • 如果 version定义为 release则项目将发布到 nexus的 release 仓库,本项目将发布到 snapshot 仓库:

下载

要下载设备的Meven setting.xml 中配置仓库

<profiles></profiles> 标签下配置:

 <profile> 
     <!--profile 的 id--> 
     <id>dev</id> 
     <repositories> 
     <repository> 
     
         <!--仓库 id,repositories 可以配置多个仓库,保证 id 不重复--> 
         <id>nexus</id> 
 ​
         <!--仓库地址,即 nexus 仓库组的地址:去public里找--> 
         <url>http://localhost:8081/nexus/content/groups/public/</url> 
 ​
         <!--是否下载 releases 构件--> 
         <releases> 
             <enabled>true</enabled> 
         </releases> 
     
         <!--是否下载 snapshots 构件--> 
         <snapshots> 
             <enabled>true</enabled> 
         </snapshots> 
     </repository> 
     </repositories> 
   
     <pluginRepositories>  
         <!-- 插件仓库,maven 的运行依赖插件,也需要从私服下载插件 --> 
         <pluginRepository> 
             <!-- 插件仓库的 id 不允许重复,如果重复后边配置会覆盖前边 --> 
             <id>public</id> 
             <name>Public Repositories</name> 
             <url>http://localhost:8081/nexus/content/groups/public/</url> 
         </pluginRepository> 
   </pluginRepositories> 
 </profile>

<settings></settings> 节点下配置:开启仓库配置

   <!-- 激活下载仓库配置! -->
   <activeProfiles>
     <activeProfile>dev</activeProfile>
   </activeProfiles>

测试:

为了更好看到效果建议: 删除本地Dao模块 删除本地仓库的Jar 直接编译Service模块就会开始根据依赖找Dao 模块了

  • 找不到就去私服下载, 私服没有就会去终于仓库 私服怎么会没有刚上传的...
  • 下载之后, 本地仓库就会自动的存储一个, 下次在调用就会直接去本地仓库找了!!
  • 以下可以看的 Downloading 下载的标识! 在这里插入图片描述 欧克, 到这Maven的学习就大致了解了, 基本满足开发需要了!建议三联哦~ 新的一年了, 祝各位新年快乐!Happy New Year 这两天有时间多更新几章(〃 ̄︶ ̄)人( ̄︶ ̄〃)