背景
项目使用的是boot 2.2.0 版本,但是子模块A用于加密的jasypt只支持2.1.0。如何在不改变整体boot版本的情况下,让A单独使用2.1.0版本?
尝试子项目中直接修改boot版本
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
spring-boot-dependencies 中管理了boot很多相关依赖的版本号,即使如下图般修改了子模块的boot 版本号,还可以看到boot下面的依赖版本依旧是2.2.0:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
这就是告诉我们子项目的boot 下面的jar 包仍然受父项目的管理,我们这样改只能改变boot自身的版本。这也是我有些不明白的地方,按理说应该层次分明,管理分明。子项目管理自己的,父项目管理子项目。
使用dependencyManagement
经过我不懈地百度,看到一段话:
“使用dependencyManagement是为了统一管理版本信息
在子工程中使用时,还是需要引入坐标的,但是不需要给出version
在我们项目顶层的POM文件中,dependencyManagement元素。
通过它元素来管理jar包的版本,
让子项目中引用一个依赖而不用显示的列出版本号。
Maven会沿着父子层次向上找,
直到找到一个拥有dependencyManagement元素的项目,
然后它就会使用在这个dependencyManagement元素中指定的版本号。”
猜测:那么,如果我在子项目中建立一个dependencyManagement,指定boot 版本为2.1 ,这样子项目中其它的jar包就也会是2.1了。
实践:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
看到了吗?子项目中的都变成了2.1的版本。大功告成!