Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore a specific transitive dependency from all dependencies in Gradle

How can I ignore a specific transitive dependency in Gradle?

For example, many libraries (such as Spring and ...) depend on commons-logging, I want to replace commons-logging with SLF4J (and its jcl-over-slf4j bridge).

Is it any way in my gradle script to mention it once, and not for each dependency which depends on commons-logging?

I was thinking of an script, iterating on all dependencies and adding some exclude on all of them, is there any better solution? And how that script be like?

like image 226
Amir Pashazadeh Avatar asked Nov 14 '13 23:11

Amir Pashazadeh


People also ask

How do you avoid transitive dependencies in Gradle?

You can tell Gradle to disable transitive dependency management for a dependency by setting ModuleDependency.

How do you handle transitive dependencies in Gradle?

Transitive dependency Releases of a module hosted on a repository can provide metadata to declare those transitive dependencies. By default, Gradle resolves transitive dependencies automatically. The version selection for transitive dependencies can be influenced by declaring dependency constraints.

How do I exclude a specific version of a dependency in Maven?

Multiple transitive dependencies can be excluded by using the <exclusion> tag for each of the dependency you want to exclude and placing all these exclusion tags inside the <exclusions> tag in pom. xml. You will need to mention the group id and artifact id of the dependency you wish to exclude in the exclusion tag.


2 Answers

configurations {
    compile.exclude group: 'commons-logging'
}
like image 132
Ori Dar Avatar answered Oct 10 '22 06:10

Ori Dar


Came here with the same problem but ended up using the following to do an actual replace. Posting it for completeness' sake.

configurations.all {
    resolutionStrategy.eachDependency {
        if(it.requested.name == 'commons-logging') {
            it.useTarget 'org.slf4j:jcl-over-slf4j:1.7.7'
        }
    }
}
like image 24
user2543253 Avatar answered Oct 10 '22 08:10

user2543253