Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the pom for a dependency in Gradle

Tags:

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?

like image 582
user2615350 Avatar asked Dec 02 '25 21:12

user2615350


2 Answers

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.

like image 74
kropp Avatar answered Dec 06 '25 00:12

kropp


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'
like image 30
Mark Vieira Avatar answered Dec 05 '25 22:12

Mark Vieira



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!