1、dependencyManagement
对于dependencyManagement,我们首先想到的就是统一管理版本
例如:
在父pom文件中声明spring-context的版本为5.3.18
<dependencyManagement><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.18</version></dependency></dependencies></dependencyManagement>
然后在子pom文件中引入,并且不用定义版本号,会自动使用父pom中定义的版本
<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId></dependency></dependencies>
实际导入的jar包如下
2、dependencyManagement其实也可以管理依赖项下自带的其他依赖的版本
举个例子:
A依赖B,B依赖C,在项目A中引入B的依赖,会自动导入依赖C,且版本是v1
A——》B——》C(v1)
这时,如果在A中加入dependencyManagement或者A的父POM中使用dependencyManagement管理C的版本为v2
<dependencyManagement><dependencies><dependency><groupId>xxx</groupId><artifactId>C</artifactId><version>v2</version></dependency></dependencies></dependencyManagement>
此时A项目中引入的C项目的jar包版本就是v2了,而不是B中声明的v1版本
例如:
在父pom中管理了这么一个依赖:spring-core,版本是5.1.8.RELEASE
<dependencyManagement><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>5.1.8.RELEASE</version></dependency></dependencies></dependencyManagement>
现在在子pom中有这么一个依赖:spring-context,版本是5.3.18
<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.18</version></dependency></dependencies>
spring-context,版本是5.3.18,下面又有这些依赖:里面的spring-core的版本也是5.3.18
这时也许你认为实际导入也应该是5.3.18的spring-core包,但实际上却是:5.1.8.RELEASE
这是因为dependencyManagement可以管理依赖项中引入其他附带的jar包的版本
就算只是在子pom中的dependencyManagement中加入版本管理,同样可以管理依赖项下引进的其他jar包版本
例如:
此时的版本也同样是5.1.8.RELEASE
