Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Retrieve a Maven artifact from Nexus without using Maven
#!/bin/bash
# Name: Nexus MVN Retrieval
# Script: nexartrel.sh
# Version: 0.0.3
# Author: Adam Whitlock [adam@adamwhitlock.com]
# Command line options to be passed
function options {
while getopts "u:p:b:n:r:g:l:v:" opt; do
case ${opt} in
u ) USERNAME="${OPTARG}";;
p ) PASSWORD="${OPTARG}";;
b ) BASEURL="${OPTARG}";;
n ) NAME="${OPTARG}";;
r ) REPOSITORY="${OPTARG}";;
g ) GROUPNAME="${OPTARG}";;
l ) LATEST="${OPTARG}";;
v ) VERSION="${OPTARG}";;
\? ) usage
;;
esac
done
}
# Usage of Nexus script
function usage {
cat <<EOM
Purpose: Retrieve Maven artifacts from Nexus 3
Usage: $(basename $0)
-u Username for Nexus
-p Password for Nexus
-b Base URL for Nexus (ex: "https://build.company.com/nexus/repository/")
-n Name of artifact (ex: "domain")
-r Repository URL (ex: "maven-snapshots")
-g Groupname (ex: "com.companyname.service")
-l Use the LATEST version of the artifact
-v Specify the VERSION of the artifact (not used with LATEST)
EOM
exit 0
}
# Main method for nexartrel
function main {
# Provide usage and command line options
if [[ $# -lt 1 ]]; then
usage
exit 0
else
options $@
fi
# Setup credentials if provided
if [[ ! -z $USERNAME || ! -z $PASSWORD ]]; then
CREDENTIALS="-u ${USERNAME}:${PASSWORD}"
fi
# Create tempfile
TEMPFILE=$(mktemp ./nexus_latest.XXXXXX)
# Retrieve "LATEST" artifact version from Nexus
if [[ $LATEST == "true" ]]; then
curl ${CREDENTIALS} ${BASEURL}/${REPOSITORY}/${GROUPNAME//.//}/${NAME}/maven-metadata.xml > /dev/null 2>&1 -o ${TEMPFILE}
LATEST=$(cat ${TEMPFILE} | grep "<latest>.*</latest>" | sed -e "s#\(.*\)\(<latest>\)\(.*\)\(</latest>\)\(.*\)#\3#g")
curl ${CREDENTIALS} ${BASEURL}/${REPOSITORY}/${GROUPNAME//.//}/${NAME}/${LATEST}/maven-metadata.xml > /dev/null 2>&1 -o ${TEMPFILE}
VERSION_FULL=$(cat ${TEMPFILE} | sort | grep "<value>" | tail -1 | sed -e "s#\(.*\)\(<value>\)\(.*\)\(</value>\)\(.*\)#\3#g")
ARTIFACT_URL="${BASEURL}/${REPOSITORY}/${GROUPNAME//.//}/${NAME}/${LATEST}/${NAME}-${VERSION_FULL// /}.jar"
echo $ARTIFACT_URL
else
curl ${CREDENTIALS} ${BASEURL}/${REPOSITORY}/${GROUPNAME//.//}/${NAME}/maven-metadata.xml > /dev/null 2>&1 -o ${TEMPFILE}
VERSION=$(grep "${VERSION}" ${TEMPFILE} | head -1 | sed -e "s#\(.*\)\(<version>\)\(.*\)\(</version>\)\(.*\)#\3#g")
# Strip out latest if it's actually the latest version
VERSION=$(echo $VERSION | sed -e "s#\(.*\)\(<latest>\)\(.*\)\(</latest>\)\(.*\)#\3#g")
curl ${CREDENTIALS} ${BASEURL}/${REPOSITORY}/${GROUPNAME//.//}/${NAME}/${VERSION}/maven-metadata.xml > /dev/null 2>&1 -o ${TEMPFILE}
VERSION_FULL=$(cat ${TEMPFILE} | sort | grep "<value>" | tail -1 | sed -e "s#\(.*\)\(<value>\)\(.*\)\(</value>\)\(.*\)#\3#g")
echo $VERSION_FULL
ARTIFACT_URL="${BASEURL}/${REPOSITORY}/${GROUPNAME//.//}/${NAME}/${VERSION}/${NAME}-${VERSION_FULL// /}.jar"
echo $ARTIFACT_URL
fi
# Cleanup
rm -f ${TEMPFILE}
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment