JAR包冲突是JAR开发过程中常遇到的问题,近期一个项目过程中,出现了如下的冲突:
org/slf4j/slf4j-log4j12/1.7.25/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]与
ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]冲突。
网上方法大多数是要仔细排查依赖关系,并将多余依赖exclusion,但问题是由于系统依赖jar较多,一一排查,费时费力。
下面给大家提供一种解决办法:
1、程序正常编译后,断开电脑网络。通过工具找出本地maven冲突的jar包,并将其删除,然后重新执行编译、打包动作,这样编译器会提示依赖jar找不到,并提供完整依赖关系,如下图所示
[ERROR] Failed to execute goal on project *****.*****:
Could not resolve dependencies for project ********:jar:1.0:
Failed to collect dependencies at com.hundsun.jrescloud:jrescloud-starter-rpc-def:jar:2.0.17 ->
com.hundsun.jrescloud:jrescloud-rpc-def:jar:2.0.17 ->
com.hundsun.jrescloud:jrescloud-rpc-t2:jar:2.0.17 ->
com.hundsun.jrescloud:jrescloud-rpc-api:jar:2.0.17 ->
com.hundsun.jrescloud:jrescloud-common:jar:2.0.17 ->
org.springframework.boot:spring-boot-starter-aop:jar:1.5.16.RELEASE ->
org.springframework.boot:spring-boot-starter:jar:1.5.16.RELEASE ->
org.springframework.boot:spring-boot-starter-logging:jar:1.5.16.RELEASE ->
ch.qos.logback:logback-classic:jar:1.1.11:
Failed to read artifact descriptor for ch.qos.logback:logback-classic:jar:1.1.11:
Could not transfer artifact ch.qos.logback:logback-classic:pom:1.1.11 from/to *****: Unknown host repos.hundsun.com -> [Help 1]
可以知道,冲突包通过com.hundsun.jrescloud:jrescloud-starter-rpc-def初始依赖,在maven引入该包处,增加exclusion处理
<dependency> <groupId>com.hundsun.jrescloud</groupId> <artifactId>jrescloud-starter-rpc-def</artifactId> <!--排除--> <exclusions> <exclusion> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </exclusion> </exclusions> </dependency>
如果还有冲突,继续按此方法排查。一直到编译通过,然后开启网络,即可。