How to download an artifact from Nexus repo via curl using REST API

Printer-friendly versionPDF version

So I can't take credit for the curl solution. The following script was a result of various stackoverflow searches to see why it wasn't working. In the end the curl command had to look like below and work in 2 steps to download the artifact I needed.

The below script assumes the following:

  1. A nexus base url (REPO_BASE_URL), you need to find this out. For me it was differnet than advertised in documentation from nexus as something like this "https://myNexusServer/service/rest/beta/search/assets/download"
  2. A space seperated string of multiple artifacts that you want to download in this format "ahsanscorner.releases:com.ahsanscorner.tools:tool1:1.0.0 ahsanscorner.releases:com.ahsanscorner.tools:tool2" where
    1. the first part is the repository name ( this is an extra parameter to the real maven co-ordinates that I needed to download the artifact)
    2. than the next 3 portions are the maven co-ordinates.

(You can get all the above from nexus UI)

Also ignore my template variables that I use from puppet to run my script. You need real values in place of <%= XXX %>

#!/bin/bash
MAVEN_COORDINATES=( &lt;%= @maven_coordinates%&gt; )
REPO_USERNAME=&lt;%= @repo_username%&gt;
REPO_SECRET=&lt;%= @repo_secret%&gt;
REPO_BASE_URL=&lt;%= @repo_base_url %&gt;
# Push current directory in stack
pushd .
# Create temporary folder to download artifacts to
rm -rf ./temp
mkdir temp
cd temp
for MAVEN_COORDINATE in "${MAVEN_COORDINATES[@]}"
do
#Convert string to array
IFS=':' read -ra SPLITTED_MVN_COORDINATES &lt;&lt;&lt; "$MAVEN_COORDINATE"
REPOSITORY=${SPLITTED_MVN_COORDINATES[0]}
GROUPID=${SPLITTED_MVN_COORDINATES[1]}
ARTIFACTID=${SPLITTED_MVN_COORDINATES[2]}
VERSION=${SPLITTED_MVN_COORDINATES[3]}
DOWNLOAD_URL="$(curl -u ${REPO_USERNAME}:${REPO_SECRET} "${REPO_BASE_URL}?repository=${REPOSITORY}&amp;version=${VERSION}&amp;maven.groupId=${GROUPID}&amp;maven.artifactId=${ARTIFACTID}" -s -L -I -o /dev/null -w '%{url_effective}')"
echo "Downloading artifact from ${DOWNLOAD_URL}"
curl -u ${REPO_USERNAME}:${REPO_SECRET} -X GET "$DOWNLOAD_URL" -O -J
done
popd
# Unpack the downloaded artifacts.
for filename in ./temp/*.zip; do
unzip -o "${filename#/}"
done
# Cleanup temp folder
rm -rf ./temp

 

 

Tags:

Top level category:

Add new comment