问题描述
在 Gradle 中,如何生成具有解析为实际使用版本的动态依赖项的 POM 文件?
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>
Maven Publishing 上的 Gradle 指南章节谈到了这样做,但没有提到如何.
The Gradle guide chapter on Maven Publishing talks about doing this, but does not mention how.
使用这个钩子,您可以修改 POM 的任何方面.例如,您可以将依赖项的版本范围替换为用于生成构建的实际版本.
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.
解决方案
使用 Peter Niederwieser 回答中的信息,我创建了一个任务,该任务读取包含动态依赖项的 POM,并用已解决依赖项的新 pom 覆盖它.
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)
}
推荐答案
这需要一些努力来编写代码.两个主要部分是:
It will require some effort to code this up. The two main parts are:
- 使用
Configuration#getIncoming
或Configuration#getResolvedConfiguration
API 查询已解析的版本 - 使用 Groovy 的
XMlParser
API 操作 POM(假设使用了新的maven-publish
插件)
- Querying resolved versions using the
Configuration#getIncoming
orConfiguration#getResolvedConfiguration
API - Manipulating the POM using Groovy's
XMlParser
API (assuming the newmaven-publish
plugin is used)
关于 Configuration
API 的信息可以在 Gradle 构建语言参考,进一步链接到 Javadoc.完整的 Gradle 发行版包含一个 小样本 演示 POM 操作.关于 XmlParser
的信息可以在 Groovy 文档中找到.
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.
这篇关于在 Gradle 中,如何生成具有解析为实际使用版本的动态依赖项的 POM 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!