Created
July 28, 2017 07:44
-
-
Save ansig/077aafa4cff98f2b6231a5ba983af76d to your computer and use it in GitHub Desktop.
This is a Bash script for fetching artifacts from Nexus via the command line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
######################################################################################################## | |
# This is a utility script to fetch artifacts from a Nexus server. It is based on an example # | |
# from Sonartype Nexus: http://blog.sonatype.com/2011/01/downloading-artifacts-from-nexus-with-bash/ # | |
######################################################################################################## | |
set -e | |
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 (Default: 0) | |
-n Nexus base URL including protocol (Required) | |
-i GAV coordinate groupId:artifactId:version (Required) | |
-c Artifact Classifier (Default: empty) | |
-p Artifact Packaging (Default: jar) | |
EOF | |
} | |
NEXUS_BASE= | |
GROUP_ID= | |
ARTIFACT_ID= | |
VERSION= | |
CLASSIFIER="" | |
PACKAGING="jar" | |
REPO= | |
VERBOSE=0 | |
while getopts "hvn:i:c:p:" OPTION | |
do | |
case $OPTION in | |
h) | |
usage | |
exit 1 | |
;; | |
n) | |
NEXUS_BASE=$OPTARG | |
;; | |
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 [ ${VERBOSE} == 1 ]; then | |
set -x | |
fi | |
if [[ -z $NEXUS_BASE ]] || [[ -z $GROUP_ID ]] || [[ -z $ARTIFACT_ID ]] || [[ -z $VERSION ]] | |
then | |
echo "BAD ARGUMENTS: Either Nexus URL, groupId, artifactId, or version was not supplied" >&2 | |
usage | |
exit 1 | |
fi | |
if [[ "${VERSION}" =~ .*SNAPSHOT ]] | |
then | |
: ${REPO:="snapshots"} | |
else | |
: ${REPO:="releases"} | |
fi | |
REDIRECT_URL=${NEXUS_BASE}${REST_PATH}${ART_REDIR} | |
PARAM_KEYS=( g a v r p c ) | |
PARAM_VALUES=( $GROUP_ID $ARTIFACT_ID $VERSION $REPO $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}" | |
OUTPUT_FILENAME="output.tmp" | |
echo "Fetching Artifact from $REDIRECT_URL" >&2 | |
RESPONSE_CODE=`curl -sS -L -o ${OUTPUT_FILENAME} -w "%{http_code}" ${REDIRECT_URL}` | |
if [ ! ${RESPONSE_CODE} == "200" ]; then | |
cat ${OUTPUT_FILENAME} | |
rm -f ${OUTPUT_FILENAME} | |
exit 1 | |
else | |
mv ${OUTPUT_FILENAME} "${ARTIFACT_ID}.${PACKAGING}" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment