Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sonatype NEXUS 2 has a rest API :-) but NEXUS 3 has none/not ready, the following simulate curl/wget call

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




@rbjorklin

This comment has been minimized.

Copy link

commented Aug 30, 2017

This script also resolves LATEST.

#!/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" || "${version}" == *SNAPSHOT* ]] ; then                                                                                                                                           
  if [[ "${version}" == "LATEST" ]] ; then                                                                                                                                                                       
    version=$(xmllint --xpath "string(//latest)" <(curl -s "${base}/${groupIdUrl}/${artifactId}/maven-metadata.xml"))                                                                                            
  fi                                                                                                                                                                                                             
  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
@rodislav

This comment has been minimized.

Copy link

commented Sep 18, 2017

I need to get the snapshot and this one worked for me:
mvn org.apache.maven.plugins:maven-dependency-plugin:3.0.1:copy -Dartifact=my.id:my-app:1-SNAPSHOT:war -DoutputDirectory=./

@gfouquet

This comment has been minimized.

Copy link

commented Oct 19, 2017

Thanks for these scripts, you've saved me hours !

@itsmesiva80

This comment has been minimized.

Copy link

commented Oct 26, 2017

I am using 3.0.0 the script does not work for me, do I need to upgrade to any specific version?

@pramodpatwa1986

This comment has been minimized.

Copy link

commented Jul 16, 2018

Thanks a lot script works for me, i did little changes to extend if version number is not provided then get latest version, also rather than taking build from repository i directly took both release and snapshot from maven-public group

#!/bin/sh

repo="https://nexus.local"
groupId=$1
artifactId=$2
version=$3

classifier=$4
type=$5

if [[ $type == "" ]]; then
type="jar"
fi
if [[ $classifier != "" ]]; then
classifier="-${classifier}"
fi
groupIdUrl="${groupId//.//}"
if [[ $version == "" ]]; then
version=$(wget -q -O- --no-check-certificate "${repo}/repository/maven-public/${groupIdUrl}/${artifactId}/maven-metadata.xml" | grep -m 1 <latest> | sed -e 's/(.*)</latest>/\1/' | sed -e 's/ //g')
fi

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/maven-public/${groupIdUrl}/${artifactId}/${version}/${artifactId}-${version}${classifier}.${type}" -O ${filename} -k
else
versionTimestamped=$(wget -q -O- --no-check-certificate "${repo}/repository/maven-public/${groupIdUrl}/${artifactId}/${version}/maven-metadata.xml" | grep -m 1 <value> | sed -e 's/(.*)</value>/\1/' | sed -e 's/ //g')

wget --no-check-certificate "${repo}/repository/maven-public/${groupIdUrl}/${artifactId}/${version}/${artifactId}-${versionTimestamped}${classifier}.${type}" -O ${filename}
fi

@rzr999

This comment has been minimized.

Copy link

commented Jul 16, 2018

Adapted code of @rbjorklin to work with sh, those parentheses near xmllint are not so compatible across sh.
Code is the same. Great work extracting the build numbers.

#!/bin/sh

# 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:-war}                                                                                                                                                                                                   
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" || "${version}" == *SNAPSHOT* ]] ; then                                                                                                                                           
  if [[ "${version}" == "LATEST" ]] ; then                                                                                                                                                                       
    version=$(xmllint --xpath "string(//latest)" <(curl -s "${base}/${groupIdUrl}/${artifactId}/maven-metadata.xml"))                                                                                            
  fi                                                                                                                                                                                                             
  timestamp=`curl -s "${base}/${groupIdUrl}/${artifactId}/${version}/maven-metadata.xml" | xmllint --xpath "string(//timestamp)" -`
  buildnumber=`curl -s "${base}/${groupIdUrl}/${artifactId}/${version}/maven-metadata.xml" | xmllint --xpath "string(//buildNumber)" -`
  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

@naveenkashyapdv

This comment has been minimized.

Copy link

commented Jan 9, 2019

Thanks @rzr999. saved me !!

@thoaingan

This comment has been minimized.

Copy link

commented Jul 14, 2019

Does anyone know if there’s a public Nexus Repository that I can try this code on without having to spin up my own Nexus instance?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.