使用 maven-publish gradle 插件获取已发布工件的 uri

Get uri of published artifact using maven-publish gradle plugin

发布后,我想知道已发布工件的 url 是什么(以便在其他 gradle 任务中使用它进行自动部署)。

有什么方法可以捕获这个生成的url?

遗憾的是,此信息无法通过 gradle 构建系统获得...您可以做的是创建一个任务来完成 publish task。然后让 task 查询 Maven 存储库以获取最新构建。 Maven 将查看 maven-metadata.xml 文件和 return <release> 标记值或最新上传(如果缺少)。您可以从响应的 Location header.

中获取确切的下载 url

这是一个如何查询 Maven 存储库的示例

$ curl -Is 'http://my.nexus.repo.com:8081/nexus/service/local/artifact/maven/redirect?r=Release&g=com.my.group.id&a=myArtifactId&v=RELEASE&p=war' | grep -Fi Location | cut -d' ' -f2

http://my.nexus.repo.com:8081/nexus/service/local/repositories/Release/content/com/my/group/id/myArtifactId/1.0.012/myArtifactId-1.0.012.war

解释命令

curl -Is http://<nexus-url>:<nexus-port>/nexus/service/local/artifact/maven/redirect?r=<nexus-repository>&g=<artifact-group-id>&a=<artifact-name>&v=<artifact-version>
    curl
        -I # only print return headers
        -s # quiet output of curl's downloading progress
    url params
        r  # nexus-repository name, tends to be Release or Snapshot
        g  # group id for the artifact
        a  # artifact id
        v  # artifact version or a link like RELEASE (don't use LATEST it's problematic)
        ##  you can also supply classifier and extension if needed
        c  # artifact classifier
        e  # artifact extension

我找到了上传的名为 maven-metadata-remote.xml 的文件的本地副本。它在这个目录下:build/tmp/publishMavenPublicationToNexusRepository/

示例:

$ cat build/tmp/publishMavenPublicationToNexusRepository/com/example/my.package/0.1.0-SNAPSHOT/maven-metadata-remote.xml

<?xml version="1.0" encoding="UTF-8"?>
<metadata>
  <groupId>com.example</groupId>
  <artifactId>my.package</artifactId>
  <version>0.1.0-SNAPSHOT</version>
  <versioning>
    <snapshot>
      <timestamp>20180326.191238</timestamp>
      <buildNumber>36</buildNumber>
    </snapshot>
    <lastUpdated>20180326191238</lastUpdated>
  </versioning>
</metadata>

所以我能够通过在 build.gradle 文件的底部添加类似这样的内容来摄取该文件:

tasks.withType(PublishToMavenRepository) {
  doLast {
    def metadata_file_path = "build/tmp/publishMavenPublicationToNexusRepository/com/example/my.package/0.1.0-SNAPSHOT"
    def metadata_XML = new File("${metadata_file_path}/maven-metadata-remote.xml").text
    def metadata = new groovy.util.XmlSlurper().parseText(metadata_XML)
    def BN = metadata.versioning.snapshot.buildNumber
    def TS = metadata.versioning.snapshot.timestamp
    println "INFO: uploaded version 0.1.0-${TS}-${BN}.pom to Nexus"
...

您可以扩展所有 Maven 发布任务并再次计算 url: 我只需要文件夹 - 对于发布工件,您也可以在末尾添加工件,但我还没有找到适合 SNAPSHOT 的好的解决方案。 nexus base repo URL 预计为人所知。

tasks.withType(AbstractPublishToMaven) {
    doLast {
        String urlString = nexusUrl.toString() + convertPackageNameToPath(publication.groupId) + "/" + publication.artifactId + "/" + publication.version
        println "Artifact URL: " + urlString
    }
}

static String convertPackageNameToPath(String packageName) {
    return packageName.replace(".", "/");
}

@Hollerweger 的回答可能是最不老套的,但它在几个方面是错误的:

  1. AbstractPublishToMavenPublishToMavenLocalPublishToMavenRepository 的超 class。扩展它并打印一条消息说工件已发布到 Nexus 是错误的,因为即使发布到本地 Maven 存储库也会打印该消息。要使用的正确任务 class 是发布到远程仓库的 PublishToMavenRepository
  2. 不需要知道远程仓库URL;出版物有 repository 属性.

综合起来:

tasks.withType(PublishToMavenRepository) {
    doFirst {
        println("Publishing ${publication.groupId}:${publication.artifactId}:${publication.version} to ${repository.url}")
    }
}