一、继承
为了避免重复。通过继承拥有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
https://blog.csdn.net/wanghantong/article/details/36427411
在dependencyManagement中配置的元素既不会给parent引入依赖,也不会给它的子模块引入依赖,仅仅是它的配置是可继承的