10

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?

3 Answers 3

11

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
8

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
        }
    }
}
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.