经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 软件/图像 » Maven » 查看文章
011.maven 继承与聚合
来源:cnblogs  作者:badboyh2o  时间:2019/5/10 8:48:03  对本文有异议

 

聚合:对于聚合模块来说,它知道有哪些被聚合的模块,而对于被聚合的模块来说,它们不知道被谁聚合了,也不知道它的存在;

继承:对于继承关系的父POM来说,它不知道自己被哪些子模块继承了,对于子POM来说,它必须知道自己的父POM是谁;

在一些最佳实践中我们会发现:一个POM既是聚合POM,又是父POM,这么做主要是为了方便。

链接:https://www.cnblogs.com/sxpy-lj/p/7251418.html

 

一、继承

为了避免重复。通过继承拥有Parent项目中的配置。

https://www.cnblogs.com/xdp-gacl/p/4058008.html

  1. <!--子工程POM 继承父工程-->    
  2. <parent>    
  3.     <artifactId>父artifactId</artifactId>    
  4.     <groupId>父groupId</groupId>    
  5.     <version>父version</version>    
  6.     <relativePath>父pom.xml的相对路径</relativePath>    
  7. </parent>  

 

  

二、聚合

 主要为了快速构建项目,可以一次构建所有子模块。

 https://www.cnblogs.com/xdp-gacl/p/4058008.html

  1. //聚合工程 POM (module的值为子模块项目目录相对于聚合工程POM的路径)
  2. <modules>
  3. <module>artifactId01</module>
  4.    <module>artifactId02</module>
  5. </modules>

 

 

 三、解决多个子模块依赖某个子模块的版本统一的问题

   如果一个项目有多个子模块,而且每个模块都需要引入依赖,但为了项目的正确运行,必须让所有的子项目(以下子项目即指子模块)使用依赖项的版本统一,才能保证测试的和发布的是相同的结果。

  Maven 使用 dependencyManagement 来统一模块间的依赖版本问题。

  子模块中引用一个依赖而不用显式列出版本号,Maven会沿着父子层次向上走,直到找到一个拥有dependencyManagement元素的项目,然后它就会使用在dependencyManagement元素中指定的版本号。

  

  1. // 聚合工程 (同时也是父工程)
  2. // 以下两个 子模块依赖的 javaee-api 版本均为 ${javaee-api.version}
  3. <groupId>groupId</groupId>
  4. <artifactId>artifactId</artifactId>
  5. <version>version</version>
  6. <packaging>pom</packaging>
  7.  
  8. <modules>
  9. <module>artifactId01</module>
  10.    <module>artifactId02</module>
  11. </modules>
  12.  
  13.  
  14. <dependencyManagement>
  15. <dependencies>
  16. <dependency>
  17. <groupId>javax</groupId>
  18. <artifactId>javaee-api</artifactId>
  19. <version>${javaee-api.version}</version>
  20. </dependency>
  21. </dependencies>
  22. </dependencyManagement>

 

  1. // 子模块01
  2. <!--继承父工程-->
  3. <parent>
  4. <artifactId>artifactId</artifactId>
  5. <groupId>groupId</groupId>
  6. <version>version</version>
  7. <relativePath>pom.xml的相对路径</relativePath>
  8. </parent>
  9.  
  10.  
  11. <groupId>groupId01</groupId>
  12. <artifactId>artifactId01</artifactId>
  13. <packaging>jar</packaging>
  14.  
  15. <!--依赖关系-->
  16. <dependencies>
  17. <dependency>
  18. <groupId>javax</groupId>
  19. <artifactId>javaee-api</artifactId>
  20. </dependency>
  21. </dependencies>

 

  1. // 子模块02
  2. <!--继承父工程-->
  3. <parent>
  4. <artifactId>artifactId</artifactId>
  5. <groupId>groupId</groupId>
  6. <version>version</version>
  7. <relativePath>pom.xml的相对路径</relativePath>
  8. </parent>
  9.  
  10.  
  11. <groupId>groupId02</groupId>
  12. <artifactId>artifactId02</artifactId>
  13. <packaging>jar</packaging>
  14.  
  15. <!--依赖关系-->
  16. <dependencies>
  17. <dependency>
  18. <groupId>javax</groupId>
  19. <artifactId>javaee-api</artifactId>
  20. </dependency>
  21. </dependencies>

 

https://blog.csdn.net/qq_39940205/article/details/80307795

 

 

  • dependencyManagement 特性

 https://blog.csdn.net/wanghantong/article/details/36427411

在dependencyManagement中配置的元素既不会给parent引入依赖,也不会给它的子模块引入依赖,仅仅是它的配置是可继承的

 

原文链接:http://www.cnblogs.com/badboyh2o/p/10840921.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号