Maven Jar冲突解决方案

时间:2022-09-21 09:15:37

MavenJar冲突解决方案

记一次在做项目过程中 遇到的jar冲突问题

话不多说 先报出错异常


//异常在这
SLF4J: Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path, preempting *Error. 





SLF4J: See also http://www.slf4j.org/codes.html#log4jDelegationLoop for more details.

//异常在这
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.apache.log4j.LogManager.getLogger(LogManager.java:44)
at org.slf4j.impl.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:66)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:277)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:288)
....

就是说 两个jar包冲突了 ,基于是哪些jar冲突 ,异常已经说得很清楚了

log4j-over-slf4j.jar AND slf4j-log4j12.jar
其他的jar 冲突 可以看冲突的日志
这里以这俩为例

解决方案 :

首先 定位 是哪些依赖 中 有这些 jar包

执行 :mvn dependency:tree >d:/tree.txt

将maven jar 依赖输出到文件中 便于查看

我的是这样的: 具体我都有解释:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building es_hbase 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.10:tree (default-cli) @ es_hbase ---
[INFO] es_hbase:es_hbase:jar:1.0-SNAPSHOT
//elasticsearch的 依赖   没有冲突 
[INFO] +- org.elasticsearch.client:transport:jar:5.6.3:compile
[INFO] |  +- org.elasticsearch:elasticsearch:jar:2.4.4:compile
[INFO] |  |  +- org.apache.lucene:lucene-core:jar:5.5.2:compile
[INFO] |  |  +- org.apache.lucene:lucene-backward-codecs:jar:5.5.2:compile
[INFO] |  |  +- org.apache.lucene:lucene-analyzers-common:jar:5.5.2:compile
[INFO] |  |  +- org.apache.lucene:lucene-queries:jar:5.5.2:compile
[INFO] |  |  +- org.apache.lucene:lucene-memory:jar:5.5.2:compile
[INFO] |  |  +- org.apache.lucene:lucene-highlighter:jar:5.5.2:compile
[INFO] |  |  +- org.apache.lucene:lucene-queryparser:jar:5.5.2:compile
[INFO] |  |  |  \- org.apache.lucene:lucene-sandbox:jar:5.5.2:compile
[INFO] |  |  +- org.apache.lucene:lucene-suggest:jar:5.5.2:compile
[INFO] |  |  |  \- org.apache.lucene:lucene-misc:jar:5.5.2:compile
[INFO] |  |  +- org.apache.lucene:lucene-join:jar:5.5.2:compile
[INFO] |  |  |  \- org.apache.lucene:lucene-grouping:jar:5.5.2:compile
[INFO] |  |  +- org.apache.lucene:lucene-spatial:jar:5.5.2:compile
[INFO] |  |  |  +- org.apache.lucene:lucene-spatial3d:jar:5.5.2:compile
[INFO] |  |  |  \- com.spatial4j:spatial4j:jar:0.5:compile
[INFO] |  |  +- org.elasticsearch:securesm:jar:1.0:compile
[INFO] |  |  +- com.carrotsearch:hppc:jar:0.7.1:compile
[INFO] |  |  +- joda-time:joda-time:jar:2.9.7:compile
[INFO] |  |  +- com.fasterxml.jackson.dataformat:jackson-dataformat-smile:jar:2.8.7:compile
[INFO] |  |  +- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:jar:2.8.7:compile
[INFO] |  |  +- com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:jar:2.8.7:compile
[INFO] |  |  +- org.yaml:snakeyaml:jar:1.17:compile
[INFO] |  |  +- io.netty:netty:jar:3.10.6.Final:compile
[INFO] |  |  +- com.ning:compress-lzf:jar:1.0.2:compile
[INFO] |  |  +- com.tdunning:t-digest:jar:3.0:compile
[INFO] |  |  +- org.hdrhistogram:HdrHistogram:jar:2.1.6:compile
[INFO] |  |  \- com.twitter:jsr166e:jar:1.1.0:compile
[INFO] |  +- org.elasticsearch.plugin:transport-netty3-client:jar:5.6.3:compile
[INFO] |  +- org.elasticsearch.plugin:transport-netty4-client:jar:5.6.3:compile
[INFO] |  |  +- io.netty:netty-buffer:jar:4.1.13.Final:compile
[INFO] |  |  +- io.netty:netty-codec:jar:4.1.13.Final:compile
[INFO] |  |  +- io.netty:netty-codec-http:jar:4.1.13.Final:compile
[INFO] |  |  +- io.netty:netty-common:jar:4.1.13.Final:compile
[INFO] |  |  +- io.netty:netty-handler:jar:4.1.13.Final:compile
[INFO] |  |  +- io.netty:netty-resolver:jar:4.1.13.Final:compile
[INFO] |  |  \- io.netty:netty-transport:jar:4.1.13.Final:compile
[INFO] |  +- org.elasticsearch.plugin:reindex-client:jar:5.6.3:compile
[INFO] |  |  \- org.elasticsearch.client:elasticsearch-rest-client:jar:5.6.3:compile
[INFO] |  |     +- org.apache.httpcomponents:httpcore:jar:4.4.6:compile
[INFO] |  |     +- org.apache.httpcomponents:httpasyncclient:jar:4.1.3:compile
[INFO] |  |     \- org.apache.httpcomponents:httpcore-nio:jar:4.4.5:compile
[INFO] |  +- org.elasticsearch.plugin:lang-mustache-client:jar:5.6.3:compile
[INFO] |  |  \- com.github.spullara.mustache.java:compiler:jar:0.9.3:compile
[INFO] |  +- org.elasticsearch.plugin:percolator-client:jar:5.6.3:compile
[INFO] |  \- org.elasticsearch.plugin:parent-join-client:jar:5.6.3:compile


