Skip to content

Instantly share code, notes, and snippets.

@suicide
Last active September 10, 2023 14:27
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save suicide/b9d3a4f26a68f68de433 to your computer and use it in GitHub Desktop.
Save suicide/b9d3a4f26a68f68de433 to your computer and use it in GitHub Desktop.
Downloads latest artifact version from artifactory
#!/bin/bash
# downloads latest version of an artifact from artifactory
set -e
usage(){
echo "Usage: $*" >&2
exit 64
}
repo=""
group=""
artifact=""
classifier=""
while getopts r:g:a:c: OPT; do
case "${OPT}" in
r) repo="${OPTARG}";;
g) group="${OPTARG}";;
a) artifact="${OPTARG}";;
c) classifier="${OPTARG}";;
esac
done
shift $(( $OPTIND - 1 ))
if [ -z "${repo}" ] || [ -z "${group}" ] || [ -z "${artifact}" ]; then
usage "-r REPOSITORY -g GROUPID -a ARTIFACTID [-c CLASSIFIER]"
fi
# Maven artifact location
ga=${group//./\/}/$artifact
repopath=$repo/$ga
version=`curl -s $repopath/maven-metadata.xml | grep latest | sed "s/.*<latest>\([^<]*\)<\/latest>.*/\1/"`
build=`curl -s $repopath/$version/maven-metadata.xml | grep '<value>' | head -1 | sed "s/.*<value>\([^<]*\)<\/value>.*/\1/"`
jar=""
if [ -z "${classifier}" ]; then
jar=$artifact-$build.jar
else
jar=$artifact-$build-$classifier.jar
fi
url=$repopath/$version/$jar
# Download
# echo $url
curl $url
@doronshai
Copy link

Great Script - thanks!

@dbkegley
Copy link

Really helpful, thanks!

@ctataryn
Copy link

Thanks a tonne!

@ramdas80
Copy link

sed need to have flag -E ? well, I was getting error \1 not defined in the RE without -E

@gavenkoa
Copy link

Responsibility to generate maven-metadata.xml is on Maven/Gradle/Jenkins side. So this solution isn't universal...

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