Skip to content

Instantly share code, notes, and snippets.

@vegaasen
Last active October 5, 2022 14:08
Show Gist options
  • Save vegaasen/49895f3f790c1da7548ae3d04bfbc977 to your computer and use it in GitHub Desktop.
Save vegaasen/49895f3f790c1da7548ae3d04bfbc977 to your computer and use it in GitHub Desktop.
Fetch artifactory artifact (maven)
#!/usr/bin/bash
##
## Read more about the definitions here regarding the REST-APIs:
## https://www.jfrog.com/confluence/display/RTF/Artifactory+REST+API
##
## Note: This script uses the maven-metadata.xml-file in order to determine the latest JAR-artifact.
##
## Script properties:
## - ARTIFACTORY = https://<artifactory-domain>/artifactory
## -- example: https://artifactory.local/artifactory
## - REPOSITORY = <your-repository>
## -- example: maven-local-snapshots
## - ARTIFACT = <your-artifact>
## -- example: awsome-api-application
## - GROUP = <group-path>
## -- example: com/vegaasen/api/awsome
##
## Script input:
## - DOWNLOAD = true|false (otherwise undefined)
##
## @author vegaasen
## @version 1.0
## @since 23.09.2016
##
# INPUT
DOWNLOAD=$1;
# ARTIFACTORY
ARTIFACTORY=
REPOSITORY=
# ARTIFACT-DETAILS
ARTIFACT=
GROUP=
ARTIFACT_PATH=$ARTIFACTORY/$REPOSITORY/$GROUP/$ARTIFACT
function log() {
echo -e "[INFO] $1";
}
function usage() {
log "";
exit 1;
}
function verifyProperties() {
if [ -z "$DOWNLOAD" ]; then
log "DOWNLOAD is undefined. Skipping fetch of artifact";
fi
if [ -z "$ARTIFACTORY" ]; then
log "Artifactory is undefined. Set variable ARTIFACTORY";
usage;
fi
if [ -z "$REPOSITORY" ]; then
log "Repository is undefined. Set variable REPOSITORY";
usage;
fi
if [ -z "$ARTIFACT" ]; then
log "Artifact is undefined. Set variable ARTIFACT";
usage;
fi
if [ -z "$GROUP" ]; then
log "Artifact group is undefined. Set variable GROUP";
usage;
fi
}
function getArtifact() {
latestVersion=`curl -k -s $ARTIFACT_PATH/maven-metadata.xml | grep latest | sed "s/.*<latest>\([^<]*\)<\/latest>.*/\1/"`
latestBuild=`curl -k -s $ARTIFACT_PATH/$latestVersion/maven-metadata.xml | grep '<value>' | head -1 | sed "s/.*<value>\([^<]*\)<\/value>.*/\1/"`
if [ -z "$latestVersion" ] || [ -z "$latestBuild" ]; then
log "Unable to define jar due to missing latestVersion ($latestVersion) or latestBuild ($latestBuild)"
exit 1;
else
log "Using latestBuild:{$latestBuild} and latestVersion:{$latestVersion}";
fi
jar=$ARTIFACT-$latestBuild.jar
artifactUrl=$ARTIFACT_PATH/$latestVersion/$jar
log "Generated url related to the artifact {$ARTIFACT} for version {$latestVersion}: {$artifactUrl}";
if [ $DOWNLOAD ] && [ $DOWNLOAD == "true" ]; then
log "Downloading the artifact. Filename: {$jar}";
curl -k -o $jar $artifactUrl;
log "File located at {`pwd`/$jar}"
else
log "Skipping download";
fi
}
log "#############################################################"
log "";
log "Fetching latest artifact"
log "Artifactory: $ARTIFACTORY";
log "Repository: $REPOSITORY";
log "Group: $GROUP";
log "Artifact: $ARTIFACT";
log "Should download: $DOWNLOAD"
log "";
log "#############################################################"
log "";
verifyProperties;
getArtifact;
log "Completed :-)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment