发布后,我想知道已发布工件的 url 是什么(在其他 gradle 任务中使用它进行自动化部署)。
有什么方法可以捕获这个生成的 url?
@Hollerweger 的答案可能是最不老套的,但它在几个方面是错误的:
PublishToMavenLocal
类PublishToMavenRepository
。扩展它并打印一条消息说工件已发布到 Nexus 是错误的,因为即使发布到本地 Maven 存储库也会打印该消息。要使用的正确任务类是PublishToMavenRepository
发布到远程仓库。repository
属性。把它们放在一起:
tasks.withType(PublishToMavenRepository) {
doFirst {
println("Publishing ${publication.groupId}:${publication.artifactId}:${publication.version} to ${repository.url}")
}
}
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 基础 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(".", "/");
}
遗憾的是,这些信息无法通过 gradle 构建系统获得......您可以做的是创建一个任务来完成publish
task
. 然后task
查询 Maven 存储库以获取最新版本。Maven 将在maven-metadata.xml
文件中查找并返回<release>
标签值或最新的上传(如果缺少)。您可以从响应的Location
标头中获取确切的下载 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