18

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

Nagendira
  • 201
  • 1
  • 3
  • 6

7 Answers7

21

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)

Fabian Braun
  • 3,612
  • 1
  • 27
  • 44
  • 2
    They have also this endpoint `GET /service/rest/v1/search/assets/download` that does the following: <> 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'` [Click here for more details](https://help.sonatype.com/repomanager3/rest-and-integration-api/search-api) – nicolimo86 Aug 14 '19 at 13:36
  • 1
    Indeed, that looks much better. You should file another answer to this question @nicolimo86 – Fabian Braun Aug 15 '19 at 07:07
9

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

Pierluigi Vernetto
  • 1,954
  • 1
  • 25
  • 27
9

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.

Stefan K.
  • 7,701
  • 6
  • 52
  • 64
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

nicolimo86
  • 718
  • 11
  • 22
2

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
Chandan Kumar
  • 1,066
  • 10
  • 16
1

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

DarthHater
  • 3,222
  • 25
  • 26
0

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