MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

How to download package from Nexus using curl?

With curl you can download a file from Sonatype Nexus Repository 3 in two ways. If you know the exact path, request /repository/<repo>/<path>. If you only know the coordinates, call the REST search API at /service/rest/v1/search/assets/download to fetch the latest matching file. This guide explains the gist examples above, fixes the ones that are broken or meant for the retired Nexus 2 API, and gives you scripts that work on current Nexus 3 servers.


Three ways Nexus 3 serves a file

The examples above mix several Nexus endpoints, and some of them belong to Nexus 2. Before choosing a command, decide which situation you are in.

You knowEndpointBest for
The exact file path/repository/<repo>/<path>Releases, raw files, fixed versions
Coordinates only (group, artifact, base version)/service/rest/v1/search/assets/downloadLatest release or latest snapshot in CI
Nothing; you want everything/service/rest/v1/assets?repository=<repo>Backups and migrations (paginated)

Note: URLs containing /service/local/artifact/maven/redirect or /service/local/artifact/maven/content belong to the Nexus 2 REST API. Nexus Repository 3 does not have them, so the PowerShell script and the first wget line above return 404 on a Nexus 3 server.

Prerequisites and safe credentials

  • A Nexus 3 user with read and browse privileges on the repository, for example nx-repository-view-maven2-maven-releases-read and -browse. If anonymous access is enabled and the repository allows reads, you do not need credentials.
  • curl, plus jq for parsing JSON (apt install jq or dnf install jq).
  • The base URL of your server. On Nexus 3 the default context path is /, so URLs look like https://nexus.example.com/repository/.... You only need /nexus/... if an administrator configured that context path.

Passing -u user:password on the command line exposes the password in shell history and in the process list. The hard-coded Authorization: Basic YWRtaW46YWRtaW4= header above has the same problem: Base64 is an encoding, not encryption, and that value decodes to admin:admin. A .netrc file is a safer choice:

cat > ~/.netrc <<'EOF'
machine nexus.example.com
login ci-reader
password change-me
EOF
chmod 600 ~/.netrc

export NEXUS_URL=https://nexus.example.comCode language: PHP (php)

With this file in place, curl -n (--netrc) sends the credentials automatically. All the examples below use -n.

Download a file by its direct URL

Every asset in Nexus 3 has a URL of the form <base>/repository/<repo-name>/<path>. In Maven repositories the path follows the standard layout: the dots in the groupId become slashes, followed by the artifactId, the version, and then the file name.

# Maven release: com.example:my-app:1.2.0 (jar)
curl -fL -n -O \
  "$NEXUS_URL/repository/maven-releases/com/example/my-app/1.2.0/my-app-1.2.0.jar"

# Raw repository: file uploaded to directory 'devops'
curl -fL -n -o devops-1.0.0.0.zip \
  "$NEXUS_URL/repository/devopsrepo/devops/devops-1.0.0.0.zip"Code language: PHP (php)
  • -f makes curl exit with an error on HTTP 4xx/5xx instead of saving the error page as your artifact.
  • -L follows redirects.
  • -O keeps the remote file name. -o <file> sets a name of your choice.
  • --output-dir, used above, requires curl 7.73.0 or later. On older curl, use -o /some-dir/file.zip instead.

Note: Maven snapshots are stored with timestamped file names such as my-app-1.0-20240101.120000-3.jar, so you usually cannot guess their direct URL. For snapshots, use the search API in the next section.

Download the latest version with the search API

The search/assets/download endpoint runs a search and answers with a 302 redirect to the matching asset, so a single curl -L downloads the file. This is the cleanest replacement for Download.sh above:

curl -fL -n -G -o my-artifact.jar \
  "$NEXUS_URL/service/rest/v1/search/assets/download" \
  -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 \
  -d sort=version \
  -d direction=descCode language: JavaScript (javascript)

-G appends the -d values to the URL as a query string, so you do not need -X GET. If more than one asset matches, the endpoint returns 400 Bad Request. Adding sort makes it pick the first result instead. Make the filters specific (extension and classifier) so that a POM or a sources jar is never chosen by mistake. To get the newest release, drop maven.baseVersion, point repository at your releases repository, and keep sort=version.

When you also want to verify the file, call search/assets instead. It returns JSON that contains the download URL and the checksums:

resp=$(curl -fsSL -n -G "$NEXUS_URL/service/rest/v1/search/assets" \
  -d repository=maven-releases \
  -d maven.groupId=com.example \
  -d maven.artifactId=my-app \
  -d version=1.2.0 \
  -d maven.extension=jar)

