Maven SL4J多次绑定,以前的解决方案失败了



我有一个有多个SLF4J绑定的项目。 我已经阅读并尝试了这个SO帖子中的解决方案,这个其他SO帖子 和SLF4J网站。

当我运行代码时,我看到的是

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/Mike/.m2/repository/org/jlab/coat/coat-libs/5.1-SNAPSHOT/coat-libs-5.1-SNAPSHOT.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/Mike/.m2/repository/org/slf4j/slf4j-log4j12/1.7.16/slf4j-log4j12-1.7.16.jar!/org/slf4j/impl/StaticLoggerBinder.class]

但是在我的pom.xml文件中,我已经有

<dependency>
<groupId>org.jlab.coat</groupId>
<artifactId>coat-libs</artifactId>
<version>5.1-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>

在 mvn 依赖项:树中,我没有看到排除罐的这种依赖关系,只看到 Spark 罐子的依赖关系,即

[INFO] com.IKP:DCdatabase:jar:0.0.1-SNAPSHOT
[INFO] +- org.jlab.coat:coat-libs:jar:5.1-SNAPSHOT:compile
[INFO] +- org.apache.spark:spark-core_2.11:jar:2.2.1:compile
...
...
...
[INFO] |  +- org.slf4j:slf4j-api:jar:1.7.16:compile
[INFO] |  +- org.slf4j:jul-to-slf4j:jar:1.7.16:compile
[INFO] |  +- org.slf4j:jcl-over-slf4j:jar:1.7.16:compile
[INFO] |  +- log4j:log4j:jar:1.2.17:compile
[INFO] |  +- org.slf4j:slf4j-log4j12:jar:1.7.16:compile

我还尝试了更多步骤,例如清理我的 .m2 目录,并从头开始构建所有源代码,但我仍然看到这种双重绑定。

发生的细微差别是 Spark 的日志抑制确实发生了,即

Logger.getLogger("org.apache.spark.SparkContext").setLevel(Level.WARN);
Logger.getLogger("org").setLevel(Level.OFF);
Logger.getLogger("akka").setLevel(Level.OFF);

不再将级别设置为关闭,我看到所有级别。

有没有另一种方法可以删除SLF4J的多重绑定?

在我看来,coat-libs已被打包为超级罐子,这就是为什么 slf4j 在您执行mvn dependency:tree时不会显示为依赖项的原因。这就解释了为什么您的排除不起作用。

我建议看看用于包装罐子的 maven 阴影插件。然后,您可以使用过滤器通过过滤器从 coat-libs 中排除 slf4j 依赖项。

例如,可能像这样:

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>org.jlab.coat:coat-libs</artifact>
<excludes>
<exclude>org/slf4j/**</exclude>
</excludes>
</filter>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

就像消息所说的那样,您从coat-libs-5.1-SNAPSHOT.jar获得一个绑定,并在slf4j-log4j12-1.7.16.jar中获得另一个绑定。这并不是说"coat-libs"试图引入具有绑定的依赖项,它是一个试图处理SLF4J日志记录的日志记录绑定。您只能使用一个日志记录绑定,因此您需要删除 coat-libs 的使用,或者您需要从 spark-core 的依赖项中排除 slf4j-log4j12,具体取决于您实际尝试使用的日志记录框架。

最新更新