011.maven 继承与聚合

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

继承:对于继承关系的父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

<!--子工程POM   继承父工程-->    
<parent>    
    <artifactId>父artifactId</artifactId>    
    <groupId>父groupId</groupId>    
    <version>父version</version>    
    <relativePath>父pom.xml的相对路径</relativePath>    
</parent>  

  

二、聚合

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

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

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

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

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

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

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

  

// 聚合工程 (同时也是父工程)
// 以下两个 子模块依赖的 javaee-api 版本均为 ${javaee-api.version}
<groupId>父groupId</groupId>  
<artifactId>父artifactId</artifactId>    
<version>父version</version>
<packaging>pom</packaging>  

<modules>
    <module>子artifactId01</module>
   <module>子artifactId02</module>
</modules>


<dependencyManagement>    
    <dependencies>    
        <dependency>    
            <groupId>javax</groupId>    
            <artifactId>javaee-api</artifactId>    
            <version>${javaee-api.version}</version>    
        </dependency>    
    </dependencies>    
</dependencyManagement>
// 子模块01 

<!--继承父工程-->    
<parent>    
    <artifactId>父artifactId</artifactId>    
    <groupId>父groupId</groupId>    
    <version>父version</version>    
    <relativePath>父pom.xml的相对路径</relativePath>    
</parent>


<groupId>子groupId01</groupId>
<artifactId>子artifactId01</artifactId>
<packaging>jar</packaging>

    
<!--依赖关系-->    
<dependencies>    
    <dependency>    
        <groupId>javax</groupId>    
        <artifactId>javaee-api</artifactId>    
    </dependency>    
</dependencies> 
// 子模块02 

<!--继承父工程-->    
<parent>    
    <artifactId>父artifactId</artifactId>    
    <groupId>父groupId</groupId>    
    <version>父version</version>    
    <relativePath>父pom.xml的相对路径</relativePath>    
</parent>


<groupId>子groupId02</groupId>
<artifactId>子artifactId02</artifactId>
<packaging>jar</packaging>

    
<!--依赖关系-->    
<dependencies>    
    <dependency>    
        <groupId>javax</groupId>    
        <artifactId>javaee-api</artifactId>    
    </dependency>    
</dependencies> 

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

  • dependencyManagement 特性

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

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

原文地址:https://www.cnblogs.com/badboyh2o/p/10840921.html