'Nexus artifact download using CURL

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



Solution 1:[1]

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)

Solution 2:[2]

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....

Solution 3:[3]

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.

Solution 4:[4]

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

Solution 5:[5]

The artifacts are now under URLs that are similar to:

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

Using that and some knowledge of Maven paths, you can get to artifacts.

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.

In newer versions of Nexus Repository, we've got the beginnings of our 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:

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

Solution 6:[6]

Below statement worked for me.

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

eg

curl -X GET https://sampleuser:[email protected]/repository/maven-public/public/util/demo.jar --output demo.jar

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Pierluigi Vernetto
Solution 3 Stefan K.
Solution 4
Solution 5
Solution 6 Chandan Kumar