Maven的dependencyManagement和传递依赖

当使用了传递依赖,也就是使用了没有显示声明的依赖时,若是继承的<dependencyManagement/>中声明了使用的传递依赖的版本,那么最终使用的依赖是<dependencyManagement/>中声明的版本。html

Spring举一个栗子:spring

1.mvn dependency:tree

先看下项目依赖的基础组件使用的Spring版本。apache


能够看到基础组件使用的Spring版本为3.1.3,不能lambda表达式(被坑过)。maven


2.mvn dependency:analyze

接下来分析下依赖,关注Spring的相关依赖。ide


能够看到Spring做为传递依赖使用,版本是基础组件使用的版本3.1.3ui


3.parent module

而后在项目的parent module<dependencyManagement/>加入一个spring-framework-bom来管理Spring的版本。this


${springframework.version}5.0.4.RELEASEspa


4.从新执行mvn dependency:tree


能够看到Spring的版本变为了5.0.4.RELEASE3d


在执行mvn dependency:analyze后看到使用的Spring版本也变成了5.0.4.RELEASEcode


5.文档

  • Dependency management - this allows project authors to directly specify the versions of artifacts to be used when they are encountered in transitive dependencies or in dependencies where no version has been specified. In the example in the preceding section a dependency was directly added to A even though it is not directly used by A. Instead, A can include D as a dependency in its dependencyManagement section and directly control which version of D is used when, or if, it is ever referenced.


文档链接: maven.apache.org/guides/intr…
相关文章
相关标签/搜索