当你想在你的某个项目中,将spring-boot的这个依赖的版本号从1.5.6升级到2.0.0的时候,你最直接且高效的办法是将 <version>1.5.6</version>改为 <version>2.0.0</version>。ok,这自然没什么问题。但是,如果你的项目经理发现2.0.0的版本更好用,让你给公司的108个maven项目全更新一遍。此时的你,是不是有句f**k不知当讲不当讲。不过不用担心解决这个问题的办法早就有了,我们编程人员就是为解决重复操作而生的啊。这个办法的核心就是本文的重点——dependencyManagement。
首先,让我们来看看dependencyManagement在maven-4.0.0.xsd中的解释。http://maven.apache.org/xsd/maven-4.0.0.xsd
<xs:element name="dependencyManagement" minOccurs="0" type="DependencyManagement">
<xs:annotation>
<xs:documentation source="version">4.0.0</xs:documentation>
<xs:documentation source="description">
Default dependency information for projects that inherit from
this one. The dependencies in this section are not immediately resolved.
Instead, when a POM derived from this one declares a dependency
described by a matching groupId and artifactId, the version and other values from this
section are used for that dependency if they were not already specified.
</xs:documentation>
</xs:annotation>
</xs:element>
考虑到有些同学英文不太好的情况,我们先翻译一下。
工程的默认依赖信息是该工程继承的父工程中POM(笔者注:使用了dependencyManagement标签)申明的依赖信息。在dependencyManament这个区间申明的那些依赖不会马上被父工程解析。而是,当一个子工程中通过groupId和artifactId申明了一个依赖的时候,该子工程会根据父工程在dependencyManagement提供的版本和一些其他参数值去解析并下载依赖。如果版本信息和其他参数值在子工程中自定义了,则使用子工程定义的参数值。
通俗地说,就是在父工程的dependencyManagement里面申明的依赖,子工程通过使用groupId和artfactId去匹配父工程中的这个申明的依赖,然后子工程就有了这个依赖。子工程如果想自定义某个依赖版本号,就在引用依赖的时候表达出来。
所以你可以定义一个maven父工程专门管理版本,然后所有的项目都继承这个父工程。这样,以后全部升级某个依赖的版本号的时候,只需要修改父工程里面的版本号就行了。将n个操作转换成了1个操作,是不是有点酷酷滴。
官网:http://maven.apache.org/pom.html#Dependency_Management
这篇博文解释也不错:https://blog.youkuaiyun.com/weixin_42114097/article/details/81391024