In Gradle, how can I generate a POM file with dynamic dependencies resolved to the actual version used?

后端 未结 3 1840
我在风中等你
我在风中等你 2021-01-02 10:22

In Gradle, how can I generate a POM file with dynamic dependencies resolved to the actual version used?

dependencies {
    testCompile(group: \'junit\', name         


        
相关标签:
3条回答
  • 2021-01-02 10:51

    I've taken a stab at integrating this into a plugin that can be applied, the specific code is available here: https://github.com/nebula-plugins/nebula-publishing-plugin/blob/master/src/main/groovy/nebula/plugin/publishing/maven/ResolvedMavenPlugin.groovy

    And it can be included via jcenter() via 'com.netflix.nebula:nebula-publishing-plugin:1.9.1'.

    0 讨论(0)
  • 2021-01-02 10:53

    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)
  • 2021-01-02 10:55

    The solution with the pom.withXml() suggested by Peter looks like this:

    publishing {
      publications {
        mavenCustom(MavenPublication) {
            from components.java
                pom.withXml {
                    // Generate map of resolved versions
                    Map resolvedVersionMap = [:]
                    Set<ResolvedArtifact> resolvedArtifacts = configurations.compile.getResolvedConfiguration().getResolvedArtifacts()
                    resolvedArtifacts.addAll(configurations.testCompile.getResolvedConfiguration().getResolvedArtifacts())
                    resolvedArtifacts.each {
                        ModuleVersionIdentifier mvi = it.getModuleVersion().getId();
                        resolvedVersionMap.put("${mvi.getGroup()}:${mvi.getName()}", mvi.getVersion())
                    }
    
                  // Update dependencies with resolved versions
                  def hasDependencies = !asNode().dependencies.isEmpty()
                  if (hasDependencies) {
                      asNode().dependencies.first().each {
                          def groupId = it.get("groupId").first().value().first()
                          def artifactId = it.get("artifactId").first().value().first()
                          it.get("version").first().value = resolvedVersionMap.get("${groupId}:${artifactId}")
                  }
              }
          }
       }
    }
    
    0 讨论(0)
提交回复
热议问题