Maven 解决依赖冲突问题

依赖冲突

最常遇到的一个冲突

日志相关jar包的冲突问题

1
2
3
4
5
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/lilixin/IdeaProjects/coupon_leeco/coupon_new_oa/target/coupon_new_oa-0.0.1-SNAPSHOT/WEB-INF/lib/log4j-slf4j-impl-2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/lilixin/IdeaProjects/coupon_leeco/coupon_new_oa/target/coupon_new_oa-0.0.1-SNAPSHOT/WEB-INF/lib/slf4j-log4j12-1.6.6.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory

解决流程

首先确定多个jar包来源

到项目目录执行

1
mvn dependency:tree
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building coupon_new_oa 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ coupon_new_oa ---
[INFO] com.bybon.coupon:coupon_new_oa:war:0.0.1-SNAPSHOT
[INFO] +- org.springframework.data:spring-data-redis:jar:1.0.2.RELEASE:compile
[INFO] | +- log4j:log4j:jar:1.2.17:runtime
[INFO] | \- org.slf4j:slf4j-log4j12:jar:1.6.6:runtime
[INFO] +- org.springframework:spring-context:jar:4.1.5.RELEASE:compile
[INFO] | \- org.springframework:spring-expression:jar:4.1.5.RELEASE:compile
[INFO] +- org.springframework:spring-context-support:jar:4.1.5.RELEASE:compile
[INFO] +- org.springframework:spring-aop:jar:4.1.5.RELEASE:compile
[INFO] | \- aopalliance:aopalliance:jar:1.0:compile
[INFO] +- org.springframework:spring-web:jar:4.1.5.RELEASE:compile
[INFO] +- org.springframework:spring-webmvc:jar:4.1.5.RELEASE:compile
[INFO] +- org.springframework:spring-beans:jar:4.1.5.RELEASE:compile
[INFO] +- org.springframework:spring-core:jar:4.1.5.RELEASE:compile
[INFO] +- org.springframework:spring-tx:jar:4.1.5.RELEASE:compile
[INFO] +- org.springframework:spring-orm:jar:4.1.5.RELEASE:compile
[INFO] | \- org.springframework:spring-jdbc:jar:4.1.5.RELEASE:compile
[INFO] +- org.springframework:spring-aspects:jar:4.1.5.RELEASE:compile
[INFO] +- org.springframework:spring-test:jar:4.1.5.RELEASE:test
[INFO] +- cglib:cglib-nodep:jar:3.1:compile
[INFO] +- org.aspectj:aspectjrt:jar:1.8.5:compile
[INFO] +- org.aspectj:aspectjweaver:jar:1.8.5:compile
[INFO] +- mysql:mysql-connector-java:jar:5.1.34:compile
[INFO] +- org.mybatis:mybatis:jar:3.2.8:compile
[INFO] +- org.mybatis:mybatis-spring:jar:1.2.2:compile
[INFO] +- com.alibaba:druid:jar:0.2.9:compile
[INFO] +- javax.servlet:javax.servlet-api:jar:3.1.0:provided
[INFO] +- com.alibaba:fastjson:jar:1.2.4:compile
[INFO] +- org.slf4j:slf4j-api:jar:1.7.10:compile
[INFO] +- org.apache.logging.log4j:log4j-slf4j-impl:jar:2.3:compile
[INFO] | \- org.apache.logging.log4j:log4j-api:jar:2.3:compile
[INFO] +- org.apache.logging.log4j:log4j-core:jar:2.3:compile
[INFO] +- org.slf4j:jcl-over-slf4j:jar:1.7.12:compile

找到冲突的两个jar包来源

1
2
slf4j-log4j12:jar:1.6.6 来自于spring-data-redis
log4j-slf4j-impl:jar:2.3 为主动引入

在pom.xml中去除一个 我们这里去除spring-data-redis里引入的

1
2
3
4
5
6
7
8
9
10
11
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.0.2.RELEASE</version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>

然后再编译打包就解决此冲突了,其它冲突同理

其它

还可以使用mvn dependency:analyze来分析你引入的依赖

1
2
3
4
5
6
7
8
9
10
11
[INFO] --- maven-dependency-plugin:2.8:analyze (default-cli) @ coupon_new_oa ---
[WARNING] Used undeclared dependencies found:
[WARNING] org.springframework:spring-jdbc:jar:4.1.5.RELEASE:compile
[WARNING] org.apache.httpcomponents:httpcore:jar:4.4:compile
[WARNING] Unused declared dependencies found:
[WARNING] org.springframework.data:spring-data-redis:jar:1.0.2.RELEASE:compile
[WARNING] org.springframework:spring-context-support:jar:4.1.5.RELEASE:compile
[WARNING] org.springframework:spring-aop:jar:4.1.5.RELEASE:compile
[WARNING] org.springframework:spring-orm:jar:4.1.5.RELEASE:compile
[WARNING] org.springframework:spring-aspects:jar:4.1.5.RELEASE:compile
[WARNING] org.springframework:spring-test:jar:4.1.5.RELEASE:test

上面会列出使用的但未声明的和未使用的但声明了的依赖,根据提示册子多余的依赖,但实际开发中发我发现很多包实际上使用了 所以删除时要小心

关注我的微信,共同分享与讨论!