Maven SL4J多重绑定,之前的解决方案失败

时间:2018-05-25 12:19:47

标签: apache-spark maven-2 slf4j

我有一个包含多个SLF4J绑定的项目。 我已阅读并尝试this SO postthis other SO post中的解决方案 和slf4j website

我在运行代码时看到的是

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依赖:树中我没有看到这个依赖的排除jar,只有Spark jar,即。

[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的多重绑定?

2 个答案:

答案 0 :(得分:1)

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

我建议您查看maven shade插件来包装你的jar。然后,您可以使用过滤器从coat-libs using a filter中排除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>

答案 1 :(得分:0)

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