maven进阶
模块拆分
- 模块拆分:即把原来的一个整体项目各个包(pojo,dao,service,controller)拆分成单独的模块
注意事项
模块与模块之间的资源如何通信?
使用maven的坐标引入即可,在引入模块之前,需要把被引入模块 install 安装到本地仓库。引入之后,点击maven工具 选中模块 使用complie指令进行检测 若build success 则引入成功!
原来各个包之间依赖的配置文件如何分配?
例如:原来dao包和service包共用application.xml文件,此时dao模块和service模块就可以拆分为 application-dao.xml 和 application-service.xml 各自保留自己需要使用的配置即可。另外,需要把两个配置文件聚合使用的时候注意 * 通配符的使用: application-*.xml
聚合
- 作用:用于统一管理项目各个模块的生命周期,一个模块更新,整个项目一起更新,从而快速构建项目
- 模块类型:war、jar(默认)、pom
- 操作:
创建一个空模块,打包类型定义为pom ==>聚合模块
<packaging>pom</packaging>
定义聚合模块进行关联的其他模块名称
<modules>
<module>../ssm_controller</module>
<module>../ssm_service</module>
<module>../ssm_dao</module>
<module>../ssm_pojo</module>
</modules>
- 注意事项:聚合操作的模块执行顺序与模块配置顺序无关
继承
继承:用一个父工程统一管理子工程共同资源的版本,以防不同子模块使用相同资源的不同版本导致的资源冲突,这样管理之后,子模块在使用资源的时候就不用写版本了,归父工程统一管理。
操作:
- 在父工程中定义依赖管理
<!--声明依赖管理 -->
<dependencyManagement>
<!-- 具体管理的依赖 -->
<dependencies>
<!--spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependencies>
<dependencyManagement>
- 在子工程中声明其父工程坐标与对应的位置
<!-- -->
<parent>
<groupId>com.itheima</groupId>
<artifactId>ssm</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- pom -->
<relativePath>../ssm/pom.xml</relativePath>
</parent>
- 在子工程使用的时候不用指定资源版本version
<dependencies>
<!--spring环境-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<!--<version>5.1.9</version>-->
</dependency>
</dependencies>
继承与聚合的区别
- 作用
聚合用于统一项目各个模块,继承用于快速统一模块中各个资源版本 - 相同
聚合与继承的pom.xml文件打包方式均为pom,继承和聚合可以同时存在
聚合与继承均属于设计型模块,并无实际的模块内容(可以不要src) - 不同
聚合是在当前模块中配置关系,聚合知道关联的模块有哪些
继承是在子模块中配置关系,父模块不知道哪些模块引用了自己
属性
概念:相当于java中的变量,用于父工程统一管理相同种类资源版本号。
操作:
properties标签定义
<!--定义自定义属性-->
<properties>
<spring.version>5.1.9.RELEASE</spring.version>
<junit.version>4.12</junit.version>
</properties>
${}引用
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>