生成Maven中的“provided”依赖项,从Gradle中的“compileOnly”

huangapple go评论154阅读模式
英文:

Generate Maven "provided" dependency in pom.xml from Gradle "compileOnly"

问题

如果我理解正确,Gradle的compileOnly依赖对应于Gradle的旧版本provided,同时也对应于Maven POM的provided。它在Gradle世界中运行得很完美。

但是,compileOnlymaven-publish生成的pom.xml中不会生成任何provided依赖。

我们正在使用Gradle将Maven构件发布到Maven Central。我们想要从Gradle的compileOnly中明确声明provided依赖项在发布的pom.xml中。

有没有人知道是否有任何简单的方法来做到这一点?或者,我们是否需要在以下位置编写自己的Gradle脚本:

publishing {
  publications {
    maven(MavenPublication) {
      pom {
         /* 我们自己的Gradle脚本来声明provided依赖项。 */
      }
    }
  }
}
英文:

If I understand correctly, Gradle's compileOnly dependency corresponds to Gradle's older provided, and at the same time, Maven POM's provided. It works perfectly in the Gradle world.

But, compileOnly does not generate any provided dependency in pom.xml generated by maven-publish.

We are publishing a Maven artifact to Maven Central, using Gradle. We would like to declare provided explicitly in the published pom.xml from Gradle's compileOnly.


Does anyone know if there is any simple way to do that? Or, do we need to write our own Gradle scripting in :

publishing {
  publications {
    maven(MavenPublication) {
      pom {
         /* Our own Gradle scripting to declare provided dependencies. */
      }
    }
  }
}

答案1

得分: 1

Sure, here's the translated part:

publishing {
  publications {
    maven(MavenPublication) {
      pom {
        withXml {
          project.configurations.compileOnly.allDependencies.each { dependency ->
              asNode().dependencies[0].appendNode("dependency").with {
                it.appendNode("groupId", dependency.group)
                it.appendNode("artifactId", dependency.name)
                it.appendNode("version", dependency.version)
                it.appendNode("scope", "provided")
              }
          }
        }
      }
    }
  }
}
英文:

I know we can tweak it by scripting dirty like below, but we basically don't want to "script" in build.gradle as far as possible.

publishing {
  publications {
    maven(MavenPublication) {
      pom {
        withXml {
          project.configurations.compileOnly.allDependencies.each { dependency ->
              asNode().dependencies[0].appendNode("dependency").with {
                it.appendNode("groupId", dependency.group)
                it.appendNode("artifactId", dependency.name)
                it.appendNode("version", dependency.version)
                it.appendNode("scope", "provided")
              }
          }
        }
      }
    }
  }
}

答案2

得分: 0

I think scripting is the way to go... you can create a method och pass the pom e.g

发布 {
发布物 {
mavenDependencyList(MavenPublication) {
修复依赖范围(pom)
}
}
}

void 修复依赖范围(pom) {
pom.withXml {
asNode().dependencies.''.findAll() {
it.scope.text() == 'compileOnly' && project.configurations.compile.allDependencies.find { dep ->
dep.name == it.artifactId.text()
}
}.each() {
it.scope
.value = 'provided'
}
}
}

英文:

I think scripting is the way to go... you can create a method och pass the pom e.g

 publishing {
        publications {
            mavenDependencyList(MavenPublication) {
                fixDependencyScope(pom)
            }
        }
    }


void fixDependencyScope(pom) {
    pom.withXml {
        asNode().dependencies.'*'.findAll() {
            it.scope.text() == 'compileOnly' && project.configurations.compile.allDependencies.find { dep ->
                dep.name == it.artifactId.text()
            }
        }.each() {
            it.scope*.value = 'provided'
        }
    }
}

huangapple
  • 本文由 发表于 2020年8月12日 16:08:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63372385.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定