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

后端 未结 3 1839
我在风中等你
我在风中等你 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: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 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}")
                  }
              }
          }
       }
    }
    

提交回复
热议问题