聚合-->多模块:一次构建多个项目
继承-->多模块:消除重复配置
一、聚合
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.hz.yisheng.account</groupId>
<artifactId>account-aggregator</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>Account Aggregator</name>
<modules>
<module>account-email</module>
<module>account-persist</module>
</modules>
</project>
可声明父POM供子 POM继承
父模块POM如下:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.hz.yisheng.account</groupId>
<artifactId> account-parent </artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>Account Parent</name>
</project>
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.hz.yisheng.account</groupId>
<artifactId>account-parent</artifactId>
<version>1.0</version>
<relativePath>../account-parent/pom.xml</relativePath>
</parent>
<artifactId>account-email</artifactId>
<name>Account Email</name>
...
</project>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.hz.yisheng.account</groupId>
<artifactId>account-aggregator</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>Account Aggregator</name>
<modules>
<module>account-email</module>
<module>account-persist</module>
<module>account-parent</module>
</modules>
</project>
1、子模块没有声明groupId和version, 这两个属性继承至父模块。但如果子模块有不同与父模块的 groupId、version ,也可指定;
2、不应该继承artifactId,如果groupId ,version,artifactId 完全继承的话会造成坐标冲突;另外即使使用不同的 groupId或version,同样的 artifactId也容易产生混淆。
3、使用继承后 parent也必须像自模块一样加入到聚合模块中。也就是在在聚合模块的 pom中加入<module>account-parent</module>
在实际的项目中,我们通常把聚合模块和父模块,放在一起实现。
反应堆:
在一个多模块的Maven项目中,反应堆(Reactor)是指所有模块组成的一个构建结构。对于单模块的项目,反应堆就是该模块本身,但对于多模块的项目来说,反应堆就包含了各模块之间继承与依赖的关系,从而能够自动计算出合理的模块构建顺序。
反应堆的构建顺序:
实际的构建顺序是这样形成的:Maven按序读取POM,如果该POM没有依赖模块,那么就构件该模块,否则就先构建其依赖模块,如果该依赖还依赖于其他模块,则进一步先构建依赖的依赖。
模块间的依赖关系会将反应堆构成一个有向非循环图(DAG),各个模块是该图的节点,依赖关系构成了有向边。这个图不允许出现循环,因此,当出现模块A依赖于B,而B又依赖于A的情况时,Maven就会报错。