在maven项目中解析多个SLF4J绑定

这个问题在SE网站上听起来像一堆类似的问题 ,所以我应该非常详细地提出我的问题。 所以,这是项目的最小pom.xml

   ch.qos.logback logback-classic 1.0.6   org.codehaus.gmaven.runtime gmaven-runtime-1.7 1.3      org.codehaus.mojo exec-maven-plugin 1.2.1  org.shabunc.App     

这是maven生成的依赖树。

mvn dependency:tree -Dverbose -Dincludes=org.slf4j

 [INFO] [dependency:tree {execution: default-cli}] [INFO] org.shabunc:logdebug:jar:1.0-SNAPSHOT [INFO] \- ch.qos.logback:logback-classic:jar:1.0.6:compile [INFO] \- org.slf4j:slf4j-api:jar:1.6.5:compile 

现在,让我们删除排除并再次检查依赖项。 我们会得到:

  [INFO] org.shabunc:logdebug:jar:1.0-SNAPSHOT [INFO] +- ch.qos.logback:logback-classic:jar:1.0.6:compile [INFO] | \- org.slf4j:slf4j-api:jar:1.6.5:compile [INFO] \- org.codehaus.gmaven.runtime:gmaven-runtime-1.7:jar:1.3:compile [INFO] +- (org.slf4j:slf4j-api:jar:1.5.10:compile - omitted for conflict with 1.6.5) [INFO] +- org.codehaus.gmaven.feature:gmaven-feature-support:jar:1.3:compile [INFO] | \- (org.slf4j:slf4j-api:jar:1.5.10:compile - omitted for conflict with 1.6.5) [INFO] \- org.codehaus.gmaven.runtime:gmaven-runtime-support:jar:1.3:compile [INFO] +- (org.slf4j:slf4j-api:jar:1.5.10:compile - omitted for conflict with 1.6.5) [INFO] \- org.sonatype.gshell:gshell-io:jar:2.0:compile [INFO] \- org.sonatype.gossip:gossip:jar:1.0:compile [INFO] \- (org.slf4j:slf4j-api:jar:1.5.8:compile - omitted for conflict with 1.6.5) 

因此,正如我们所看到的,一切都按预期工作,并且冲突的依赖性实际上被排除在外。 但问题是,即使排除依赖关系,我仍然会在编译和调用mvn exec:java时得到以下消息mvn exec:java

 SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/home/shabunc/.m2/repository/ch/qos/logback/logback-classic/1.0.6/logback-classic-1.0.6.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/home/shabunc/.m2/repository/org/sonatype/gossip/gossip/1.0/gossip-1.0.jar!/org/slf4j/impl/StaticLoggerBinder.class] 

问题是:为什么我仍然会看到这个警告,以及在执行过程中只能使一个版本的slf4j可以到达应该做些什么?

您的问题是没有获得SLF4J API的两个副本,它正在获得两个不同的SLF4J实现。 您需要排除Gossip,而不是API。 这意味着:

  org.codehaus.gmaven.runtime gmaven-runtime-1.7 1.3   org.sonatype.gossip gossip    

gshell-io依赖性由gshell-io声明; 希望它实际上不需要Gossip,它只需要一个SLF4J SLF4J,你以Logback的forms提供它。

你需要做的就是添加这样的东西

 compile "org.sonatype.gossip:gossip:1.0" { exclude module:'slf4j-jcl' exclude module:'slf4j-log4j12' } 

我猜您需要使用依赖范围,请参阅: http : //www.mojohaus.org/exec-maven-plugin/java-mojo.html

classpathScope – 定义传递给插件的类路径的范围。 根据您的需要设置编译,测试,运行时或系统。