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:
1 2 3 4 5 6 | 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:
1 2 3 | 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:
1 | gradle -q dependencies web:dependencies --configuration compile |
* web stands for your module name.
Snap view of the output:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | 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:
1 | mycompany:logback-core: 1.1 . 2 |
(I found logback show’s in other dependencies).
Now we have two options:
- Exclude each logback’s route with-in the indecencies tree
- Use configuration-wide exclusion (the easier way)
So go to your build.gradle and add this:
1 2 3 | 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.
Reference: | How to exclude libraries from all dependencies in Gradle from our JCG partner Idan Fridman at the IdanFridman.com blog. |