假设一个项目包含两个模块,咱们想要一次构建两个模块,而不是到两个模块的目录下分别执行mvn命令,这时就须要用到聚合。apache
聚合模块pom配置maven
packaging
类型为 pom
modules
元素,每一个module
对应一个要被聚合的子模块。module
的值是当前pom的相对目录。具体配置工具
<modules> <module>模块1</module> <module>模块2</module> <module>模块3</module> </modules>
聚合模块相对位置ui
聚合模块意义url
聚合模块构建插件
解决重复配置问题。版本控制
项目继承的配置code
packaging
类型为pom
。子模块须要配置project -> parent
元素信息xml
relativePath
: 父模块pom文件所在路径,默认父pom在上一层目录下(即 ../pom.xml
)。子模块继承父模块的配置继承
<parent> <groupId>父构件groupId</groupId> <artifactId>父构件artifactId</artifactId> <version>父构件的版本号</version> <relativePath>父构件pom.xml路径</relativePath> </parent>
可继承的pom元素
依赖管理
dependencyManagement
元素既能让子模块继承到父模块的依赖配置,又能保证子模块依赖使用的灵活性.dependencyManagement
元素下的依赖声明不会引入实际的依赖
,可是它能约束对dependencies
下的依赖使用。import
依赖范围:
dependencyManagement
配置导入,并合并到当前项目的dependencyManagement
元素中。import依赖范围通常都指向打包类型为pom的模块。
<dependencyManagement> <dependencies> <dependency> <groupId>targetGroupId</groupId> <artifactId>targetArtifactId</artifactId> <version>targetVersion</version> <type>pom</type> <scope>import</scope> </dependency> <dependency>构件1</dependency> <dependency>构件2</dependency> </dependencies> </dependencyManagement>
插件管理
pluginManagement
元素帮助管理插件继承。plugins -> plugin
元素声明该插件的时候,插件才会起效。groupId、artifactId
,其余信息均可以从父pom中传递过来。举例,使用maven-source-plugin插件生成项目源码包,将该插件的jar-no-fork目标绑定到default生命周期的verify阶段。
父pom插件管理配置
<build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>3.2.1</version> <executions> <execution> <id>attach-source</id> <phase>verify</phase> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin> </plugins> </pluginManagement> </build>
子pom插件声明配置
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> </plugin> </plugins> </build>
目的
与其余模块关系
parent
来引用父模块。共同点
实际使用
反应堆
单模块项目的反应堆
多模块项目的反应堆
说明
裁剪反应堆选项
mvn -h
能够看到裁剪反应堆的选项。-am
:--also-make, 同时构建所列模块的依赖模块。-amd
:-also-make-dependencies,同时构建依赖于所列模块的模块。-pl
:--projects <arg>,构建指定的模块,模块之间使用逗号分隔-rf
:-resume-from <arg>,从指定的模块继续反应堆