Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nexus artifact download using CURL

Tags:

curl

nexus

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

like image 866
Nagendira Avatar asked Aug 07 '17 04:08

Nagendira


People also ask

How do I download a file from URL using curl?

How to download a file with curl command. The basic syntax: Grab file with curl run: $ curl https://your-domain/file.pdf. Get file using ftp or sftp protocol: $ curl ftp://ftp-your-domain-name/file.tar.gz.

How do I download from Nexus repository manager?

To download the latest Nexus Repository Manager OSS distribution, go to Sonatype's OSS download page and choose the compressed bundle file that suits your need from the Nexus Repository Manager OSS 2. x section. A download will begin to the latest version. Older versions can be found here if needed.


1 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)

like image 128
Fabian Braun Avatar answered Oct 09 '22 11:10

Fabian Braun