简体   繁体   中英

How do you use gradle's ``exclude`` to replace a dependency?

So I want to disable logging for JUnit Tests.

Simplest way would be to switch the binding from slf4j-simple to slf4j-nop .

How do I do that, though?

I tried exclude :

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"

    implementation "com.android.support:appcompat-v7:$support_version"
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'

    implementation "org.jetbrains.anko:anko-common:$anko_version"

    implementation "org.slf4j:slf4j-simple:1.6.1"
    testImplementation("org.slf4j:slf4j-nop:1.6.1"){
        exclude(group:'org.slf4j', module:'slf4j-simple')
    }
    implementation 'io.github.microutils:kotlin-logging:1.6.10'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

But that still results in

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/user1291/.gradle/caches/modules-2/files-2.1/org.slf4j/slf4j-nop/1.6.1/70249094d4e5653b6bdfea46f3a1a4165c1e1993/slf4j-nop-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/user1291/.gradle/caches/modules-2/files-2.1/org.slf4j/slf4j-simple/1.6.1/58e59bfb3e247097b8122243b3bfe0049c8cfae8/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 [org.slf4j.helpers.NOPLoggerFactory]

Also tried configurations :

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"

    implementation "com.android.support:appcompat-v7:$support_version"
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'

    implementation "org.jetbrains.anko:anko-common:$anko_version"

    implementation "org.slf4j:slf4j-simple:1.6.1"
    testImplementation "org.slf4j:slf4j-nop:1.6.1"
    implementation 'io.github.microutils:kotlin-logging:1.6.10'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

configurations{
    testImplementation.exclude(group:'org.slf4j',module:'slf4j-simple')
}

which made no difference.

Try this :

configurations.all { config ->
    config.resolutionStrategy.dependencySubstitution {
        if (config.name.toLowerCase().contains('test')) {
            substitute module('org.slf4j:slf4j-simple:1.6.1') with module('org.slf4j:slf4j-nop:1.6.1')
        }
    }
}

I would go a different route. SLF4J's simple logger will look for simplelogger.properties on your classpath. If you put that in src/test/resources , your test code should find it on the classpath, and you can use that to turn off SLF4J's output.

# src/test/resources/simplelogger.properties
org.slf4j.simpleLogger.defaultLogLevel=off

You can see the full list of configuration properties, as well as a good description of simple logger on its javadoc page .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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