把项目的各个模块(子工程)聚合在一起构建。一般用于分模块开发,最后整体打包发布,聚合项目一定是父子工程,而父子工程不一定是聚合工程。
将多个工程拆分为模块后,需要手动逐个安装到仓库后依赖才能够生效。修改源码后也需要逐个手动进行 clean 操作。而使用了聚合之后就可以批量进行 Maven 工程的安装、清理工作。
这次搭建两个子工程,maven_demo7,maven_demo9
<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.gzl.cn</groupId> <artifactId>Parent1</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <!-- 配置聚合 --> <modules> <module>../maven_demo7</module> <module>../maven_demo9</module> </modules> <!-- 配置依赖的管理 --> <dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> </dependencies> </dependencyManagement> </project>指定父工程
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.gzl.cn</groupId> <artifactId>Parent2</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 以当前文件为基准的父工程pom.xml文件的相对路径 --> <relativePath>.../Parent2/pom.xml</relativePath> </parent> <artifactId>maven_demo7</artifactId> <name>maven_demo7</name> </project>用的Eclipse的JavaEE视图
对parent2打包,maven install他自己会把子项目安装,这就是聚合的作用,不用一个一个去安装,假如不用聚合的话,我们肯定得先安装maven_demo7,maven_demo9才能用,因为9引入了7。
父工程当中的。
<modules> <module>maven_demo7</module> <module>maven_demo9</module> </modules>子工程当中的
<!-- 以当前文件为基准的父工程pom.xml文件的相对路径 --> <relativePath>.../Parent2/pom.xml</relativePath>