url=$(jq -r '.items[0].downloadUrl' <<<"$resp")
sha1=$(jq -r '.items[0].checksum.sha1' <<<"$resp")

curl -fsSL -n -o my-app.jar "$url"
echo "$sha1  my-app.jar" | sha1sum -c -Code language: JavaScript (javascript)
my-app.jar: OKCode language: CSS (css)

Download an entire repository (corrected script)

DownloadAll.sh above has the right idea: page through /service/rest/v1/assets with continuationToken, then download each downloadUrl. It breaks in practice, though. $curFolder is never set, so after the first file the script cds into your home directory. The path stripping only works for http://host:port URLs, and -k turns off TLS verification. The version below fixes these problems and uses jq for all JSON parsing:

#!/usr/bin/env bash
# Usage: NEXUS_URL=https://nexus.example.com REPO=maven-releases ./nexus-export.sh
set -euo pipefail

: "${NEXUS_URL:?set NEXUS_URL}"
: "${REPO:?set REPO}"
OUT_DIR="./${REPO}"
LIST="${REPO}-artifacts.txt"
: > "$LIST"                      # start with an empty list on every run

# 1. Collect all download URLs, following pagination
token=""
while :; do
  url="${NEXUS_URL}/service/rest/v1/assets?repository=${REPO}"
  [ -n "$token" ] && url="${url}&continuationToken=${token}"
  resp=$(curl -fsSL -n -H 'Accept: application/json' "$url")
  jq -r '.items[].downloadUrl' <<<"$resp" >> "$LIST"
  token=$(jq -r '.continuationToken // empty' <<<"$resp")
  [ -z "$token" ] && break
done
echo "Found $(wc -l < "$LIST") assets"

# 2. Download each asset, keeping the repository layout
fail=0
while IFS= read -r dl; do
  rel="${dl#*/repository/${REPO}/}"
  dest="${OUT_DIR}/${rel}"
  mkdir -p "$(dirname "$dest")"
  code=$(curl -sSL -n -o "$dest" -w '%{http_code}' "$dl") || true
  if [ "$code" = "200" ]; then
    echo "OK    $rel"
  else
    echo "FAIL  $rel (HTTP $code)" >&2
    rm -f "$dest"; fail=$((fail+1))
  fi
done < "$LIST"

echo "Done with $fail failures"
[ "$fail" -eq 0 ]Code language: PHP (php)
  • jq -r '.items[].downloadUrl' replaces the four sed passes and the temporary files.
  • .continuationToken // empty prints nothing when the token is null, which ends the loop on the last page.
  • ${dl#*/repository/${REPO}/} strips everything up to the repository name, so the script works with HTTP or HTTPS, with or without a port, and with any context path.
  • -w '%{http_code}' reports the status code directly, so there is no need to write a header file and parse it.

Downloading on Windows with PowerShell

The download.ps1 script above calls the Nexus 2 redirect endpoint and then parses the redirect out of the response body. On Nexus 3, call the search download endpoint and let Invoke-WebRequest follow the redirect:

$nexus   = "https://nexusrepo.example.com"
$pair    = "{0}:{1}" -f $env:NEXUS_USER, $env:NEXUS_PASS
$token   = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($pair))
$headers = @{ Authorization = "Basic $token" }

$query = "repository=my-snapshot&maven.groupId=my.group.id&maven.artifactId=my.artifact.id" +
         "&maven.baseVersion=1.10.0-SNAPSHOT&maven.classifier=win32.win32.x86_64" +
         "&maven.extension=zip&sort=version"

