gradlegradle-pluginmaven-repository

How to download a .pom file from the gradle plugin repository?


I want to build a project with a couple of gradle plugins on the machine without internet connection. I have a private maven repo with all of dependencies for this project, except .pom file for gradle plugins. How can I grab it from plugins.gradle.org and publish to my private repo?


Solution

  • This code, using Kotlin DSL, defines a task getThePoms which obtains the POMs from the artifacts added to configuration pomConfiguration1 and hosted at the Gradle Plugin Portal, and copies them to build/poms.

    Note you need to specify the artifact coordinates in the dependencies block, not the Gradle plugin ids, which are part of a separate lookup mechanism.

    val pomConfigurationName = "pomConfiguration"
    
    repositories {
        gradlePluginPortal()
    }
    
    val pomConfiguration = configurations.register(pomConfigurationName) {
        isTransitive = false // We only want the declared dependencies
    }
    
    dependencies {
        pomConfigurationName("org.jetbrains.kotlin:kotlin-gradle-plugin:2.0.0")
        pomConfigurationName("org.jetbrains.kotlin:kotlin-serialization:2.0.0")
    }
    
    tasks.register<Copy>("getThePoms") {
        val componentIds = pomConfiguration.get().resolvedConfiguration.resolvedArtifacts.map { it.id.componentIdentifier }
        val artifactQueryResult = dependencies.createArtifactResolutionQuery()
            .forComponents(componentIds)
            .withArtifacts(MavenModule::class.java, MavenPomArtifact::class.java)
            .execute()
        val pomFiles = artifactQueryResult.resolvedComponents
            .flatMap { component -> component.getArtifacts(MavenPomArtifact::class) }
            .map { result -> (result as ResolvedArtifactResult).file }
        from(pomFiles)
        into(layout.buildDirectory.dir("poms"))
    }
    

    1 Here using the Kotlin Gradle plugin and Kotlin serialization plugin as examples.