1 jar包冲突场景:不是引用了不同版本的 jar,而是路径相同,类名相同,方法名称相同导致的,解决方案可以通过exclusions来解决:
···/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.3/log4j-slf4j-impl-2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class
···/repository/org/slf4j/slf4j-log4j12/1.6.1/slf4j-log4j12-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class
解决方案:
下面配置可以避免jar包冲突,本项目的pom文件本身配置依赖zookeeper,但是不想依赖 exclusions里面的,就可以做如下配置。
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.3</version>
<exclusions>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
</exclusions>
</dependency>
其中,exclusions 是为了 排除间接依赖,避免jar包冲突。比如a依赖b,b依赖c ,maven的依赖是有传递作用的,但是加了这个配置即可理解为 a依赖b,但是排除了a依赖b依赖的c。
<exclusions>
<exclusion>
如上遇到了jar包冲突,如果a直接依赖c,和a依赖b,b依赖c,和a传递依赖了b里的c,冲突了,怎么办?
不用担心,这个是有优先顺序的,即外层的优先,如果层级一样,那么谁第一个出现谁优先。