//logging依赖 没有冲突 
[INFO] +- org.apache.logging.log4j:log4j-core:jar:2.8.2:compile
[INFO] |  \- org.apache.logging.log4j:log4j-api:jar:2.7:compile

//json依赖 没有冲突 
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.1:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.8.0:compile
[INFO] |  \- com.fasterxml.jackson.core:jackson-core:jar:2.8.7:compile



//hbase依赖  有冲突    具体冲突看下面
[INFO] +- org.apache.hbase:hbase-client:jar:0.99.2:compile
[INFO] |  +- org.apache.hbase:hbase-annotations:jar:0.99.2:compile
[INFO] |  |  \- jdk.tools:jdk.tools:jar:1.7:system
[INFO] |  +- org.apache.hbase:hbase-common:jar:0.99.2:compile
[INFO] |  +- org.apache.hbase:hbase-protocol:jar:0.99.2:compile
[INFO] |  +- commons-codec:commons-codec:jar:1.10:compile
[INFO] |  +- commons-io:commons-io:jar:2.4:compile
[INFO] |  +- commons-lang:commons-lang:jar:2.6:compile
[INFO] |  +- commons-logging:commons-logging:jar:1.2:compile
[INFO] |  +- com.google.guava:guava:jar:12.0.1:compile
[INFO] |  |  \- com.google.code.findbugs:jsr305:jar:1.3.9:compile
[INFO] |  +- com.google.protobuf:protobuf-java:jar:2.5.0:compile
[INFO] |  +- io.netty:netty-all:jar:4.0.23.Final:compile
[INFO] |  +- org.apache.zookeeper:zookeeper:jar:3.4.6:compile

                //在这里 
