Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/bash -e
# Argument = -h -v -i groupId:artifactId:version -c classifier -p packaging
#shopt -o -s xtrace
# Define Nexus Configuration
NEXUS_BASE=http://repository.example.com:8081/nexus #this URL shouldn't end in a /
REST_PATH=/service/local
ART_REDIR=/artifact/maven/redirect
usage()
{
cat <<EOF
usage: $0 options
This script will fetch an artifact from a Nexus server using the Nexus REST redirect service.
OPTIONS:
-h Show this message
-v Verbose
-i GAV coordinate groupId:artifactId:version
-c Artifact Classifier
-p Artifact Packaging
EOF
}
# Read in Complete Set of Coordinates from the Command Line
GROUP_ID=
ARTIFACT_ID=
VERSION=
CLASSIFIER=""
PACKAGING=war
VERBOSE=0
while getopts "hvi:c:p:" OPTION
do
case $OPTION in
h)
usage
exit 1
;;
i)
OIFS=$IFS
IFS=":"
GAV_COORD=( $OPTARG )
GROUP_ID=${GAV_COORD[0]}
ARTIFACT_ID=${GAV_COORD[1]}
VERSION=${GAV_COORD[2]}
IFS=$OIFS
;;
c)
CLASSIFIER=$OPTARG
;;
p)
PACKAGING=$OPTARG
;;
v)
VERBOSE=1
;;
?)
usage
exit
;;
esac
done
if [[ -z $GROUP_ID ]] || [[ -z $ARTIFACT_ID ]] || [[ -z $VERSION ]]
then
echo "BAD ARGUMENTS: Either groupId, artifactId, or version was not supplied" >&2
usage
exit 1
fi
# Construct the base URL
REDIRECT_URL=${NEXUS_BASE}${REST_PATH}${ART_REDIR}
# Generate the list of parameters
PARAM_KEYS=( g a v r p c )
#snapshots/releases may be restricted from downloading. Just use 'public'
PARAM_VALUES=( $GROUP_ID $ARTIFACT_ID $VERSION public $PACKAGING $CLASSIFIER )
PARAMS=""
for index in ${!PARAM_KEYS[*]}
do
if [[ ${PARAM_VALUES[$index]} != "" ]]
then
PARAMS="${PARAMS}${PARAM_KEYS[$index]}=${PARAM_VALUES[$index]}&"
fi
done
REDIRECT_URL="${REDIRECT_URL}?${PARAMS}"
exec 3>&1
HTTP_STATUS=`curl -sS -L -w "%{http_code}" -o >(cat >&3) ${REDIRECT_URL}`
if [ "$HTTP_STATUS" != 200 ]
then
echo "Invalid HTTP Status $HTTP_STATUS" >&2
exit 1
fi
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.