应用场景:假设现有项目 helloMvn,该项目有2个模块,helloMvnService 和 helloMvnPersist
需求:运行一次mvn命令,同时构建两个模块 。
解决办法:利用mvn的聚合特性。
操作:
1.新建项目helloMvnAggregator,该项目仅仅需要包含pom.xml 就可以 ,helloMvnAggregator和helloMvnService 、helloMvnPersist在同一个平行目录,假设项目路径为
helloMvn
--helloMvnService
--helloMvnPersist
--helloMvnAggregator
2.编写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.helloMvn</groupId>
<artifactId>helloMvnAggregator</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>HelloMvn Aggregator</name>
<modules>
<module>../HelloMvnService</module>
<module>../HelloMvnPersist</module>
</modules>
</project>
需要注意的关键点:
1.packaging 一定要选择pom(,如不写该标签,默认是jar ),否则无法聚合。
2.添加新标签 module,module内容是被聚合模块pom.xml 的相对地址 ,以上配置中../是指当前目录的上一层目录,即
HelloMvnService项目相对helloMvnAggregator项目的pom地址为 helloMvn/HelloMvnService/pom.xml
3.通常聚合模块会被放在模块的最顶层,被聚合的模块做为子目录存在,这样用户得到源代码时,第一眼就能发现聚合模块的pom,从而省去了寻找聚合模块pom的时间,同时项目路径也更加清晰 ,所以以上module配置应改为:
<modules>
<module>HelloMvnService</module>
<module>HelloMvnPersist</module>
</modules>
则,目录为:
helloMvn
--helloMvnAggregator
--helloMvnPersist
--helloMvnService