Author

Author


Rajesh Kumar

Rajesh Kumar

DevOps@RajeshKumar.xyz

DevOps@RajeshKumar.xyz

Support my work @Patron! Support my work @Patron!

Downloading artifact through REST/API/Wget/Curl from Nexus2 & Nexus3

Nexus 2.x

Nexus 2.x had a REST API to download artifacts based on some Maven GAV co-ordinates

123456789101112
wget "http://local:8081/service/local/artifact/maven/redirect?g=com.mycompany&a=my-app&v=LATEST" --content-disposition

wget --user=username --password=password "http://:/nexus/service/local/artifact/maven/content?g=&a=&v=&r=snapshots"


wget --user=userid --password=password 'https://nexusurl:8081/nexus/repository/////' -O ${WORKSPACE}/

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.strong>

Solution 1

From Maven 3, support for uniqueVersion is disabled and when you distribute your snapshots by publishing them on nexus you end up with snapshot names ending with timestamps. Suddenly your QA or Integration server scripts start failing if you are getting war file from nexus repository as there is no unique name now.

To get the latest version of snapshot you can use a service available from Nexus as

123456789101112
wget -O my-services.war http://nexus.myorg.net/nexus/service/local/artifact/maven/redirect?r=snapshots\&g=my.company.product.services\&a=my-services\&v=1.0-SNAPSHOT\&p=war

Where 
r is the id of the repository
g is the group id 
a is the artifact id
v is the version of artifact
p is the packaging type

The argument -O helps in renaming the output file to the name you want so that you have a fixed name you can use in your shell script.
Solution 2

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

12345
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 3

Here is my pure bash implementation of wget that can fetch release and discover latest version of SNAPSHOT

123456789101112131415161718192021222324252627282930313233343536373839404142
#!/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 \ | sed -e 's/\(.*\)<\/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


Nexus REST from a bash script

123
Here is good reference for Interacting with Nexus REST from a bash script
https://blog.sonatype.com/2011/01/downloading-artifacts-from-nexus-with-bash/

Avail Rajesh Kumar as trainer at 50% Discount
Puppet Online Training
Puppet Classroom TrainingEnroll Now