繁体   English   中英

如何在构建时防止SLF4J声名狼藉的“多重绑定”警告?

[英]How can I prevent SLF4J's infamous “multiple bindings” warning at build time?

我的程序输出以下令人讨厌的消息:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/scratch/events-beware/events-beware/build/install/events-beware/lib/logback-classic-1.1.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/scratch/events-beware/events-beware/build/install/events-beware/lib/slf4j-log4j12-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/scratch/events-beware/events-beware/build/install/events-beware/lib/slf4j-simple-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]

不需要的SLF4J绑定是项目的可传递依赖项,因此我将依赖项管理器配置为排除包含不需要的绑定的jar。 这会工作一段时间,直到将新的依赖项添加到项目中,这会引入另一个不需要的绑定...

如果我暂时依赖于多个SLF4J绑定,如何使用构建系统的功能使构建失败?

在构建时,您可以检查每个依赖项,以查找SLF4J绑定。 如果发现多个,则可能导致构建失败。

使用Gradle来做到这一点:

task checkSlf4j {
    description 'Ensure only one SFL4j binding is present in the runtime configuration'

    doLast {
        def bindings = []
        configurations.runtime.each {
            zipTree(it).matching { include 'org/slf4j/impl/StaticLoggerBinder.class' }.each { c ->
                bindings << [it.getName(), c]
            }
        }
        if (bindings.size () > 1) {
            throw new GradleException("Multiple SLF4J bindings found: ${bindings*.getAt(0)}")
        }
    }
}

check.dependsOn checkSlf4j

结果如下:

$ ./gradlew build
:bundleJars UP-TO-DATE
:compileJava UP-TO-DATE
:compileGroovy UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:assemble UP-TO-DATE
:checkSlf4j FAILED

FAILURE: Build failed with an exception.

* Where:
Script '/scratch/events-beware/events-beware/slf4j.gradle' line: 14

* What went wrong:
Execution failed for task ':checkSlf4j'.
> Multiple SLF4J bindings found: [logback-classic-1.1.2.jar, slf4j-log4j12-1.6.1.jar, slf4j-simple-1.6.1.jar]

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 2.009 secs

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM