Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
}
like image 840
Mike Rylander Avatar asked Jan 06 '14 21:01

Mike Rylander


People also ask

How do I resolve Gradle dependencies?

Given a required dependency, with a version, Gradle attempts to resolve the dependency by searching for the module the dependency points at. Each repository is inspected in order. Depending on the type of repository, Gradle looks for metadata files describing the module ( .

How does Gradle resolve transitive dependencies?

Transitive dependencyBy default, Gradle resolves transitive dependencies automatically. The version selection for transitive dependencies can be influenced by declaring dependency constraints.

How do I sync Gradle with dependencies?

Perform a Gradle sync/reload¶ The file versions. properties is part of the Gradle build. Consequently, after you have edited it, you need to ensure the IDE gets the changes. Android Studio: Run the “Sync Project with Gradle Files” action (via ctrl / cmd + shift + A ), or click the elephant + arrow icon in the toolbar.


1 Answers

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.

like image 186
Peter Niederwieser Avatar answered Oct 16 '22 10:10

Peter Niederwieser