In Gradle, how can I generate a POM file with dynamic dependencies resolved to the actual version used?(在 Gradle 中,如何生成具有解析为实际使用版本的动态依赖项的 POM 文件?)
问题描述
在 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#getResolvedConfigurationAPI 查询已解析的版本 - 使用 Groovy 的 
XMlParserAPI 操作 POM(假设使用了新的maven-publish插件) 
- Querying resolved versions using the 
Configuration#getIncomingorConfiguration#getResolvedConfigurationAPI - Manipulating the POM using Groovy's 
XMlParserAPI (assuming the newmaven-publishplugin 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 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Gradle 中,如何生成具有解析为实际使用版本的动态依赖项的 POM 文件?
				
        
 
            
        基础教程推荐
- 从 python 访问 JVM 2022-01-01
 - 在 Java 中创建日期的正确方法是什么? 2022-01-01
 - 不推荐使用 Api 注释的描述 2022-01-01
 - Java Swing计时器未清除 2022-01-01
 - 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
 - 多个组件的复杂布局 2022-01-01
 - 大摇大摆的枚举 2022-01-01
 - 验证是否调用了所有 getter 方法 2022-01-01
 - 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
 - Java 实例变量在两个语句中声明和初始化 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				