[INFO] |  |  \- org.slf4j:slf4j-api:jar:1.7.24:compile
[INFO] |  +- org.htrace:htrace-core:jar:3.0.4:compile
[INFO] |  +- org.codehaus.jackson:jackson-mapper-asl:jar:1.8.8:compile
[INFO] |  +- org.jruby.joni:joni:jar:2.1.2:compile
[INFO] |  |  \- org.jruby.jcodings:jcodings:jar:1.0.8:compile
[INFO] |  +- org.apache.hadoop:hadoop-common:jar:2.5.1:compile
[INFO] |  |  +- org.apache.commons:commons-math3:jar:3.1.1:compile
[INFO] |  |  +- xmlenc:xmlenc:jar:0.52:compile
[INFO] |  |  +- commons-net:commons-net:jar:3.1:compile
[INFO] |  |  +- commons-el:commons-el:jar:1.0:compile
[INFO] |  |  +- net.java.dev.jets3t:jets3t:jar:0.9.0:compile
[INFO] |  |  |  \- com.jamesmurty.utils:java-xmlbuilder:jar:0.4:compile
[INFO] |  |  +- commons-configuration:commons-configuration:jar:1.6:compile
[INFO] |  |  |  +- commons-digester:commons-digester:jar:2.1:compile
[INFO] |  |  |  |  \- commons-beanutils:commons-beanutils:jar:1.9.3:compile
[INFO] |  |  |  \- commons-beanutils:commons-beanutils-core:jar:1.8.0:compile
[INFO] |  |  +- org.apache.avro:avro:jar:1.7.4:compile
[INFO] |  |  |  +- com.thoughtworks.paranamer:paranamer:jar:2.3:compile
[INFO] |  |  |  \- org.xerial.snappy:snappy-java:jar:1.0.4.1:compile
[INFO] |  |  +- com.jcraft:jsch:jar:0.1.42:compile
[INFO] |  |  \- org.apache.commons:commons-compress:jar:1.4.1:compile
[INFO] |  |     \- org.tukaani:xz:jar:1.0:compile
[INFO] |  +- org.apache.hadoop:hadoop-auth:jar:2.5.1:compile
[INFO] |  |  +- org.apache.httpcomponents:httpclient:jar:4.5.3:compile
[INFO] |  |  \- org.apache.directory.server:apacheds-kerberos-codec:jar:2.0.0-M15:compile
[INFO] |  |     +- org.apache.directory.server:apacheds-i18n:jar:2.0.0-M15:compile
[INFO] |  |     +- org.apache.directory.api:api-asn1-api:jar:1.0.0-M20:compile
[INFO] |  |     \- org.apache.directory.api:api-util:jar:1.0.0-M20:compile
[INFO] |  +- org.apache.hadoop:hadoop-mapreduce-client-core:jar:2.5.1:compile
[INFO] |  |  +- org.apache.hadoop:hadoop-yarn-common:jar:2.5.1:compile
[INFO] |  |  |  +- com.google.inject:guice:jar:3.0:compile
[INFO] |  |  |  |  +- javax.inject:javax.inject:jar:1:compile
[INFO] |  |  |  |  \- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  |  |  \- com.sun.jersey.contribs:jersey-guice:jar:1.9:compile
[INFO] |  |  \- com.google.inject.extensions:guice-servlet:jar:3.0:compile
[INFO] |  \- com.github.stephenc.findbugs:findbugs-annotations:jar:1.3.9-1:compile



