How to exclude some transitive dependency artifacts (only artifacts, not the whole module)

(Stephane Galles) #1

In a huge transitive dependency tree, one module brings an artifact that I do not want. I would like to exclude this artifact in this dependant module

In the documentation of the DependencyHandler ( we found :

exclude module: 'cglib' //by artifact name

But I guess that what is really meant is ‘by module name’, too bad :).

But basically that’s what I’m trying to do, something like that (I’m making this up) :

exclude module: 'foo', artifactName: 'bar'

Meaning, I will get all artifacts of module foo, expect artifact ‘bar’ ( bar.ear, for exemple)

Using ‘configuration’ in the dependency is not an option because the publication of this module does not allow me to filter by configuration

I could explicitly declare the transitive dependency and use the ‘artifact only’ notation in order to filter the artifacts of the dependency, but in this case I will have to explicitly declare the version number of the transitive dependency (and I don’t really want to maintain the version in the file in sync with the version declared in the other modules)

So I don’t really know how to solve this

Thank you

(Peter Niederwieser) #2

I don’t think this is supported directly, but you should be able to implement it yourself based on ‘Configuration.resolvedConfiguration.resolvedArtifacts’. Have a look at the Javadoc for ‘Configuration’, ‘ResolvedConfiguration’, and ‘ResolvedArtifact’.

General dependency management features (like excluding modules) are described in the Gradle user guide.

(Stephane Galles) #3

Would you do it like that ? (here for exemple I want to keep only the jar in my classpath, as some dependencies would pollute it with other type of artifacts)

def excludedArtifactFiles
= configurations.default.resolvedConfiguration.resolvedArtifacts
        .findAll {ResolvedArtifact artifact -> artifact.type != 'jar'}
        .collect {ResolvedArtifact artifact -> artifact.file}
  sourceSets {
 main {
  compileClasspath -= files(excludedArtifactFiles)
 test {
  compileClasspath -= files(excludedArtifactFiles)

Would it be a better idiom to construct an intermediate Configuration for the excluded files ?

Anyway this works for me, thank you for your answer.

(Stephane Galles) #4

I must also exclude these artifacts from the eclipse classpath, so I had to create a Configuration anyway

dependencies {
 excludedConf files(excludedArtifactFiles)

in order to do :

eclipse {
 classpath {
  minusConfigurations += configurations.excludedConf

But it does not work (the eclipse classpath still contains the artifacts).

I’ve done more tests, and I may be mistaken, but it really looks like only for files in the cache, something like : “eclipse plugin does not honor minusConfigurations for file dependencies when the files are in the cache”

Any idea ?

(Stephane Galles) #5

I’ve opened for the ‘minusConfiguration’ problem


I am trying to use minusConfigurations and build fails with the following error: Cause: Could not find property ‘allDependencies’ on main output.

Am I doing something wrong?

eclipse {
   classpath {
      plusConfigurations += configurations.provided
  plusConfigurations += configurations.integrationTestCompile
  minusConfigurations += sourceSets.main.output

(Peter Niederwieser) #7

‘sourceSets.main.output’ is not a configuration.


Thanks Peter for pointing it out. The Gradle message was a bit cryptic for me :slight_smile:

How can I exclude sourceSets.main.output from Eclipse classpath in this case? It includes a non existing directory (resources) and Eclipse complains about it.

Will it be a better approach to create the missing directory instead (sourceSets.main.output.each { it.mkdirs() }) ?