类路径包含与Gradle的多个SLF4J绑定

时间:2014-06-26 22:03:54

标签: unit-testing gradle slf4j

构建Gradle项目时出现以下错误:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/qliu/.gradle/caches/modules-2/files-2.1/ch.qos.logback/logback-classic/1.0.6/ba738848da3e6fffa0107771c75546eb6d407f3c/logback-classic-1.0.6.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/qliu/.gradle/caches/modules-2/files-2.1/uk.org.lidalia/slf4j-test/1.1.0/f4f523049e041dea673bd421d7b0d61fb5e49548/slf4j-test-1.1.0.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.selector.DefaultContextSelector]

我的Gradle构建文件:

dependencies {
        // for output logger messages
        compile group: 'log4j', name: 'log4j', version: '1.2.17'

        // for testing outputed logger messages
        compile group: 'uk.org.lidalia', name: 'slf4j-test', version: '1.1.0'
}

1 个答案:

答案 0 :(得分:3)

您可以尝试通过resolutionStrategy解决此问题,例如:

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        if (details.requested.name == 'log4j') {
            details.useTarget 'log4j:log4j:1.2.+'
        }
    }
}

请参阅文档:http://www.gradle.org/docs/current/dsl/org.gradle.api.artifacts.ResolutionStrategy.html

但如果您阅读SLF4J网站上的说明,则表示它只是一个警告: http://www.slf4j.org/codes.html#multiple_bindings

The warning emitted by SLF4J is just that, a warning. Even when multiple bindings are present, SLF4J will pick one logging framework/implementation and bind with it.