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

时间:2017-01-19 15:44:03

标签: gradle nexus maven-publish

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

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

4 个答案:

答案 0 :(得分:1)

遗憾的是,此信息无法通过gradle构建系统...您可以创建的任务是最终确定publish task。然后让task查询maven存储库以获取最新版本。 Maven会查看maven-metadata.xml文件并返回<release>代码值或最新上传的内容(如果缺少该值)。您可以从回复的Location标题中获取确切的下载网址。

以下是您如何查询maven repo

的示例
$ 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

答案 1 :(得分:1)

我找到了一个名为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"
...

答案 2 :(得分:0)

您可以扩展所有maven发布任务并再次计算网址: 我只需要文件夹 - 对于发布工件,你也可以在最后添加工件,但我还没有为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(".", "/");
}

答案 3 :(得分:0)

@Hollerweger的回答可能是最少的,但在几种方面是错误的:

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

将它们放在一起:

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