Maven依赖管理中传递依赖版本未按预期更新问题求助
关于(version managed from 5.3.27)的含义
这个提示是Maven在告诉你:spring-webmvc原本在依赖传递链中(来自spring-boot-starter-web:2.5.15的依赖)的预期版本是5.3.27,但因为项目里有更高优先级的版本规则(来自spring-boot-dependencies:2.4.0的版本管控),实际生效的版本被替换成了5.3.1。
问题原因
你虽然在dependencyManagement里声明了spring-boot-starter-web:2.5.15,但spring-boot-dependencies是一个BOM(物料清单),核心作用就是统一管控所有Spring Boot相关依赖的版本,包括spring-webmvc。spring-boot-dependencies:2.4.0内置的spring-webmvc版本就是5.3.1,且BOM对依赖版本的管控优先级高于单个starter的版本声明——哪怕你把starter的声明放在BOM前面,BOM依然会覆盖其管控范围内的所有依赖版本,导致spring-webmvc还是使用5.3.1。
解决方法
有两种可靠的解决方式,推荐优先选择第二种:
方式一:显式管控
spring-webmvc版本
在dependencyManagement中,把spring-webmvc的版本声明放在spring-boot-dependencies之前,强制指定版本为5.3.27。示例如下:<dependencyManagement> <dependencies> <!-- 显式指定spring-webmvc版本,优先级高于BOM --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.27</version> </dependency> <!-- 你的starter声明 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.5.15</version> </dependency> <!-- 原有的BOM导入 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.4.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>这样Maven会优先使用你显式声明的
spring-webmvc:5.3.27,忽略BOM里的旧版本。方式二:升级
spring-boot-dependencies到2.5.15
直接把spring-boot-dependencies的版本改成2.5.15,整个BOM里的所有依赖版本都会和spring-boot-starter-web:2.5.15匹配,spring-webmvc自然就是5.3.27。这种方式更稳妥,能避免因版本不匹配导致的潜在兼容性问题——Spring Boot的starter和BOM版本保持一致是官方推荐的做法。
内容的提问来源于stack exchange,提问作者user6952691

