Nexus artifact download using CURL

Viewed 87471

I'm trying to download an artifact uploaded to nexus using CURL. 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

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? 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. I'm new to nexus and any help is greatly appreciated.

Thanks

7 Answers

In newer versions of nexus you can:

  1. Use curl to search for a maven artifact in your nexus via the new REST-API
  2. Parse the json-response to extract a download-link
  3. Use curl to download the artifact

On bash this boils down to:

$ 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. Finally you pass the extracted URL to curl again to download your artifact. (tested with 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. So the /service/local/artifact/maven/ is no longer available in Nexus3.

An alternative way - independent of the Nexus version - is using 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=./

where "mvnpackaging" can be 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.

Add -u usr:pw if needed.

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

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

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

Related