Nexus 2.x had a REST API to download artifacts like below based on some Maven GAV co-ordinates but this no longer works for Nexus 3.x
Nexus 2.x had a REST API to download artifacts based on some Maven GAV co-ordinates
wget "http://local:8081/service/local/artifact/maven/redirect?g=com.mycompany&a=my-app&v=LATEST" --content-disposition
or
curl --insecure "https://local:8081/service/local/artifact/maven/content?r=public&g=log4j&a=log4j&v=1.2.17&p=jar&c=" > log4j.jar
Nexus 3.x has no REST API to fetch artifacts.
If you can tolerate to have maven in path properly configured on system, You can replace all your REST call with a one line maven call
mvn org.apache.maven.plugins:maven-dependency-plugin:3.0.1:copy -Dartifact=log4j:log4j:1.2.17:jar -DoutputDirectory=./
This support ONLY getting releases! snapshot with unique ID are not supported
Here is my pure bash implementation of wget that can fetch release and discover latest version of SNAPSHOT
#!/bin/bash
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
IFS=$'\n\t'
groupId=$1
artifactId=$2
version=$3
# optional
classifier=${4:-}
type=${5:-jar}
repo=${6:-snapshots}
# Nexus 2
#base="http://nexus.example.com/service/local/repositories/${repo}/content"
# Nexus 3
base="http://nexus.example.com/repository/${repo}"
if [[ ${classifier} != "" ]]; then
classifier="-${classifier}"
fi
groupIdUrl="${groupId//.//}"
filename="${artifactId}-${version}${classifier}.${type}"
if [[ "${version}" == "LATEST" ]] ; then
version=$(xmllint --xpath "string(//latest)" <(curl -s "${base}/${groupIdUrl}/${artifactId}/maven-metadata.xml"))
elif [[ "${version}" == "RELEASE" ]] ; then
version=$(xmllint --xpath "string(//release)" <(curl -s "${base}/${groupIdUrl}/${artifactId}/maven-metadata.xml"))
fi
if [[ "${version}" == *SNAPSHOT* ]] ; then
timestamp=$(xmllint --xpath "string(//timestamp)" <(curl -s "${base}/${groupIdUrl}/${artifactId}/${version}/maven-metadata.xml"))
buildnumber=$(xmllint --xpath "string(//buildNumber)" <(curl -s "${base}/${groupIdUrl}/${artifactId}/${version}/maven-metadata.xml"))
curl -s -o ${filename} "${base}/${groupIdUrl}/${artifactId}/${version}/${artifactId}-${version%-SNAPSHOT}-${timestamp}-${buildnumber}${classifier}.${type}"
else
curl -s -o ${filename} "${base}/${groupIdUrl}/${artifactId}/${version}/${artifactId}-${version}${classifier}.${type}"
fi
usage
script.sh groupid artifactid version classifier type
ex:
script.sh log4j log4j 4.2.18 sources zip
script.sh log4j log4j 4.2.19 -> get jar as default
script.sh log4j log4j 4.2.19-SNAPSHOT -> get jar as default
Forked from https://gist.github.com/cedricwalter/e7739aab3d370ef83f1a13b8322e50be, updated script based on comment from @rbjorklin. Added ability to use
RELEASE
version identifier and fixed getting snapshot/release version when usingLATEST
orRELEASE
.