SpringBoot多模块引入自定义Maven BOM报依赖版本缺失问题
问题原因
Maven通过<scope>import</scope>引入BOM类型依赖时,只会读取被导入POM中<dependencyManagement>节点下声明的依赖版本规则,普通<dependencies>节点下的依赖不会被纳入版本管理池。你的配置存在3个核心问题:
- 统一管理依赖的DA项目把gson、junit的版本声明放在了普通
<dependencies>节点下,导入后版本规则不生效 - 父项目ADef中导入DA依赖的
<dependency>标签未正确闭合,XML解析失败会导致依赖管理配置无法正常加载 - 子模块坐标配置错误,groupId与artifactId写反、和父项目artifactId重名,会引发额外构建异常
修复步骤
- 第一步:修改DA依赖管理项目的POM,将所有需要统一管控版本的依赖移到
<dependencyManagement>节点下,BOM为pom打包类型,本身不需要主动引入这些依赖,仅做版本声明即可,修正后的DA核心配置如下:
<groupId>Dependency</groupId> <artifactId>DA</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <dependencyManagement> <dependencies> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <!-- 注意替换为实际存在的正式版本,示例中的1.2.3为虚构版本无法拉取 --> <version>2.10.1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <!-- 注意替换为实际存在的正式版本 --> <version>4.13.2</version> </dependency> </dependencies> </dependencyManagement>
修改完成后在DA项目根目录执行mvn clean install,将修正后的BOM安装到本地Maven仓库。
- 第二步:修正父项目ADef的POM配置,闭合DA依赖的标签,非全局通用的依赖不要放在父项目的普通
<dependencies>节点下,避免所有子模块强制引入不需要的依赖,修正后的核心配置如下:
<groupId>A</groupId> <artifactId>ADef</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>A1</module> <module>A2</module> </modules> <dependencyManagement> <dependencies> <dependency> <groupId>Dependency</groupId> <artifactId>DA</artifactId> <version>0.0.1-SNAPSHOT</version> <type>pom</type> <scope>import</scope> </dependency> <!-- 修正原配置中标签未闭合的问题 --> </dependencies> </dependencyManagement> <!-- 父项目如果不需要全局引入gson、junit,直接删除该节点下的对应依赖即可 -->
- 第三步:修正子模块的POM配置,子模块的artifactId为自身模块名,groupId直接继承父项目即可,不需要单独声明,修正后的A1子模块核心配置如下:
<parent> <groupId>A</groupId> <artifactId>ADef</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <!-- 修正原配置中groupId、artifactId写反的问题 --> <artifactId>A1</artifactId> <dependencies> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> </dependencies>
验证
所有配置修改完成后,回到ADef项目根目录执行mvn clean compile,之前的依赖版本缺失报错会消失,项目可正常构建。
内容的提问来源于stack exchange,提问作者santro
相关产品推荐
相关产品推荐

