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

Ant*_*met 3 nexus gradle maven-publish

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

有没有办法捕获这个生成的网址?

Abh*_*kar 6

@Hollerweger 的回答可能是最不粗俗的,但有几个方面是错误的:

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

把它们放在一起:

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


JBi*_*gas 1

遗憾的是,此信息无法通过 gradle 构建系统获得...您可以做的是创建一个任务来完成publish task. 然后查询taskMaven 存储库以获取最新的构建。Maven 将查找maven-metadata.xml文件并返回<release>标签值或最近上传的内容(如果缺少)。您可以从响应的标头中获取确切的下载 URL Location

以下是如何查询 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
Run Code Online (Sandbox Code Playgroud)

解释命令

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
Run Code Online (Sandbox Code Playgroud)