Core Java

How to exclude libraries from all dependencies in Gradle

I am using Spring boot. Spring boot by default comes with logback. I wanted to use log4j (for whatever reasons..)

In order to do that I had to exclude logback and add new log4j dependencies:

Logback is “hidden” inside this package:

compile("org.springframework.boot:spring-boot-starter:$project.ext.springBootVersion")
 {
 exclude module: 'org.springframework.boot:spring-boot-starter-logging'
}

compile("org.springframework.boot:spring-boot-starter-log4j:$project.ext.springBatchVersion")

Now when you try to run app you get this Exception:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/dev/caches/modules-2/files-2.1/org.slf4j/slf4j-log4j12/1.7.10/b3eeae7d1765f988a1f45ea81517191315c69c9e/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/dev/caches/modules-2/files-2.1/ch.qos.logback/logback-classic/1.1.2/b316e9737eea25e9ddd6d88eaeee76878045c6b2/logback-classic-1.1.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]

Now we have to look at Gradle’s dependencies tree to see where logback is hidden in order to eliminate it.

Simple command to see gradle’s dependencies tree:

gradle -q dependencies web:dependencies --configuration compile

* web stands for your module name.

Snap view of the output:

Project :web - web
 ------------------------------------------------------------

 compile - Compile classpath for source set 'main'.
 +--- org.springframework.boot:spring-boot-starter-actuator:1.2.2.RELEASE
 | +--- org.springframework.boot:spring-boot-starter:1.2.2.RELEASE
 | | +--- org.springframework.boot:spring-boot:1.2.2.RELEASE
 | | | +--- org.springframework:spring-core:4.1.5.RELEASE
 | | | | \--- commons-logging:commons-logging:1.2
 | | | \--- org.springframework:spring-context:4.1.5.RELEASE
 | | | +--- org.springframework:spring-aop:4.1.5.RELEASE
 | | | | +--- aopalliance:aopalliance:1.0
 | | | | +--- org.springframework:spring-beans:4.1.5.RELEASE
 | | | | | \--- org.springframework:spring-core:4.1.5.RELEASE (*)
 | | | | \--- org.springframework:spring-core:4.1.5.RELEASE (*)
 | | | +--- org.springframework:spring-beans:4.1.5.RELEASE (*)
 | | | +--- org.springframework:spring-core:4.1.5.RELEASE (*)
 | | | \--- org.springframework:spring-expression:4.1.5.RELEASE
 | | | \--- org.springframework:spring-core:4.1.5.RELEASE (*)
 | | +--- org.springframework.boot:spring-boot-autoconfigure:1.2.2.RELEASE
 | | | +--- org.springframework.boot:spring-boot:1.2.2.RELEASE (*)
 | | | \--- org.yaml:snakeyaml:1.14
 | | +--- org.springframework.boot:spring-boot-starter-logging:1.2.2.RELEASE
 | | | +--- org.slf4j:jcl-over-slf4j:1.7.10
 | | | | \--- org.slf4j:slf4j-api:1.7.10
 | | | +--- org.slf4j:jul-to-slf4j:1.7.10
 | | | | \--- org.slf4j:slf4j-api:1.7.10
 | | | +--- org.slf4j:log4j-over-slf4j:1.7.10
 | | | | \--- org.slf4j:slf4j-api:1.7.10
 | | | \--- mycompany:logback-classic:1.1.2
 | | | +--- mycompany:logback-core:1.1.2
 | | | \--- org.slf4j:slf4j-api:1.7.6 -> 1.7.10
 | | +--- org.springframework:spring-core:4.1.5.RELEASE (*)
 | | \--- org.yaml:snakeyaml:1.14
 | +--- org.springframework.boot:spring-boot-actuator:1.2.2.RELEASE

We can find one of the instances of logback popping up from one of our dependencies:

mycompany:logback-core:1.1.2

(I found logback show’s  in other dependencies).

Now we have two options:

  1. Exclude each logback’s route with-in the indecencies tree
  2. Use configuration-wide exclusion (the easier way)

So go to your build.gradle and add this:

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

Thats it. Your nightmare is over. If you check the dependencies tree again you wont see any existence of logback anymore.

Idan Fridman

Idan is Software engineer with experience in Server side technologies. Idan is responsible for various infrastructure models in the software industry(Telecommunications, Finance).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button