文章目录
- 多模块构建维护
- 一、聚合
- 二、继承
- 三、聚合与继承
- 四、属性
- 4.1 介绍
- 4.2 常见的属性及其调用方式
- 1、自定义属性
- 2、内置属性
- 3、setting属性
- 4、java系统属性
- 5、环境变量属性
多模块构建维护

可以看到通过上一章的讲解,我们将一个ssm项目分模块管理,形成了pojo、dao、service、controller四个模块,并且逐层递进形成依赖关系,假设此时dao层模块出现了改动,我们该如何进一步管理呢?
一、聚合
聚合用于快速构建maven工程,一次性构建多个项目、模块
操作起来很简单:
- 创建一个空模块,打包类型定义为pom
- 定义当前模块进行构建操作时关联其他的模块(绑定)
注:参与合操作的模块最终执行顺序与模块间的依赖关系有关,与配置顺序无关
首先构建一个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>com.zyx</groupId>
<artifactId>maven_ssm</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>../maven_ssm_pojo</module>
<module>../maven_ssm_dao</module>
<module>../maven_ssm_service</module>
<module>../maven_ssm_controller</module>
</modules>
</project>
对项目进行整体的编译,可以看到结果显示的是所有的模块选项:

install一下:

可以看到对应的文件夹下添加了jar包文件:

返回顶部
二、继承
模块间依赖关系的维护

实际开发过程中,由于每个模块一个负责人,所以会导致每个人使用的相同依赖出现不同版本,可能会引发版本冲突问题。
如上图所示,假设service、dao模块都用到了spring-context依赖,但是一个5.1.9,另一个5.2.0,就有可能引起版本的问题。此时我们可以通过继承实现子工程中沿用父工程(ssm)的配置,统一版本。
首先在父工程中声明其子工程的所需要的所有坐标,并配置声明依赖管理:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.zyx</groupId>
<artifactId>maven_ssm_pojo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.zyx</groupId>
<artifactId>maven_ssm_dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.zyx</groupId>
<artifactId>maven_ssm_service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>80</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
子工程中对应声明使用父工程的依赖配置,同时将子工程所有的版本信息去除:
<parent>
<groupId>com.zyx</groupId>
<artifactId>maven_ssm</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../maven_ssm/pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
</dependencies>
可继承的资源:

返回顶部
三、聚合与继承
作用
相同点
- 聚合与继承的pom.xm文件打包方式均为pom,可以将两种关系制作到同一个pom文件中
- 聚合与继承均属于设计型模块,并无实际的模块内容
不同点
- 聚合是在当前模块中配置关系,聚合可以感知到参与聚合的模块有哪些
- 继承是在子模块中配置关系,父模块无法感知哪些子模块继承了自己
返回顶部
四、属性
4.1 介绍
版本统一的重要性

虽然我们可以在父项目中进行依赖资源的统一管理,但是不免我们有时会出错,如上图所示,上下配置的资源版本不一致,这种情况下我们可以增添属性信息避免人为配置的出错。
<properties>
<spring.version>5.1.9.RELEASE</spring.version>
<junit.version>4.12</junit.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
对于我们继承的项目依赖,版本同样可以统一,因为使用的是maven的项目对象版本,在最开始就声明了<version>1.0-SNAPSHOT</version>
,所以后面直接使用${version}
调用就行了。
<?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>com.itheima</groupId>
<artifactId>ssm</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.itheima</groupId>
<artifactId>ssm_pojo</artifactId>
<version>${version}</version>
</dependency>
<dependency>
<groupId>com.itheima</groupId>
<artifactId>ssm_dao</artifactId>
<version>${version}</version>
</dependency>
<dependency>
<groupId>com.itheima</groupId>
<artifactId>ssm_service</artifactId>
<version>${version}</version>
</dependency>
</dependencies>
</dependencyManagement>
返回顶部
4.2 常见的属性及其调用方式
1、自定义属性

2、内置属性

3、setting属性

4、java系统属性


5、环境变量属性


返回顶部