Invoke-WebRequest -Uri "$nexus/service/rest/v1/search/assets/download?$query" `
  -Headers $headers -OutFile "MyApp.zip"Code language: PHP (php)

Note: In PowerShell 7, add -PreserveAuthorizationOnRedirect so that the Authorization header is still sent after the 302. In Windows PowerShell 5.1, curl is an alias for Invoke-WebRequest. Type curl.exe to run the real curl that ships with current Windows versions.

Corrections to the examples above

  • nexus-download.sh starts with uploads. The first two curl -X POST .../components commands publish a file to a raw repository; they do not download anything. Keep them as an upload step, and remove the trailing space in -F 'raw.directory=devops '. Otherwise the directory name may include the space.
  • Nexus 2 endpoints. download.ps1 and the first wget line use /service/local/artifact/maven/..., which does not exist in Nexus 3. Use /service/rest/v1/search/assets/download instead.
  • Fragile JSON parsing in Download.sh. The grep -Po pattern depends on the exact whitespace of the JSON output. If it matches more than one URL, xargs downloads all of them into the same file. Use jq or the search/assets/download endpoint.
  • /nexus/ in URLs. That was the default path in Nexus 2. On Nexus 3 it is only correct if your server was configured with that context path.
  • --output-dir needs curl 7.73.0 or later.
  • wget with credentials. --password=... stores the password in shell history. Use --ask-password, or put the credentials in ~/.wgetrc with restricted permissions.

Troubleshooting common errors

SymptomLikely causeFix
HTTP 401Wrong credentials, or anonymous access disabledCheck .netrc and test with curl -n -I <url>
HTTP 403User lacks read/browse privilege on the repositoryGrant nx-repository-view-<format>-<repo>-read and -browse
HTTP 404Wrong repository name or path, or wrong /nexus context pathCopy the path from the asset in the Nexus UI (Browse view)
HTTP 400 from search/assets/downloadSeveral assets match the searchAdd sort=version or more specific filters
Downloaded file is tiny HTML or JSONError page saved because -f was missing, or redirect not followedUse curl -fL, then check the file with file <name>
SSL certificate problemInternal CA not trustedUse --cacert ca.pem rather than -k
option --output-dir: is unknowncurl older than 7.73.0Use -o dir/filename

Frequently asked questions

How do I download an artifact from Nexus 3 with curl?

Request <nexus-url>/repository/<repo>/<path-to-file> with curl -fL -O, adding credentials with -n (.netrc) or -u if anonymous access is disabled. In the Nexus UI, the Browse view shows the exact path of each asset.

How do I download the latest version of an artifact from Nexus?

Call /service/rest/v1/search/assets/download with the repository, Maven coordinates and sort=version, and use curl -L so that curl follows the 302 redirect to the newest matching file. For snapshots, filter by maven.baseVersion, for example 1.0-SNAPSHOT.

Why does search/assets/download return 400 Bad Request?

The search matched more than one asset, for example the jar, the POM and the sources jar. Add maven.extension and maven.classifier filters, or add a sort parameter so that the endpoint picks the first result.

Can I use wget instead of curl to download from Nexus?

Yes. Use the same /repository/<repo>/<path> URL with wget --user=<name> --ask-password <url>. The Nexus 2 /service/local/artifact/maven/content URLs will not work on Nexus 3 with either tool.

Does the Nexus REST API return all assets in one response?

No. /service/rest/v1/assets and /service/rest/v1/search/assets return results one page at a time, with a continuationToken. Pass the token back in the next request until it is null.

Related tutorials

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

How to upload Package to Nexus using jenkins Pipeline

To upload a build artifact to Sonatype Nexus Repository from a Jenkins Pipeline, install the Nexus Platform Plugin, register your Nexus server in Jenkins, and then call…

Read More

Install Sonatype Nexus with Docker: RM2/RM3 Commands, Validation Steps, and Important Caveats

A structured, production-minded rewrite of the original 2016 Docker setup for Sonatype Nexus Repository Manager 2 and 3. Includes command examples, a workflow diagram, and clear warnings about outdated tags/endpoints.

Read More

Install Sonatype Nexus Repository OSS (Community) on Linux: Updated Step-by-Step Guide

This updated guide modernizes a legacy Nexus Community install post into a clear Linux runbook. It covers Java setup, Nexus download/start, first login checks, control commands, and upgrade-safe practices.

Read More

Sonatype Nexus Repository Manager: A Practical DevOps Introduction

Sonatype Nexus helps teams centralize dependency downloads and internal artifact publishing. This guide shows how Nexus fits into CI/CD, when to use hosted/proxy/group repos, and how to configure common build clients.

Read More

Nexus Training in India: Course Scope, Hands-On Labs, and Registration Details

This revised guide summarizes the original Nexus training offering, including audience, delivery model, and syllabus topics. It also adds a practical lab workflow and sample Nexus/Maven tasks to help learners prepare.

Read More

Sonar Maven Plugin 1.0-beta-2: What Changed for Archiva/Nexus-Based Builds

This historical release note introduced simpler Sonar Maven Plugin behavior when using repository managers like Archiva or Nexus. Hereโ€™s what changed from beta-1, how compatibility works with Sonar versions, and how to validate legacy CI behavior.

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x