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

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

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

This is generated from the above dependency.

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

I want + resolve to the accrual version, as shown below.

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

The Gradle chapter of the Maven Publishing tutorial talks about this, but does not mention how to do this.

With this hook, you can change any aspect of the POM. For example, you can replace the version range to depend on the actual version used to create the assembly.

Decision

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

 /** * 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) } 
+8
java maven dependencies gradle
source share
3 answers

This will require some effort. Two main parts:

  • Requesting permitted versions using the Configuration#getIncoming or Configuration#getResolvedConfiguration
  • Manipulate POM using the Groovy XMlParser API (assuming the new maven-publish plugin is used)

Information on the Configuration API can be found in the Gradle Assembly Language Reference , which is further linked to Javadoc. The full Gradle distribution contains a tiny sample that demonstrates POM manipulation. Information about XMlParser can be found in Groovy docs.

+5
source share

I tried to integrate 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 enabled via jcenter () through 'com.netflix.nebula: nebula-publishing-plugin: 1.9.1'.

+2
source share

The solution with pom.withXml() proposed by Peter is as follows:

 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}") } } } } } 
+2
source share

All Articles