为什么需要继承关系?

非compile范围的依赖(不参与打包)不能传递依赖关系

例如多个模块中使用了junit依赖,单元测试一般不参与打包所以范围一般是test范围,这样每个模块的junit版本很可能不一致,可能会导致一些意想不到的问题,为了防止出现问题,一般都会统一版本,这时就需要用继承来实现统一junit版本

在maven中怎么使用?

创建一个打包方式为pom方式的maven项目,作为父工程其中的<packaging>中写pom

父工程:

<groupId>公司域名</groupId>
<artifactId>模块名</artifactId>
<version>版本号</version>
<packaging>pom</packaging>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>要统一的junit</groupId>
<artifactId>模块名</artifactId>
<version>统一的版本号</version>
</dependency>
</dependencies>
</dependencyManagement>

子工程:添加<parent>标签指明父工程,下面的groupId、artifactId、version改成父工程的groupId、artifactId、version即可

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- 父工程的相对路径 -->
</parent>


maven中为什么需要用到聚合?

目的是将工程安装到maven的本地仓库中,maven的安装命令是mvn install,但是模块之间出现继承关系或者依赖关系,如果依赖的模块没有先被安装,则需要先将依赖安装好后才可以安装当前模块。

怎么使用聚合:可以在父工程中添加下面标签并且指明子工程的相对路径。也可以新建一个工程来做聚合(其中的module循序无所谓)

<modules>
<module>子工程1的相对路径</module>
<module>子工程2的相对路径</module>
</modules>

这样只需要在父工程中执行mvn install即可