//hbase-server依赖 有冲突   看下面
[INFO] +- org.apache.hbase:hbase-server:jar:0.96.1-hadoop2:compile
[INFO] |  +- org.apache.hbase:hbase-prefix-tree:jar:0.96.1-hadoop2:runtime
[INFO] |  +- commons-httpclient:commons-httpclient:jar:3.1:compile
[INFO] |  +- commons-collections:commons-collections:jar:3.2.2:compile
[INFO] |  +- org.apache.hbase:hbase-hadoop-compat:jar:0.96.1-hadoop2:compile
[INFO] |  +- org.apache.hbase:hbase-hadoop2-compat:jar:0.96.1-hadoop2:compile
[INFO] |  +- com.yammer.metrics:metrics-core:jar:2.1.2:compile
[INFO] |  +- commons-cli:commons-cli:jar:1.2:compile
[INFO] |  +- com.github.stephenc.high-scale-lib:high-scale-lib:jar:1.1.1:compile
[INFO] |  +- org.apache.commons:commons-math:jar:2.2:compile
[INFO] |  +- log4j:log4j:jar:1.2.17:compile
[INFO] |  +- org.mortbay.jetty:jetty:jar:6.1.26:compile
[INFO] |  +- org.mortbay.jetty:jetty-util:jar:6.1.26:compile
[INFO] |  +- org.mortbay.jetty:jetty-sslengine:jar:6.1.26:compile
[INFO] |  +- org.mortbay.jetty:jsp-2.1:jar:6.1.14:compile
[INFO] |  |  \- org.eclipse.jdt:core:jar:3.1.1:compile
[INFO] |  +- org.mortbay.jetty:jsp-api-2.1:jar:6.1.14:compile
[INFO] |  +- org.mortbay.jetty:servlet-api-2.5:jar:6.1.14:compile
[INFO] |  +- org.codehaus.jackson:jackson-core-asl:jar:1.8.8:compile
[INFO] |  +- org.codehaus.jackson:jackson-jaxrs:jar:1.8.8:compile
[INFO] |  +- tomcat:jasper-compiler:jar:5.5.23:runtime
[INFO] |  +- tomcat:jasper-runtime:jar:5.5.23:compile
[INFO] |  +- org.jamon:jamon-runtime:jar:2.3.1:compile
[INFO] |  +- com.sun.jersey:jersey-core:jar:1.8:compile
[INFO] |  +- com.sun.jersey:jersey-json:jar:1.8:compile
[INFO] |  |  +- org.codehaus.jettison:jettison:jar:1.1:compile
[INFO] |  |  +- com.sun.xml.bind:jaxb-impl:jar:2.2.3-1:compile
[INFO] |  |  \- org.codehaus.jackson:jackson-xc:jar:1.7.1:compile
[INFO] |  +- com.sun.jersey:jersey-server:jar:1.8:compile
[INFO] |  |  \- asm:asm:jar:3.1:compile
[INFO] |  +- javax.xml.bind:jaxb-api:jar:2.2.2:compile
[INFO] |  |  \- javax.activation:activation:jar:1.1:compile
[INFO] |  +- stax:stax-api:jar:1.0.1:compile
[INFO] |  +- org.cloudera.htrace:htrace-core:jar:2.01:compile
[INFO] |  +- org.apache.hadoop:hadoop-client:jar:2.2.0:compile
[INFO] |  |  +- org.apache.hadoop:hadoop-mapreduce-client-app:jar:2.2.0:compile
[INFO] |  |  +- org.apache.hadoop:hadoop-yarn-api:jar:2.2.0:compile
[INFO] |  |  \- org.apache.hadoop:hadoop-mapreduce-client-jobclient:jar:2.2.0:compile
[INFO] |  +- org.apache.hadoop:hadoop-mapreduce-client-jobclient:test-jar:tests:2.2.0:compile
[INFO] |  |  +- org.apache.hadoop:hadoop-mapreduce-client-common:jar:2.2.0:compile
[INFO] |  |  |  +- org.apache.hadoop:hadoop-yarn-client:jar:2.2.0:compile
[INFO] |  |  |  |  \- com.sun.jersey.jersey-test-framework:jersey-test-framework-grizzly2:jar:1.9:compile
[INFO] |  |  |  |     +- com.sun.jersey.jersey-test-framework:jersey-test-framework-core:jar:1.9:compile
[INFO] |  |  |  |     |  +- javax.servlet:javax.servlet-api:jar:3.1.0:compile
[INFO] |  |  |  |     |  \- com.sun.jersey:jersey-client:jar:1.9:compile
[INFO] |  |  |  |     \- com.sun.jersey:jersey-grizzly2:jar:1.9:compile
[INFO] |  |  |  |        +- org.glassfish.grizzly:grizzly-http:jar:2.1.2:compile
[INFO] |  |  |  |        |  \- org.glassfish.grizzly:grizzly-framework:jar:2.1.2:compile
[INFO] |  |  |  |        |     \- org.glassfish.gmbal:gmbal-api-only:jar:3.0.0-b023:compile
[INFO] |  |  |  |        |        \- org.glassfish.external:management-api:jar:3.0.0-b012:compile
[INFO] |  |  |  |        +- org.glassfish.grizzly:grizzly-http-server:jar:2.1.2:compile
[INFO] |  |  |  |        |  \- org.glassfish.grizzly:grizzly-rcm:jar:2.1.2:compile
[INFO] |  |  |  |        +- org.glassfish.grizzly:grizzly-http-servlet:jar:2.1.2:compile
[INFO] |  |  |  |        \- org.glassfish:javax.servlet:jar:3.1:compile
[INFO] |  |  |  \- org.apache.hadoop:hadoop-yarn-server-common:jar:2.2.0:compile
[INFO] |  |  +- org.apache.hadoop:hadoop-mapreduce-client-shuffle:jar:2.2.0:compile
[INFO] |  |  |  \- org.apache.hadoop:hadoop-yarn-server-nodemanager:jar:2.2.0:compile

        //又是一个冲突

