一个pom文件中出现了两个相同的依赖_在Gradle中,如何生成一个POM文件,其动态依赖关系解析为实际使用的版本?...

本文探讨了在Gradle中如何生成POM文件时,将动态依赖(如'4.+’)解析为实际使用的版本(如'4.12’)。通过创建任务并使用Configuration API查询已解析的版本,再利用Groovy的XmlParser API更新POM文件,实现了动态依赖到具体版本的转换。
摘要由CSDN通过智能技术生成

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.

junit

junit

4.+

test

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

junit

junit

4.12

test

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 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)

}

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值