参考答案:
JAR 冲突通常是指项目中引入了多个版本的同一个 JAR 包,导致类加载异常或运行时错误。解决方法如下:
1mvn dependency:tree
1<dependency> 2 <groupId>org.example</groupId> 3 <artifactId>example-library</artifactId> 4 <version>1.0.0</version> 5 <exclusions> 6 <exclusion> 7 <groupId>org.example</groupId> 8 <artifactId>conflict-jar</artifactId> 9 </exclusion> 10 </exclusions> 11</dependency>
<dependencyManagement>
统一版本dependencyManagement
来指定统一的依赖版本,确保所有模块使用同一个版本:
1<dependencyManagement> 2 <dependencies> 3 <dependency> 4 <groupId>org.example</groupId> 5 <artifactId>conflict-jar</artifactId> 6 <version>2.0.0</version> 7 </dependency> 8 </dependencies> 9</dependencyManagement>
pom.xml
中直接声明冲突 JAR 包的版本,优先使用声明的版本:
1<dependency> 2 <groupId>org.example</groupId> 3 <artifactId>conflict-jar</artifactId> 4 <version>2.0.0</version> 5</dependency>
<scope>
标签控制依赖的作用域,避免不必要的依赖传播。例如:
1<dependency> 2 <groupId>org.example</groupId> 3 <artifactId>conflict-jar</artifactId> 4 <version>2.0.0</version> 5 <scope>provided</scope> 6</dependency>
1mvn clean install -U
maven-enforcer-plugin
来强制检查依赖冲突,找出重复依赖:
1<plugin> 2 <groupId>org.apache.maven.plugins</groupId> 3 <artifactId>maven-enforcer-plugin</artifactId> 4 <version>3.0.0</version> 5 <executions> 6 <execution> 7 <id>enforce</id> 8 <goals> 9 <goal>enforce</goal> 10 </goals> 11 <configuration> 12 <rules> 13 <dependencyConvergence /> 14 </rules> 15 </configuration> 16 </execution> 17 </executions> 18</plugin>
最近更新时间:2024-12-24