[INFO] |  |  \- org.slf4j:slf4j-log4j12:jar:1.7.24:compile
[INFO] |  +- org.apache.hadoop:hadoop-hdfs:jar:2.2.0:compile
[INFO] |  |  +- commons-daemon:commons-daemon:jar:1.0.13:compile
[INFO] |  |  +- javax.servlet.jsp:jsp-api:jar:2.1:compile
[INFO] |  |  \- javax.servlet:servlet-api:jar:2.5:compile
[INFO] |  +- org.apache.hadoop:hadoop-hdfs:test-jar:tests:2.2.0:compile
[INFO] |  \- org.apache.hadoop:hadoop-annotations:jar:2.2.0:compile





//SpringBoot 依赖 没冲突
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:1.5.2.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:1.5.2.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot:jar:1.5.2.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-autoconfigure:jar:1.5.2.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:1.5.2.RELEASE:compile
[INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.1.11:compile
[INFO] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.1.11:compile
[INFO] |  |  |  +- org.slf4j:jcl-over-slf4j:jar:1.7.24:compile
[INFO] |  |  |  +- org.slf4j:jul-to-slf4j:jar:1.7.24:compile
[INFO] |  |  |  \- org.slf4j:log4j-over-slf4j:jar:1.7.24:compile
[INFO] |  |  \- org.springframework:spring-core:jar:4.3.7.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.5.2.RELEASE:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.5.11:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.5.11:compile
[INFO] |  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.5.11:compile
[INFO] |  +- org.hibernate:hibernate-validator:jar:5.3.4.Final:compile
[INFO] |  |  +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] |  |  +- org.jboss.logging:jboss-logging:jar:3.3.0.Final:compile
[INFO] |  |  \- com.fasterxml:classmate:jar:1.3.3:compile
[INFO] |  +- org.springframework:spring-web:jar:4.3.7.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-aop:jar:4.3.7.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-beans:jar:4.3.7.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-context:jar:4.3.7.RELEASE:compile
[INFO] |  \- org.springframework:spring-webmvc:jar:4.3.7.RELEASE:compile
[INFO] |     \- org.springframework:spring-expression:jar:4.3.7.RELEASE:compile




[INFO] +- org.springframework.boot:spring-boot-starter-thymeleaf:jar:1.5.2.RELEASE:compile
[INFO] |  +- org.thymeleaf:thymeleaf-spring4:jar:2.1.5.RELEASE:compile
[INFO] |  |  \- org.thymeleaf:thymeleaf:jar:2.1.5.RELEASE:compile
[INFO] |  |     +- ognl:ognl:jar:3.0.8:compile
[INFO] |  |     +- org.javassist:javassist:jar:3.21.0-GA:compile
[INFO] |  |     \- org.unbescape:unbescape:jar:1.1.0.RELEASE:compile
[INFO] |  \- nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:jar:1.4.0:compile
[INFO] |     \- org.codehaus.groovy:groovy:jar:2.4.9:compile
[INFO] \- junit:junit:jar:3.8.1:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.000 s
[INFO] Finished at: 2017-11-16T09:28:02+08:00
[INFO] Final Memory: 31M/362M

冲突的解决方案 maven中排除掉

具体如下 :

  <!--HBase-->
    <dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-client</artifactId>
      <version>0.99.2</version>
                //重点:
      <exclusions>
        <exclusion>
          <groupId>org.slf4j</groupId>
          <artifactId>log4j-over-slf4j</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
      </exclusions>

    </dependency>
    <dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-server</artifactId>
      <version>0.96.1-hadoop2</version>
            //重点:
      <exclusions>
        <exclusion>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
      </exclusions>

    </dependency>

综上 解决问题