maven的继承与聚合

1.聚合

为了能够使用一条命令就能构建 account-email account-persist两个模块,我们需要建立一个额外的名为 account-aggregator的模块,然后通过该模块构建整个项目的所有模块。 account-aggregator本身也是个 Maven项目,它的 POM如下

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <project>  
  2.     <modelVersion>4.0.0</modelVersion>  
  3.     <groupId>com.juvenxu.mvnbook.account</groupId>  
  4.     <artifactId>account-aggregator</artifactId>  
  5.     <version>1.0.0-SNAPSHOT</version>  
  6.     <packaging> pom </packaging>  
  7.     <name>Account Aggregator</name>  
  8.      <modules>  
  9.         <module>account-email</module>  
  10.         <module>account-persist</module>  
  11.      </modules>  
  12. </project>  
注意: packaging的类型为pom module的值是一个以当前POM为主目录的相对路径。

聚合模块与其他模块的目录结构并非一定是父子关系。

2.继承

可声明父POM供子 POM继承

父模块POM如下:

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <project>  
  2.     <modelVersion>4.0.0</modelVersion>  
  3.     <groupId>com.juvenxu.mvnbook.account</groupId>  
  4.     <artifactId> account-parent </artifactId>  
  5.     <version>1.0.0-SNAPSHOT</version>  
  6.     <packaging>pom</packaging>  
  7.     <name>Account Parent</name>  
  8. </project>  
子模块声明继承如下:

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <project>  
  2.     <modelVersion>4.0.0</modelVersion>  
  3.       
  4.     < parent >  
  5.         <groupId>com.juvenxu.mvnbook.account</groupId>  
  6.         <artifactId> account-parent </artifactId>  
  7.         <version>1.0.0-SNAPSHOT</version>  
  8.         < relativePath >../account-parent/pom.xml</ relativePath>  
  9.     </ parent >  
  10.       
  11.     <artifactId> account-email </artifactId>  
  12.     <name>Account Email</name>  
  13.   ...  
  14. </project>  
  最后,同样还需要把 account-parent加入到聚合模块account-aggregator中。

聚合的 POM如下:

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <project>  
  2.     <modelVersion>4.0.0</modelVersion>  
  3.     <groupId>com.juvenxu.mvnbook.account</groupId>  
  4.     <artifactId>account-aggregator</artifactId>  
  5.     <version>1.0.0-SNAPSHOT</version>  
  6.     <packaging> pom </packaging>  
  7.     <name>Account Aggregator</name>  
  8.     <modules>  
  9.         <module>account-email</module>  
  10.         <module>account-persist</module>  
  11.         <module> account-parent</module>  
  12.     </modules>  
  13. </project>  

1、子模块没有声明groupIdversion, 这两个属性继承至父模块。但如果子模块有不同与父模块的 groupIdversion ,也可指定;

2、不应该继承artifactId,如果groupId versionartifactId 完全继承的话会造成坐标冲突;另外即使使用不同的 groupIdversion,同样的 artifactId也容易产生混淆。
3
、使用继承后 parent也必须像自模块一样加入到聚合模块中。也就是在在聚合模块的 pom中加入<module>account-parent</module>


区别 
1. 对于聚合模块来说,它知道有哪些被聚合的模块,但那些被聚合的模块不知道这个聚合模块的存在。
2. 对于继承关系的父  POM 来说,它不知道有哪些子模块继承与它,但那些子模块都必须知道自己的父  POM 是什么。

共同点 
1. 聚合  POM 与继承关系中的父 POM   packaging 都是 pom
2. 聚合模块与继承关系中的父模块除了  POM 之外都没有实际的内容。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值