In Gradle, how can I generate a POM file with dynamic dependencies resolved to the actual version used?
Asked Answered
M

4

10

In Gradle, how can I generate a POM file with dynamic dependencies resolved to the actual version used?

dependencies {
    testCompile(group: 'junit', name: 'junit', version: '4.+')
}

This is generated from the dependency above.

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.+</version>
        <scope>test</scope>
    </dependency>
</dependencies>

I want to have the + resolved to an accrual version like below.

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>

The Gradle guide chapter on Maven Publishing talks about doing this, but does not mention how.

With this hook, you can modify any aspect of the POM. For example, you could replace the version range for a dependency with the actual version used to produce the build.

Solution

Using the information in Peter Niederwieser's answer, I created a task that reads a POM that contains dynamic dependencies and overwrites it with a new pom that has the dependencies resolved.

/**
 * Reads and Overwrites POM file resolving dynamic dependencies
 */
task cleanPom(dependsOn: writeNewPom) << {
    // Get existing pom file
    Node xml = new XmlParser().parse(pomFileLocation)

    // Generate map of resolved versions
    Map resolvedVersionMap = new HashMap()
    Set<ResolvedArtifact> resolvedArtifacts = configurations.compile.getResolvedConfiguration().getResolvedArtifacts()
    resolvedArtifacts.addAll(configurations.testCompile.getResolvedConfiguration().getResolvedArtifacts())
    resolvedArtifacts.each {
        resolvedVersionMap.put(it.getName(), it.getModuleVersion().getId().getVersion())
    }

    // Update dependencies with resolved versions
    xml.dependencies.first().each {
        Node artifactId = it.get("artifactId").first()
        def artifactName = artifactId.value().first()
        def artifactVersion = resolvedVersionMap.get(artifactName)

        Node version = it.get("version").first()
        version.value = artifactVersion
    }

    // Overwrite existing pom file
    new XmlNodePrinter(new PrintWriter(new FileWriter(pomFileLocation))).print(xml)
}
Mazzard answered 6/1, 2014 at 21:25 Comment(4)
Why didn't you use the pom.withXml() hook shown in the sample I linked to?Hatty
@PeterNiederwieser I am currently using the old 'maven' plugin to create a POM file and this code will modify the POM I am already creating. I assume that pom.withXml() is part of the new incubating 'maven-publish' plugin.Mazzard
There is a similar hook (and sample) for the old plugin. With the old plugin there is even a simpler solution, because it allows to manipulate the POM on an object level (demonstrated in the same sample).Hatty
@PeterNiederwieser Thanks, I will have to revisit and update this when I have time.Mazzard
H
5

It will require some effort to code this up. The two main parts are:

  • Querying resolved versions using the Configuration#getIncoming or Configuration#getResolvedConfiguration API
  • Manipulating the POM using Groovy's XMlParser API (assuming the new maven-publish plugin is used)

Information about the Configuration API can be found in the Gradle Build Language Reference, which further links into the Javadoc. The full Gradle distribution contains a tiny sample that demonstrates POM manipulation. Information about XmlParser can be found in the Groovy docs.

Hatty answered 6/1, 2014 at 22:52 Comment(0)
A
3

The solution with the pom.withXml() suggested by Peter looks like this:

publishing {
  publications {
    mavenCustom(MavenPublication) {
        from components.java
            pom.withXml {
                // Generate map of resolved versions
                Map resolvedVersionMap = [:]
                Set<ResolvedArtifact> resolvedArtifacts = configurations.compile.getResolvedConfiguration().getResolvedArtifacts()
                resolvedArtifacts.addAll(configurations.testCompile.getResolvedConfiguration().getResolvedArtifacts())
                resolvedArtifacts.each {
                    ModuleVersionIdentifier mvi = it.getModuleVersion().getId();
                    resolvedVersionMap.put("${mvi.getGroup()}:${mvi.getName()}", mvi.getVersion())
                }

              // Update dependencies with resolved versions
              def hasDependencies = !asNode().dependencies.isEmpty()
              if (hasDependencies) {
                  asNode().dependencies.first().each {
                      def groupId = it.get("groupId").first().value().first()
                      def artifactId = it.get("artifactId").first().value().first()
                      it.get("version").first().value = resolvedVersionMap.get("${groupId}:${artifactId}")
              }
          }
      }
   }
}
Aldarcy answered 2/9, 2015 at 12:51 Comment(2)
Does this work with Android projects? I am receiving error "Error:Could not get unknown property 'java' for SoftwareComponentInternal set." on the line from components.java. Previously I was using mavenDeployer: docs.gradle.org/current/userguide/maven_plugin.html. How can I use these together?Cropper
I've never worked with Android before, but did you apply the java-plugin in the gradle file?Aldarcy
B
2

I've taken a stab at integrating this into a plugin that can be applied, the specific code is available here: https://github.com/nebula-plugins/nebula-publishing-plugin/blob/master/src/main/groovy/nebula/plugin/publishing/maven/ResolvedMavenPlugin.groovy

And it can be included via jcenter() via 'com.netflix.nebula:nebula-publishing-plugin:1.9.1'.

Bengurion answered 20/1, 2014 at 18:24 Comment(0)
L
1

As of Gradle 5.2, the versionMapping block of MavenPublication can be used to achieve this:

publishing {
    publications {
        mavenJava(MavenPublication) {
            groupId 'my.group'
            artifactId 'my-artifact'
            from components.java

            versionMapping {
                allVariants {
                    fromResolutionResult()
                }
            }
        }
    }
}

https://docs.gradle.org/5.2/userguide/publishing_maven.html#publishing_maven:resolved_dependencies

Laureate answered 11/1 at 23:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.