一、parent:子项目直接继承父依赖
有三个项目A、B、C,它们都需要用到一个通用的包。如果分别在三个项目的pom文件中定义各自对这个包的依赖,那么当依赖的版本发生变化时,三个项目的pom文件都要改。这时候我们创建一个parent项目,
打包类型为pom,再在parent项目新建三个项目A、B、C;在parent项目的pom文件中有的依赖,子项目都可以直接继承。
<!--父依赖:直接导入依赖,子类继承父依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
</parent>
<dependencies>
<!--子项目都有单元测试依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependencies>
二、dependencyManagement:父项目管理依赖版本,子项目根据需要引入依赖
有三个项目A、B、C,只有A和B需要某个依赖,C不需要,
如果A、B中分别定义依赖,当版本变化时修改起来有需要每个都单独修改。这时可以在在parent项目的pom文件中使用标签管理依赖,如果有哪个子项目要用,那么子项目在自己的pom文件中再引入这个依赖,只是无需输入版本号。
<!--依赖管理:管理依赖的版本,但并不引入依赖,子类需单独导入依赖,但无需写版本号-->
<dependencyManagement>
<dependencies>
<!--父项目定义了依赖,子类若使用需重新引入依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>