Fetching artifact programmatically through REST/API fro Nexus2/3
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
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
Nexus 3.x has no REST API to fetch artifacts.
Solution 1
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
Solution 2
Here is my pure bash implementation of wget that can fetch release and discover latest version of SNAPSHOT
#!/bin/sh
repo="https://nexus.url.com"
groupId=$1
artifactId=$2
version=$3
# optional
classifier=$4
type=$5
if [[ $type == "" ]]; then
type="jar"
fi
if [[ $classifier != "" ]]; then
classifier="-${classifier}"
fi
groupIdUrl="${groupId//.//}"
filename="${artifactId}-${version}${classifier}.${type}"
if [[ ${version} == *"SNAPSHOT"* ]]; then repo_type="snapshots"; else repo_type="releases"; fi
if [[ $repo_type == "releases" ]]
then
wget --no-check-certificate "${repo}/repository/releases/${groupIdUrl}/${artifactId}/${version}/${artifactId}-${version}${classifier}.${type}" -O ${filename} -k
else
versionTimestamped=$(wget -q -O- --no-check-certificate "${repo}/repository/snapshots/${groupIdUrl}/${artifactId}/${version}/maven-metadata.xml" | grep -m 1 \<value\> | sed -e 's/<value>\(.*\)<\/value>/\1/' | sed -e 's/ //g')
wget --no-check-certificate "${repo}/repository/snapshots/${groupIdUrl}/${artifactId}/${version}/${artifactId}-${versionTimestamped}${classifier}.${type}" -O ${filename}
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
This script also resolves
LATEST
.