Maven依赖版本更新踩坑

news/2024/9/28 18:18:22

问题描述

项目xx基于Spring Boot框架,其<parent>配置如下:

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.4.2</version><relativePath />
</parent>

spring-boot-dependencies-2.4.2.pom中通过<dependencyManagement>配置的caffine版本为2.8.8

<properties><caffeine.version>2.8.8</caffeine.version>
</properties>
<dependencyManagement><dependencies><dependency><groupId>com.github.ben-manes.caffeine</groupId><artifactId>caffeine</artifactId><version>${caffeine.version}</version></dependency></dependencies>
</dependencyManagement>

项目yy是一个基础的Maven模块,在其pom.xml中添加了3.1.8版本的caffeine配置。

<dependency><groupId>com.github.ben-manes.caffeine</groupId><artifactId>caffeine</artifactId><version>3.1.8</version>
</dependency>

此时在xx项目中添加对yy模块的依赖,即:

<dependency><groupId>x.y.z</groupId><artifactId>yy</artifactId><version>1.0</version>
</dependency>

至此,重新梳理下xx,yy,Spring Boot以及caffeine的依赖关系如下:

如上图,按照对Maven传递依赖的理解,此时在xx项目中使用的caffeine版本应该是在yy项目中配置的3.1.8,但实际结果却是使用的spring-boot-dependencies-2.4.2.pom中定义的2.8.8

这显示不是我们期望的结果!

解决办法

经过实验发现,凡是在<parent>中通过<dependencyManagement>管理的组件版本优先级都比向下依赖的组件版本优先级高。
那么当我们需要修改组件版本以覆盖在<parent>定义的组件版本时,该怎么实现呢?根据不同的情况有2种办法。

版本定义覆盖

这种解决办法适用于:
1.项目的<parent>直接设置为通过<dependencyManagement>管理组件的模块
2.在<parent>中通过<properties>定义了组件版本

示例:
spring-boot-dependencies-2.4.2.pom中通过<dependencyManagement>配置的caffine版本为2.8.8

<properties><caffeine.version>2.8.8</caffeine.version>
</properties>
<dependencyManagement><dependencies><dependency><groupId>com.github.ben-manes.caffeine</groupId><artifactId>caffeine</artifactId><version>${caffeine.version}</version></dependency></dependencies>
</dependencyManagement>

xx项目的<parent>设置为spring-boot-starter-parent

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.4.2</version><relativePath />
</parent><properties><caffeine.version>3.1.8</caffeine.version>
</properties>

xx<properties>重新定义<caffeine.version>为新版本号,如:3.1.8,此时将会使用在xx中定义的<caffeine.version>3.1.8)覆盖在spring-boot-dependencies-2.4.2.pom中定义的<caffeine.version>2.8.8),也就达到了更新组件版本的目的。

重新依赖组件

倘若项目的<parent>不是直接设置为通过<dependencyManagement>管理组件的模块,或者在<parent>中没有通过<properties>定义组件版本,那么此时就只能在项目中重新依赖组件及版本了,这样也能达到覆盖在<parent>中定义的组件版本的目的。
场景1:项目的<parent>不是直接设置为通过<dependencyManagement>管理组件的模块

<!-- 没有直接将Spring Boot设置为parent -->
<parent><artifactId>test-java-samples</artifactId><groupId>org.chench.extra</groupId><version>1.0</version>
</parent>
<!-- Spring Boot通过dependencyManagement引入 -->
<dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.4.2</version><type>pom</type><scope>import</scope></dependency></dependencies>
</dependencyManagement>

场景2:在<parent>中未通过<properties>中定义了组件版本

<!-- 在parent中未定义版本property -->
<!-- 自然也就无法通过重新定义property来达到覆盖版本的目的 -->
<dependencyManagement><dependencies><dependency><groupId>com.github.ben-manes.caffeine</groupId><artifactId>caffeine</artifactId><version>2.8.8</version></dependency></dependencies>
</dependencyManagement>

xx项目中重新依赖组件,以达到版本更新的目的。

<dependency><groupId>com.github.ben-manes.caffeine</groupId><artifactId>caffeine</artifactId><version>3.1.8</version>
</dependency>

使用该方式解决的版本更新问题,依靠的是版本优先级的机制。

完毕!

【参考】
记录一次Maven依赖传递,模块之间依赖版本不一致问题
覆盖 Spring Boot 依赖的版本号

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hjln.cn/news/47227.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

c#自定义Winfrom飞机仪表控件-第一个

先看下效果,这是客户定制的飞机仪表控件中的一个。 我们来分析下这个这种控件要怎么开发,开发思路是什么。背景边框,这个你可以用gdi+画,或者让美工做背景图,我这里直接让美工做背景图了,中间部分留黑。刻度盘有左中右下总计5个左侧的刻度盘又有一个放大盘右侧刻度盘也有…

explorer 资源管理器 win10 剪切大量文件 最终99%卡死好几个小时

用Process Explorerthreads栏 可以看到 shcore.dll占用最多 不过光看这个dll没啥用,这个dll有很多功能 再点stack 看调用栈 ntdll.dll!NtReadFile+0x14KERNELBASE.dll!ReadFile+0x73windows.storage.dll!SHGetKnownFolderItem+0x1a6coml2.dll!StgOpenStorageOnILockBytes+0x99…

设计模式-状态模式

状态模式 状态模式也成为状态机模式,是允许对象在内部状态发生改变时改变它的行为。对象看起来好像改变了它的类,属于行为型模式。 角色:上下文角色(Context):定义客户端需要的接口,内部维护一个当前状态实例,并负责具体状态的切换。 抽象状态角色(State):定义该状态…

透视投影矩阵的推导

透视投影矩阵的推导 本文完全 copy 自 透视投影矩阵的推导 - bluebean - 博客园 (cnblogs.com) 只是用 markdown 将公式全部又打了一遍图1: View Frustum Perspective Projection Matrix 的任务就是把位于视锥体内的物体的顶点 (x, y, z) 坐标映射到 [-1, 1] 范围。(如果是 D…

或门实现

或门实现其中有一个为真,就输出高电平感谢使用!本文来自博客园,作者:草履虫1023,转载请注明原文链接:https://www.cnblogs.com/lichenglin1023/p/18257235

异或门

异或门实现异或就是:两个输入相同为0,不同为1感谢使用!本文来自博客园,作者:草履虫1023,转载请注明原文链接:https://www.cnblogs.com/lichenglin1023/p/18257238

Gitlab搭建

目录Gitlab1. 使用docker部署gitlab2. 登录gitlab2.1 修改语言为中文2.3 修改密码3. 用户管理3.1 创建用户4. 配置ssh拉取代码4.1 添加ssh密钥4.2 测试拉取 Gitlab 1. 使用docker部署gitlab [root@master ~]# mkdir -p /data/gitlab/{config,logs,data}编写docker-compose.yaml…

Linux部署Mysql(服务器)

远程服务器:CentOS 7.6 (本地VM也一样)连接:XShell 7 与 Xftp 71. 下载tar包与准备工作查看系统的 glibc 版本:(我的是2.17)rpm -qa | grep glibc  官网下载对应的 Mysql tar包:https://dev.mysql.com/downloads/mysql/ # 查找与mysql相关的软件包并将其从系统中卸载 …