Christian Goetze
Christian Goetze

Reputation: 2414

How can I retrieve both the artifact file and the artifact metadata from gradle dependencies?

I want to write a gradle build task to perform some artifact repo copying and reorg. I got this far:

apply plugin: 'maven'
apply plugin: 'maven-publish'

repositories {
  ...
}

configurations {
  ...
}

dependencies {
  ...
}

task doit << {
  configurations.each { configuration ->
    println configuration
    configuration.files.each { file ->
      println "  f=${file.path}"
    }
    configuration.dependencies.each { dependency ->
      println "  g=${dependency.group}"
      println "  i=${dependency.name}"
      println "  v=${dependency.version}"
      dependency.artifacts.each { artifact ->
        println "    x=${artifact.classifier}"
        println "    n=${artifact.name}"
        println "    u=${artifact.url}"
      }
    }
  }
}

What I can't get is a reference to the downloaded file within the dependency.artifacts.each() loop.

The best I can do is populate an array by going through the configuration.files, and then hope that my second set of loops over the artifact metadata goes in the same order as the files. I'm obviously missing something

Maybe there is some alternate way? What I really want is to generate a set of tasks, one per artifact, that will allow me to publish a new artifact with changed metadata items (i.e. group id, artifact id and version should change).

Upvotes: 6

Views: 7146

Answers (1)

Peter Niederwieser
Peter Niederwieser

Reputation: 123900

You want to iterate over the resolved dependencies/artifacts, not over the requested ones. Something like:

configuration.resolvedConfiguration.resolvedArtifacts.each { artifact ->
    println artifact.moduleVersion.id.group
    println artifact.moduleVersion.id.name
    println artifact.moduleVersion.id.version
    println artifact.file
}

Upvotes: 5

Related Questions