Skip to content

Instantly share code, notes, and snippets.

@kennedyj
Created January 30, 2012 22:32
Show Gist options
  • Select an option

  • Save kennedyj/1707223 to your computer and use it in GitHub Desktop.

Select an option

Save kennedyj/1707223 to your computer and use it in GitHub Desktop.
Fetch artifacts from nexus.
#!/bin/bash
# Argument = -h -v -i groupId:artifactId:version -c classifier -p packaging -r repository
#shopt -o -s xtrace
# Define Nexus Configuration
NEXUS_BASE=http://repository.example.com:8081/nexus
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
-q Quiet
-i GAV coordinate groupId:artifactId:version
-c Artifact Classifier
-p Artifact Packaging
-o Output to a file
EOF
}
# Read in Complete Set of Coordinates from the Command Line
GROUP_ID=
ARTIFACT_ID=
VERSION=
CLASSIFIER=""
PACKAGING=war
REPO=
VERBOSE=0
QUIET=0
OUTPUT_FILE=
CURL_OPTS=
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
;;
q)
QUIET=1
;;
o)
OUTPUT_FILE=$OPTARG
;;
?)
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
# Define default values for optional components
# If the version requested is a SNAPSHOT use snapshots, otherwise use releases
if [[ "$VERSION" =~ ".*SNAPSHOT" ]]
then
: ${REPO:="snapshots"}
else
: ${REPO:="releases"}
fi
if [[ -n $OUTPUT_FILE ]]
then
CURL_OPTS="-o $OUTPUT_FILE $CURL_OPTS"
fi
if [[ "$VERBOSE" -eq "1" ]]
then
CURL_OPTS="-v $CURL_OPTS"
fi
if [[ "$QUIET" -eq "1" ]]
then
CURL_OPTS="-sS $CURL_OPTS"
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 )
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}"
echo "Fetching Artifact from $REDIRECT_URL..." >&2
curl ${CURL_OPTS} -L ${REDIRECT_URL}
@kennedyj
Copy link
Copy Markdown
Author

Added support for verbose, quiet, and file output

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment