user2615350
user2615350

Reputation: 363

Getting the pom for a dependency in Gradle

In resolving a dependency from a Maven-repository, Gradle has to download the corresponding pom-file. I would like to access this file from within the Gradle-script and save it somewhere. How can I do this?

Upvotes: 3

Views: 3352

Answers (2)

Mark Vieira
Mark Vieira

Reputation: 13466

You could simply declare a dependency on the POM itself.

configurations {
    pom
}


dependencies {
    pom 'org.foo:some-lib:1.0.0@pom'
}

task copyPom(type: Copy) {
    into "${buildDir}/poms"
    from configurations.pom
}

If you want the aar and jar dependencies, not the pom itself, you can use the above, except omit the @pom specifier in the dependencies. You can also have both of these simultaneously.

    pom 'org.foo:some-lib:1.0.0'

Upvotes: 3

kropp
kropp

Reputation: 351

Take a look at Artifact Query API. Here is the sample code (Groovy):

def componentIds = configurations.compileClasspath.incoming.resolutionResult.allDependencies.collect { it.selected.id }
def result = project.dependencies.createArtifactResolutionQuery()
        .forComponents(componentIds)
        .withArtifacts(MavenModule, MavenPomArtifact)
        .execute()
for (component in result.resolvedComponents) {
    component.getArtifacts(MavenPomArtifact).each { println "POM file for ${component.id}: ${it.file}" }
}

I'm using the same API in my plugin in Kotlin.

Upvotes: 9

Related Questions