简体   繁体   English

使用 CURL 下载 Nexus 工件

[英]Nexus artifact download using CURL

I'm trying to download an artifact uploaded to nexus using CURL.我正在尝试使用 CURL 下载上传到 nexus 的工件。 But I'm unable to get it downloaded.但是我无法下载它。 The below command execution from command prompt doesn't download the required zip file and I'm using Nexus admin account从命令提示符执行以下命令不会下载所需的 zip 文件,我使用的是 Nexus 管理员帐户

curl -X GET -u userid:pwd "http://nexusserver:8081/nexus/service/local/artifact/maven/redirect?r=Repo_Name&g=GroupID&a=artifactID&v=LATEST&p=zip" -O

Did I form the URL correctly?我是否正确形成了 URL? I tried to browse the URL ( http://nexusserver:8081/nexus/service/local/artifact/maven/redirect?r=Repo_Name&g=GroupID&a=artifactID&v=LATEST&p=zip ), but got HTTP 404 Not found in Nexus Repository Manager. I tried to browse the URL ( http://nexusserver:8081/nexus/service/local/artifact/maven/redirect?r=Repo_Name&g=GroupID&a=artifactID&v=LATEST&p=zip ), but got HTTP 404 Not found in Nexus Repository Manager . I'm using Nexus version 3.0.2-02.我正在使用 Nexus 版本 3.0.2-02。 I'm new to nexus and any help is greatly appreciated.我是nexus的新手,非常感谢任何帮助。

Thanks谢谢

In newer versions of nexus you can:在较新版本的 nexus 中,您可以:

  1. Use curl to search for a maven artifact in your nexus via the new REST-API使用 curl 通过新的 REST-API 在您的联系中搜索 Maven 工件
  2. Parse the json-response to extract a download-link解析 json-response 以提取下载链接
  3. Use curl to download the artifact使用 curl 下载神器

On bash this boils down to:在 bash 上,这归结为:

$ curl -sSL -X GET -G "http://mynexus3.local/service/rest/v1/search/assets" \
  -d repository=maven-snapshots \
  -d maven.groupId=my.group.id \
  -d maven.artifactId=my-artifact \
  -d maven.baseVersion=1.0-SNAPSHOT \
  -d maven.extension=jar \
  -d maven.classifier=jar-with-dependencies \
  | grep -Po '"downloadUrl" : "\K.+(?=",)' \
  | xargs curl -fsSL -o my-artifact.jar

The first block will search for your artifact and output something similar to第一个块将搜索您的工件并输出类似于

{
  "items" : [ {
    "downloadUrl" : "http://mynexus3.local/repository/maven-snapshots/my/group/id/my-artifact/1.0-SNAPSHOT/my-artifact-1.0-20180821.085657-1-jar-with-dependencies.jar",
    "path" : "/my/group/id/my-artifact/1.0-SNAPSHOT/my-artifact-1.0-20180821.085657-1-jar-with-dependencies.jar",
    "id" : "foo",
    "repository" : "maven-snapshots",
    "format" : "maven2",
    "checksum" : {
      "sha1" : "bar",
      "md5" : "baz"
    }
  } ],
  "continuationToken" : null
}

Then you can use grep or something similar to extract the download URL.然后你可以使用 grep 或类似的东西来提取下载 URL。 Finally you pass the extracted URL to curl again to download your artifact.最后,您再次将提取的 URL 传递给 curl 以下载您的工件。 (tested with Nexus 3.13) (使用 Nexus 3.13 测试)

Indeed Sonatype brilliantly decided to change the REST API in a way totally incompatible from Nexus2 to Nexus3 - for the joy of system aministrators.事实上,Sonatype 明智地决定以一种从 Nexus2 到 Nexus3 完全不兼容的方式改变 REST API——为了系统管理员的乐趣。 So the /service/local/artifact/maven/ is no longer available in Nexus3.所以 /service/local/artifact/maven/ 在 Nexus3 中不再可用。

An alternative way - independent of the Nexus version - is using Maven:另一种方法 - 独立于 Nexus 版本 - 使用 Maven:

mvn -Dmaven.wagon.http.ssl.insecure=true org.apache.maven.plugins:maven-dependency-plugin:3.0.1:copy -Dartifact=mvngroup:mvnartifactid:mvnversion:mvnpackaging -DoutputDirectory=./ mvn -Dmaven.wagon.http.ssl.insecure=true org.apache.maven.plugins:maven-dependency-plugin:3.0.1:copy -Dartifact=mvngroup:mvnartifactid:mvnversion:mvnpackaging -DoutputDirectory=./

where "mvnpackaging" can be jar, war, zip....其中“mvnpackaging”可以是 jar、war、zip....

you can use curl -L -X GET 'https://MY_NEXUS/service/rest/v1/search/assets/download?sort=version&repository=MY-REPO&group=MY_GROUP&name=MY_ARTIFACT_NAME&maven.baseVersion=0.1-SNAPSHOT' --output some.file with Nexus 3.你可以使用curl -L -X GET 'https://MY_NEXUS/service/rest/v1/search/assets/download?sort=version&repository=MY-REPO&group=MY_GROUP&name=MY_ARTIFACT_NAME&maven.baseVersion=0.1-SNAPSHOT' --output some.file与 Nexus 3。

Add -u usr:pw if needed.如果需要,添加-u usr:pw

You could use the following endpoint:您可以使用以下端点:
GET /service/rest/v1/search/assets/download
that does the following:执行以下操作:
This endpoint is specifically designed to search for one asset and then redirect the request to the downloadUrl of that asset此端点专门用于搜索一项资产,然后将请求重定向到该资产的 downloadUrl

Example:示例:
curl -u admin:admin123 -X GET 'http://localhost:8081/service/rest/v1/search/assets?group=org.osgi&name=org.osgi.core&version=4.3.1&maven.extension=jar&maven.classifier

The artifacts are now under URLs that are similar to:工件现在位于类似于以下内容的 URL 下:

http://nexusserver:8081/repository/ {repositoryname}/ http://nexusserver:8081/repository/ {repositoryname}/

Using that and some knowledge of Maven paths, you can get to artifacts.使用它和 Maven 路径的一些知识,你可以得到工件。

That said the URL you are using will not work yet as well, as that was a REST API call we've removed for the time being.也就是说,您使用的 URL 还不能正常工作,因为这是我们暂时删除的 REST API 调用。

In newer versions of Nexus Repository, we've got the beginnings of our REST API.在较新版本的 Nexus Repository 中,我们开始了 REST API。 If you upgrade to 3.3 at a minimum (and 3.5 because it's out now), you can access the beginnings of this work at:如果您至少升级到 3.3(和 3.5,因为它现在已经出来了),您可以在以下位置访问这项工作的开始:

