Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude transitive dependency of Gradle plugin

Tags:

java

gradle

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?

like image 593
Cristina_eGold Avatar asked Jun 02 '19 07:06

Cristina_eGold


People also ask

How do you exclude a transitive dependency in Gradle?

Option 1) per-dependency exclude rules. When you specify a dependency in your build script, you can provide an exclude rule at the same time telling Gradle not to pull in the specified transitive dependency. For example, say we have a Gradle project that depends on Google's Guava library, or more specifically com.

Does Gradle support transitive dependencies?

Gradle automatically resolves those additional modules, so called transitive dependencies. If needed, you can customize the behavior the handling of transitive dependencies to your project's requirements. Projects with tens or hundreds of declared dependencies can easily suffer from dependency hell.


Video Answer


1 Answers

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
like image 51
Daniele Avatar answered Oct 17 '22 16:10

Daniele