maven中dependencyManagement和dependencies区别
maven使用dependencyManagement元素来提供了一种管理依赖版本号的方式,通常会在一个组织或者项目的最顶层父POM中看到dependencyManagement元素 例如:在父工程我们指定了spring-boot-dependencies为2.2.2.RELEASE
<dependencyManagement>
<dependencies>
<!--spring boot 2.2.2-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
</dependencyManagement>
然后再子项目中添加spring-boot-dependencies可以不指定版本号,不指定版本号就和老爸一样版本。
<dependencies>
<!--spring boot 2.2.2-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
</dependency>
好处:聚合管理,以后只许在父容器中修改即可
注意:
- dependencyManagement只是声明依赖,并不实现引入,因此,子项目需要显示的声明需要用依赖
- 如果不在子项目中声明依赖,是不会从父项目中继承下来的,只有在子项目中写了该依赖项,并且没有指定版本号,才会从父项目中继承该项,并且version和scope都读取自父pom
