前言
如题,这属于包含多个日志依赖的问题,话不多说,迅速展示解决方案。
1 问题场景
我的Java项目依赖一个别人写的项目A(不在maven仓库里面),所以我得先在项目A目录下运行mvn install
,如下:
mvn clean package -DskipTests
mvn install:install-file -Dfile=/home/apr/xxx-0.0.1-SNAPSHOT-jar-with-dependencies.jar -DgroupId=xxx -DartifactId=xxx -Dversion=0.0.1-SNAPSHOT-jar-with-dependencies -Dpackaging=jar
然后在我的java项目下的pom.xml导入这个依赖。如下:
<dependency>
<groupId>xxx</groupId>
<artifactId>xxx</artifactId>
<version>0.0.1-SNAPSHOT-jar-with-dependency</version>
</dependency>
这时候出问题了:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/xxx.jar/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/xxx.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
2 解决方案
1)先找原因,即先查看项目A的pom.xml看导入了什么日志依赖:
<!-- Logger -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
再查看我的java项目下的pom.xml导入了什么日志依赖:
<!-- For logging (remove <scope>test</scope>) -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
可以推断是我的项目和项目A的日志依赖冲突了。
2)不应该加入jar-with-dependencies这个jar包。即在项目A目录下运行mvn install
,如下:
mvn clean package -DskipTests
mvn install
3)然后在我的java项目下的pom.xml导入这个依赖(之前的0.0.1-SNAPSHOT-jar-with-dependency 依赖就不要了),加入exclusion项。如下:
<dependency>
<groupId>xxx</groupId>
<artifactId>xxx</artifactId>
<version>0.0.1-SNAPSHOT</version>
<exclusions>
<!-- <exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</exclusion>-->
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
然后再编译我的项目的时候,就不会报错啦。
可以了。
小结
以上。
参考文献
- How to install local jar with dependencies in maven https://stackoverflow.com/questions/39958360/how-to-install-local-jar-with-dependencies-in-maven
- mvn install jar-with-dependencies https://stackoverflow.com/questions/4734623/mvn-install-jar-with-dependencies
- SLF4J: Class path contains multiple SLF4J bindings. 问题原因及解决方法。 https://blog.csdn.net/SilenceCarrot/article/details/80609401
- Maven Class path error multiple SLF4J bindings https://stackoverflow.com/questions/51125553/maven-class-path-error-multiple-slf4j-bindings
- Guide to installing 3rd party JARs https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
or mvn install:install-file -Dfile=<path-to-file> -DpomFile=<path-to-pomfile>
or mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=<path-to-file>
教你如何打包install maven项目。
- SLF4J: Class path contains multiple SLF4J bindings https://stackoverflow.com/questions/14024756/slf4j-class-path-contains-multiple-slf4j-bindings
- SLF4J Warning: Class Path Contains Multiple SLF4J Bindings https://www.baeldung.com/slf4j-classpath-multiple-bindings