'Exclude transitive dependency of Gradle plugin
Excluding a transitive dependency in Gradle is pretty straightforward:
compile('com.example.m:m:1.0') {
exclude group: 'org.unwanted', module: 'x'
}
How would we go around he situation in which we use a plugin:
apply: "somePlugin"
And when getting the dependencies we realize that the plugin is bringing some transitive dependencies of its own?
Solution 1:[1]
You can manipulate the classpath of the buildscript itself through:
buildscript {
configurations {
classpath {
exclude group: 'org', module: 'foo' // For a global exclude
}
}
dependencies {
classpath('org:bar:1.0') {
exclude group: 'org', module: 'baz' // For excluding baz from bar but not if brought elsewhere
}
}
}
Solution 2:[2]
You can remove dependencies after the plugin is applied, (from a single configuration, or to all configurations) using eg. compile.exclude
. Note that compile
resolves to a "Configuration"; see the javadocs at Configuration.exclude .
edit
Be aware that excluding dependecies could fail, if the configuration has already been resolved.
Sample script
apply plugin: 'java-library'
repositories {
jcenter()
}
dependencies {
compile 'junit:junit:4.12'
compile 'ant:ant:1.6'
compile 'org.apache.commons:commons-lang3:3.8'
}
// remove dependencies
configurations.all {
exclude group:'junit', module:'junit'
}
configurations.compile {
exclude group:'org.apache.commons', module: 'commons-lang3'
}
println 'compile deps:\n' + configurations.compile.asPath
Solution 3:[3]
Here is another way to enforce your project to strictly use a specific version for the build.gradle.kts
val grpcVersion = "1.45.1"
implementation("io.grpc:grpc-stub") {
version {
strictly(grpcVersion)
}
}
More info can be found at the gradle documentation: https://docs.gradle.org/current/userguide/dependency_downgrade_and_exclude.html
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Louis Jacomet |
Solution 2 | |
Solution 3 | bluecheese |