java - Gradle: Excluding transitive dependency not working - Stack Overflow

admin2025-04-18  3

I am using Gradle catalogue and added the following dependency in my build.gradle (groovy)

mockserver-client = { module = "org.mock-server:mockserver-client-java-no-dependencies", version = "5.14.0" }

and add dependency on

 testImplementation(libs.mockserver.client)

However, I get the following error

LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.slf4j.helpers.NOPLoggerFactory loaded from file:/Users/XYZ/.gradle/caches/modules-2/files-2.1/org.mock-server/mockserver-client-java-no-dependencies/5.14.0/14d667cc7a345ccb9e3ccae229bd42f67f48b258/mockserver-client-java-no-dependencies-5.14.0.jar).

I understand that I need to exclude sl4j-api which is transitively imported from mockserver-client-java-no-dependencies. However following produce no difference

    testImplementation(libs.mockserver.client) {
        exclude group: 'org.slf4j', module: 'sl4j-api'
}

Similarly, transitive = false has been useless, too. I can use configurations.configureEach { as I need sl4j-api in other configuration.

How could I exclude the said dependency? Also mockserver-client-java-no-dependencies shows no dependencies in the IntelliJ but is visible in the error message.

I am using Gradle catalogue and added the following dependency in my build.gradle (groovy)

mockserver-client = { module = "org.mock-server:mockserver-client-java-no-dependencies", version = "5.14.0" }

and add dependency on

 testImplementation(libs.mockserver.client)

However, I get the following error

LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.slf4j.helpers.NOPLoggerFactory loaded from file:/Users/XYZ/.gradle/caches/modules-2/files-2.1/org.mock-server/mockserver-client-java-no-dependencies/5.14.0/14d667cc7a345ccb9e3ccae229bd42f67f48b258/mockserver-client-java-no-dependencies-5.14.0.jar).

I understand that I need to exclude sl4j-api which is transitively imported from mockserver-client-java-no-dependencies. However following produce no difference

    testImplementation(libs.mockserver.client) {
        exclude group: 'org.slf4j', module: 'sl4j-api'
}

Similarly, transitive = false has been useless, too. I can use configurations.configureEach { as I need sl4j-api in other configuration.

How could I exclude the said dependency? Also mockserver-client-java-no-dependencies shows no dependencies in the IntelliJ but is visible in the error message.

Share Improve this question asked Jan 30 at 16:08 Mangat Rai ModiMangat Rai Modi 5,7069 gold badges50 silver badges80 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

The error message isn't telling you to remove sl4j-api but either Logback or SLF4J NOP.

And your Mockserver dependency has no dependencies of its own, so excluding a transitive dependency from that dependency can't be right.

You want to exclude a given dependency in the whole configuration testImplementation.

You can exclude Logback if you don't want logging:

configurations {
    testImplementation {
        exclude group: 'ch.qos.logback'
    }
}

Or SLF4J NOP if you want logging:

configurations {
    testImplementation {
        exclude group: 'org.slf4j', module: 'slf4j-nop'
    }
}

So I figured the following is happening

  1. org.mock-server:mockserver-client-java-no-dependencies declares no dependency but packages them all together in a fat jar.
  2. Gradle is not aware of the dependencies in the jar and hence can't ignore it.

Solution Import org.mock-server:mockserver-client-java instead and use transitive=false

转载请注明原文地址:http://anycun.com/QandA/1744907015a89306.html