Is there a way to get the files associated with a dependency from a configuration with modifications to the dependency?


(John Engelman) #1

Hard to explain this in the title, so I’ll write an example. First, assume, I have a dependency A that depends on B and I declare this dependency in my build:

dependencies {
  compile 'a:a:1.0'
}

I can get the files for this dependency by doing this:

configurations.compile.files(dependencies.create('a:a:1.0'))

That would return a Set that contains both ‘a-1.0.jar’ and ‘b-1.0.jar’.

What I’m trying to do, is get the files for the A dependency but exclude it’s transitive dependencies. So I tried something like this:

configurations.compile.files(
  dependencies.create('a:a:1.0') {
    transitive = false
  }
)

I expect to get ‘a-1.0.jar’ but NOT ‘b-1.0.jar’. Instead I get nothing back. I believe it’s because it’s matching the dependency that I’ve created against what was resolved in the configuration. Since the dependency I’ve created is different (transitive=false, instead of true in the original declaration), than it doesn’t match and no files are returned.

Is there anyway to do this type of query against a resolved configuration?


(Peter Niederwieser) #2

If you can’t change the initial dependency declaration or configuration to non-transitive, you could perhaps do something like this:

def files = configurations.compile.resolvedConfiguration.resolvedArtifacts.find { artifact ->
  def id = artifact.moduleVersion.id
  id.group == "a" && id.name == "a" && id.version == "1.0"
 }.collect { it.file }
println files

One drawback of this approach is that it will still resolve everything.


(John Engelman) #3

Hm, that might be an option. I see there is a new Artifact Query API being introduced in Gradle 2.0 (https://github.com/gradle/gradle/commit/de7abf924fb4a17615bcb7ceec1bb5a685fdc715). It looks like this might be what I want. Maybe Daz can comment?

My use case is that I’m reimplementing my Shadow plugin to be based on Gradle’s Jar task and then using a custom CopyTaskAction underneath. One of the features of Shadow (from Maven’s Shade) is that you can exclude dependencies from the shaded jar using their Maven coordinates (and additionally apply filtering rules to specific jars using Maven coordinates). So I’m trying to replicate that feature in the most Gradle way possible.