http://nexusserver:8081/swagger-ui/ http://nexusserver:8081/swagger-ui/

This is beta functionality so is likely to change, aka if you write something and it breaks in a newer version, you'll need to fix it, but you can take a look there to see if there are some endpoints that will help you.这是测试版功能,因此可能会发生变化,也就是说,如果您编写了一些内容并且在较新版本中出现故障,您需要修复它,但您可以查看那里是否有一些端点可以帮助您。

You likely want to take a look at: http://localhost:8081/swagger-ui/#!/search/search你可能想看看: http://localhost:8081/swagger-ui/#!/search/search

Below statement worked for me.下面的陈述对我有用。

curl -X GET https://<username>:<password>@<nexusdomain>/repository/<repository name>/<filepath> --output <filename>

eg例如

curl -X GET https://sampleuser:samplepassword@mynexus.com/repository/maven-public/public/util/demo.jar --output demo.jar

This works fine for me:这对我来说很好:

curl -L -X GET 'http://YOURNEXUSSERVER:8081/service/rest/v1/search/assets/download?sort=version&repository=YOUREPO&group=YOURGROUP&name=YOURARTIFACTID&maven.extension=war' --output '/d/YOUR/PATH/FILE.war' -u user:password curl -L -X GET 'http://YOURNEXUSSERVER:8081/service/rest/v1/search/assets/download?sort=version&repository=YOUREPO&group=YOURGROUP&name=YOURARTIFACTID&maven.extension=war' --output '/d/YOUR/ PATH/FILE.war' -u 用户:密码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM