<legend id='aakOF'><style id='aakOF'><dir id='aakOF'><q id='aakOF'></q></dir></style></legend>

  • <small id='aakOF'></small><noframes id='aakOF'>

    <i id='aakOF'><tr id='aakOF'><dt id='aakOF'><q id='aakOF'><span id='aakOF'><b id='aakOF'><form id='aakOF'><ins id='aakOF'></ins><ul id='aakOF'></ul><sub id='aakOF'></sub></form><legend id='aakOF'></legend><bdo id='aakOF'><pre id='aakOF'><center id='aakOF'></center></pre></bdo></b><th id='aakOF'></th></span></q></dt></tr></i><div id='aakOF'><tfoot id='aakOF'></tfoot><dl id='aakOF'><fieldset id='aakOF'></fieldset></dl></div>

      <bdo id='aakOF'></bdo><ul id='aakOF'></ul>
    <tfoot id='aakOF'></tfoot>

        在 Gradle 中,如何生成具有解析为实际使用版本的动态依赖项的 POM 文件?

        In Gradle, how can I generate a POM file with dynamic dependencies resolved to the actual version used?(在 Gradle 中,如何生成具有解析为实际使用版本的动态依赖项的 POM 文件?)

        1. <legend id='CxYy3'><style id='CxYy3'><dir id='CxYy3'><q id='CxYy3'></q></dir></style></legend>

            <bdo id='CxYy3'></bdo><ul id='CxYy3'></ul>
            <tfoot id='CxYy3'></tfoot>
                  <tbody id='CxYy3'></tbody>

                <small id='CxYy3'></small><noframes id='CxYy3'>

                • <i id='CxYy3'><tr id='CxYy3'><dt id='CxYy3'><q id='CxYy3'><span id='CxYy3'><b id='CxYy3'><form id='CxYy3'><ins id='CxYy3'></ins><ul id='CxYy3'></ul><sub id='CxYy3'></sub></form><legend id='CxYy3'></legend><bdo id='CxYy3'><pre id='CxYy3'><center id='CxYy3'></center></pre></bdo></b><th id='CxYy3'></th></span></q></dt></tr></i><div id='CxYy3'><tfoot id='CxYy3'></tfoot><dl id='CxYy3'><fieldset id='CxYy3'></fieldset></dl></div>
                  本文介绍了在 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#getIncomingConfiguration#getResolvedConfiguration API 查询已解析的版本
                  • 使用 Groovy 的 XMlParser API 操作 POM(假设使用了新的 maven-publish 插件)
                  • 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)

                  关于 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 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Maven JAVA_HOME environment variable is not defined correctly, but it is(Maven JAVA_HOME 环境变量未正确定义,但它是)
                  Java System Environment Variable(Java 系统环境变量)
                  Java -classpath option(Java -classpath 选项)
                  Read environment variable in SpringBoot(在 SpringBoot 中读取环境变量)
                  Tomcat 8 - context.xml use Environment Variable in Datasource(Tomcat 8 - context.xml 在数据源中使用环境变量)
                  Issue with JAVA_HOME(JAVA_HOME 的问题)

                  <small id='fTPbV'></small><noframes id='fTPbV'>

                    <i id='fTPbV'><tr id='fTPbV'><dt id='fTPbV'><q id='fTPbV'><span id='fTPbV'><b id='fTPbV'><form id='fTPbV'><ins id='fTPbV'></ins><ul id='fTPbV'></ul><sub id='fTPbV'></sub></form><legend id='fTPbV'></legend><bdo id='fTPbV'><pre id='fTPbV'><center id='fTPbV'></center></pre></bdo></b><th id='fTPbV'></th></span></q></dt></tr></i><div id='fTPbV'><tfoot id='fTPbV'></tfoot><dl id='fTPbV'><fieldset id='fTPbV'></fieldset></dl></div>
                    <legend id='fTPbV'><style id='fTPbV'><dir id='fTPbV'><q id='fTPbV'></q></dir></style></legend>
                        <bdo id='fTPbV'></bdo><ul id='fTPbV'></ul>

                      • <tfoot id='fTPbV'></tfoot>

                            <tbody id='fTPbV'></tbody>