参考答案:
Maven依赖冲突通常是由于项目中不同依赖的版本间存在传递依赖造成的。解决方法如下:
使用mvn dependency:tree
命令
依赖范围控制
<scope>
标签控制依赖的范围(例如compile
、provided
、runtime
、test
等),避免不必要的传递依赖。排除传递依赖
<exclusions>
排除冲突的传递依赖。例如:
1<dependency> 2 <groupId>org.springframework</groupId> 3 <artifactId>spring-core</artifactId> 4 <version>5.3.8</version> 5 <exclusions> 6 <exclusion> 7 <groupId>org.example</groupId> 8 <artifactId>example-dependency</artifactId> 9 </exclusion> 10 </exclusions> 11</dependency>
依赖重定向
<dependencyManagement>
统一管理依赖的版本,确保项目依赖的版本一致。例如:
1<dependencyManagement> 2 <dependencies> 3 <dependency> 4 <groupId>org.example</groupId> 5 <artifactId>example-dependency</artifactId> 6 <version>1.2.3</version> 7 </dependency> 8 </dependencies> 9</dependencyManagement>
强制指定依赖版本
pom.xml
中直接声明冲突依赖的版本,确保优先使用项目声明的版本。升级依赖版本
模块隔离
最近更新时间:2024-12-24