在Gradle中,如何生成一个POM文件,其动态依赖关系解析为实际使用的版本? [英] In Gradle, how can I generate a POM file with dynamic dependencies resolved to the actual version used?

查看:2739
本文介绍了在Gradle中,如何生成一个POM文件,其动态依赖关系解析为实际使用的版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 依赖关系{
testCompile(group:'junit',name:'junit',version:'4. +')
}

这是从上面的依赖生成的。

 依赖> 
<依赖关系>
< groupId> junit< / groupId>
< artifactId> junit< / artifactId>
< version> 4。+< / version>
< scope> test< / scope>
< / dependency>
< / dependencies>

我想让 + 一个应计版本如下。

 <依赖关系> 
<依赖关系>
< groupId> junit< / groupId>
< artifactId> junit< / artifactId>
< version> 4.12< / version>
< scope> test< / scope>
< / dependency>
< / dependencies>

关于Maven Publishing 谈论这样做,但没有提到如何。


使用此钩子,您可以修改POM的任何方面。例如,您可以将依赖关系的版本范围替换为用于生成构建的实际版本。




解决方案



使用Peter Niederwieser的答案中的信息,读取包含动态依赖关系的POM的任务,并用解决依赖关系的新pom覆盖它。

  / ** 
*读取和覆盖解决动态依赖关系的POM文件
* /
task cleanPom(dependsOn:writeNewPom)<< {
//获取现有的pom文件
Node xml = new XmlParser()。parse(pomFileLocation)

//生成解析版本的映射
映射resolvedVersionMap = new HashMap()
设置< ResolvedArtifact> resolveArtifacts = configured.compile.getResolvedConfiguration()。getResolvedArtifacts()
resolvedArtifacts.addAll(configurations.testCompile.getResolvedConfiguration()。getResolvedArtifacts())
resolvedArtifacts.each {
resolvedVersionMap.put(it .getName(),it.getModuleVersion()。getId()。getVersion())
}

//更新依赖关系与解析版本
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
}

//覆盖现有pom文件
new XmlNodePrinter(new PrintWriter(new FileWriter(pomFileLocation)))。print(xml)
}


解决方案

需要一些努力来编码t他的两个主要部分是:




  • 使用配置#getIncoming 配置#getResolvedConfiguration API

  • 使用Groovy的 XMlParser API(假设使用新的 maven-publish 插件)



有关配置 API可以在 Gradle Build Language Reference ,进一步链接到Javadoc。
完整的Gradle发行版包含一个演示POM操纵的小样本。关于 XmlParser 的信息可以在Groovy文档中找到。


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

解决方案

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.

这篇关于在Gradle中,如何生成一个POM文件,其动态依赖关系解析为实际使用的版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