Gradle 5's uber-jar puts a second slf4j binding on the classpath and breaks our code

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/me/.gradle/caches/5.4.1/generated-gradle-jars/gradle-api-5.4.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/me/.gradle/caches/modules-2/files-2.1/ch.qos.logback/logback-classic/1.2.3/7c4f3c474fb2c041d8028740440937705ebb473a/logback-classic-1.2.3.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.gradle.internal.logging.slf4j.OutputEventListenerBackedLoggerContext]

java.lang.ClassCastException: org.gradle.internal.logging.slf4j.OutputEventListenerBackedLoggerContext cannot be cast to ch.qos.logback.classic.LoggerContext

at com.foo.bar.logging.LoggerFactory.getOrCreateLogger(LoggerFactory.java:23)

…
at com.foo.bar.logging.LoggingTest.shouldLogErrorInCorrectFormatToResponseFile(LoggingTest.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

The problem is:

SLF4J: Found binding in [jar:file:/Users/me/.gradle/caches/5.4.1/generated-gradle-jars/gradle-api-5.4.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]

But gradle-api-5.4.1 is an uber-jar, with a bunch of stuff in it, over which we have no control, and loading ahead of us on the classpath. There doesn’t seem to be a way to exclude anything.

How do we tell Gradle to not include an slf4j binding in the uber-jar?

This problem doesn’t seem to have anything to do with the Java version being used.

Interestingly, the gradle-api-3.4.1.jar also has slf4j impl, but doesn’t exhibit this behavior.

Turns out this person ran into the same problem.

It’s caused by applying java-gradle-plugin to a non-plugin component that does logging:

1 Like