# 继承与聚合
# 聚合
聚合:将多个模块组织成一个整体,同时进行项目构建的过程称为聚合
聚合工程:通常是一个不具有业务功能的,"空" 工程 (有且仅有一个 pom 文件)
作用:使用聚合工程可以将多个工程编组,通过对聚合工程进行构建,实现对所有包含的模块进行同步构建
- 当工程中某个模块发生更新 (变更) 时,必须保障工程中与已更新模块关联的模块同步更新,此时可以使用聚合工程来解决批量模块同步构建的问题
 

- 创建 Maven 模块,设置打包类型为 pom
 
<packaging>pom</packaging>  | 
- 注意事项: 每个 Maven 工程都有对应的打包方式,默认为 jar,web 工程默认为 war
 
- 设置当前聚合工程所包含的子模块名称
 
<modules> | |
<module>...</module>  | |
<module>...</module>  | |
<module>...</module>  | |
</modules>  | 
- 注意事项: 聚合工程中所包含的模块在进行构建时会根据模块间的依赖关系设置构建顺序,与聚合工程中模块的配置书写位置无关,参与聚合的工程无法向上感知是否参与聚合,只能向下配置哪些模块参与本工程的聚合
 
# 继承
概念:继承描述的是两个子工程间的关系,与 java 中的继承相似,子工程可以继承父工程中的配置信息,常见于依赖关系的继承
作用:
- 简化配置
 - 减少版本冲突
 
<relativePath>: 快速找到父工程
<relativePath> 父工程 pom.xml 文件路径 比如: ../pom.xml</relativePath>
- 父 pom.xml
 
<?xml version="1.0" encoding="UTF-8" standalone="no"?> | |
<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.dkx.spring</groupId>  | |
<artifactId>maven-spring-fu</artifactId>  | |
<version>1.0-SNAPSHOT</version>  | |
<packaging>pom</packaging>  | |
<name>maven-spring-fu</name>  | |
<url>http://maven.apache.org</url>  | |
  <properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  | |
  </properties> | |
    <dependencies> | |
      <dependency> | |
<groupId>org.springframework</groupId>  | |
<artifactId>spring-webmvc</artifactId>  | |
<version>5.2.10.RELEASE</version>  | |
      </dependency> | |
      <dependency> | |
<groupId>org.mybatis</groupId>  | |
<artifactId>mybatis-spring</artifactId>  | |
<version>2.0.6</version>  | |
      </dependency> | |
      <dependency> | |
<groupId>org.mybatis</groupId>  | |
<artifactId>mybatis</artifactId>  | |
<version>3.5.11</version>  | |
      </dependency> | |
      <dependency> | |
<groupId>org.springframework</groupId>  | |
<artifactId>spring-test</artifactId>  | |
<version>5.2.11.RELEASE</version>  | |
      </dependency> | |
    </dependencies> | |
  <build> | |
    <resources> | |
      <resource> | |
<directory>src/main/resources</directory>  | |
        <includes> | |
<include>**/*.properties</include>  | |
<include>**/*.xml</include>  | |
        </includes> | |
<filtering>true</filtering>  | |
      </resource> | |
      <resource> | |
<directory>src/main/java</directory>  | |
        <includes> | |
<include>**/*.properties</include>  | |
<include>**/*.xml</include>  | |
        </includes> | |
<filtering>true</filtering>  | |
      </resource> | |
    </resources> | |
    <plugins> | |
    <plugin> | |
<groupId>org.apache.tomcat.maven</groupId>  | |
<artifactId>tomcat7-maven-plugin</artifactId>  | |
<version>2.2</version>  | |
        <configuration> | |
<port>80</port>  | |
<path>/</path>  | |
<uriEncoding>utf-8</uriEncoding>  | |
        </configuration> | |
    </plugin> | |
    </plugins> | |
  </build> | |
  <modules> | |
<module>maven-spring-zi</module>  | |
<module>maven-spring-zi01</module>  | |
<module>maven-spring-zi02</module>  | |
  </modules> | |
</project> | 
- 子 pom.xml
 
<?xml version="1.0"?> | |
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  | |
<modelVersion>4.0.0</modelVersion>  | |
  <parent> | |
<groupId>com.dkx.spring</groupId>  | |
<artifactId>maven-spring-fu</artifactId>  | |
<version>1.0-SNAPSHOT</version>  | |
<relativePath>../pom.xml</relativePath>  | |
  </parent> | |
<artifactId>maven-spring-zi</artifactId>  | |
<version>1.0-SNAPSHOT</version>  | |
<name>maven-spring-zi</name>  | |
<url>http://maven.apache.org</url>  | |
  <properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  | |
  </properties> | |
  <dependencies> | |
  </dependencies> | |
</project> | 
查看 maven Dependencies 拥有的 jar

- 将子工程中的 parent 给注释掉
 

- 刷新查看 Dependencies 拥有 jar
 

子工程中的 jar 自动消失不见了
将子工程的 parent 注释去掉后,刷新 jar 资源就又回来了

# dependencyManagement
依赖管理
父工程提供给子工程可选的依赖资源,子工程要使用需要自己写上而不需要写 version,vserion 在父工程中进行统一管理
父 pom.xml
<dependencyManagement> | |
    <dependencies> | |
      <dependency> | |
<groupId>org.springframework</groupId>  | |
<artifactId>spring-webmvc</artifactId>  | |
<version>5.2.10.RELEASE</version>  | |
      </dependency> | |
      <dependency> | |
<groupId>org.mybatis</groupId>  | |
<artifactId>mybatis-spring</artifactId>  | |
<version>2.0.6</version>  | |
      </dependency> | |
      <dependency> | |
<groupId>org.mybatis</groupId>  | |
<artifactId>mybatis</artifactId>  | |
<version>3.5.11</version>  | |
      </dependency> | |
      <dependency> | |
<groupId>org.springframework</groupId>  | |
<artifactId>spring-test</artifactId>  | |
<version>5.2.11.RELEASE</version>  | |
      </dependency> | |
    </dependencies> | |
    </dependencyManagement> | 
- 查看父工程与子工程的 Dependencies
 

- 子 pom.xml
 
<dependencies> | |
    <dependency> | |
<groupId>org.springframework</groupId>  | |
<artifactId>spring-webmvc</artifactId>  | |
    </dependency> | |
  </dependencies> | 
- 查看父工程与子工程的 Dependencies
 

注意事项:
子工程中使用父工程中的可选依赖时,仅需要提供群组 id 和项目 id, 无需提供版本,版本由父工程统一提供,避免版本冲突,子工程中还可以定义父工程中没有定义的依赖关系
# 聚合与继承的区别
- 作用
- 聚合用于快速构建项目
 - 继承用于快速配置
 
 - 相同点:
- 聚合与继承的 pom.xml 文件打包方式均为 pom, 可以将两种关系制作到同一个 pom 文件中
 - 聚合与继承均属于设计型模块,并无实际的模块内容
 
 - 不同点:
- 聚合是在当前模块中配置关系,聚合可以感知到参与聚合的模块有哪些
 - 继承是在子模块中配置关系,父模块无法感知哪些子模块继承了自己