[Maven 基础课程]Maven 工程继承和聚合
继承
继承是用来统一管理多个子模块的公共配置。你可以创建一个父项目(Parent Project),在父项目的 pom.xml
中定义通用的配置(比如依赖版本、插件配置、变量等),然后让所有的子模块都继承这个父项目。
作用:
- 统一版本:避免在每个子模块中重复定义相同的依赖版本,防止版本不一致导致的冲突。例如,你可以将 Spring Framework 的版本号统一在父 POM 中管理。
- 简化配置:将所有公共的插件、属性等配置集中到父 POM 中,子模块只需要简单继承即可,减少了重复代码。
- 强制统一:确保所有子模块都遵循相同的构建规范和依赖版本,有利于团队协作和项目维护。
要达到 Maven 工程继承的效果,在子模块的 pom.xml
中,使用 <parent>
标签来指定父项目的坐标即可:
<project><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>parent</artifactId><version>1.0.0-SNAPSHOT</version><!-- 当前工程作为父工程,它要去管理子工程,所以打包方式必须是 pom --><packaging>pom</packaging> <properties><spring.version>5.3.20</spring.version><spring-context.version>5.3.20</spring-context.version></properties><!-- 使用 dependencyManagement 标签配置对依赖的管理 --><dependencyManagement><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.version}</version></dependency></dependencies></dependencyManagement>
</project>
<project><modelVersion>4.0.0</modelVersion><parent><groupId>com.example</groupId><artifactId>parent</artifactId><version>1.0.0-SNAPSHOT</version></parent><artifactId>child</artifactId><packaging>jar</packaging><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId></dependency><!-- 使用从父项目继承过来的变量 --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring-context.version}</version></dependency></dependencies>
</project>
在父项目中,我们使用 dependencyManagement 标签配置对依赖的管理,被管理的依赖并没有真正被引入到项目,子项目导入了父工程中dependencyManagement 管理的依赖时,就可以把版本号去掉,这样就表示子项目中这个依赖的版本由父项目决定,从而达到由父工程来统一管理所有的子项目的依赖的版本的效果。
在父项目中统一控制所有子项目的插件版本:
<build><pluginManagement><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.11.0</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>3.1.2</version><configuration><skipTests>true</skipTests></configuration></plugin></plugins></pluginManagement>
</build>
聚合
聚合是用来一次性构建多个项目。你可以创建一个聚合父项目(Aggregator Project),通过 <modules>
标签将所有子模块包含进来。当你对这个父项目执行构建命令(如 mvn install
)时,Maven 会自动依次构建所有子模块。
作用:
- 简化构建命令:你不需要进入每一个子模块的目录去执行
mvn install
,只需要在聚合父项目的根目录执行一次命令即可。 - 强制构建顺序:Maven 会根据子模块之间的依赖关系,自动决定正确的构建顺序。例如,如果
child2
依赖child1
,Maven 会先构建child1
,再构建child2
。 - 提高效率:对于大型项目,可以确保所有模块都以正确的顺序构建和安装,避免手动构建的错误。
要达到聚合的效果,在聚合父项目的 pom.xml
中,使用 <modules>
标签来列出所有子模块的目录。
<project><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>aggregator-parent</artifactId><version>1.0.0-SNAPSHOT</version><packaging>pom</packaging><modules><module>../module-a</module><module>../module-b</module></modules>
</project>
这里的 <module>
标签指定的是子模块目录的相对路径。
<project><modelVersion>4.0.0</modelVersion><artifactId>moduleA</artifactId><packaging>jar</packaging><dependencies></dependencies>
</project>
在实际项目中,继承和聚合通常会结合使用。一个项目既是父项目,也是聚合项目:
- 你可以创建一个顶层的
pom.xml
,它的packaging
是pom
。 - 这个 POM 既通过
<modules>
聚合了所有的子模块,同时,它又作为所有子模块的<parent>
,提供统一的依赖管理和配置。
这种模式在大型的微服务或多模块应用中非常常见。