#!/bin/bash set -e set -u export APPSH_HOME=$(cd $(dirname "$0")/.. && pwd) . $APPSH_HOME/lib/common # HEADER END usage_text() { echo "usage: $usage_app init -r " echo "usage: $usage_app resolve-version" echo "usage: $usage_app download-version -v -f " echo "Maven url can be one of: :: and" echo ":::" } slash() { echo $1 | sed "s,\.,/,g" } # TODO: look in the local repository first get() { local url=$1; shift local file=$1; shift if [[ $url == file://* ]] then get_file "$url" "$file" else get_http "$url" "$file" fi } get_file() { url=${1:7} cp "$url" "$2" } get_http() { curl -o $file $url -D curl.tmp exit=`grep "^HTTP/[0-9]\.[0-9] 200 .*" curl.tmp >/dev/null; echo $?` head=`head -n 1 curl.tmp` rm -f curl.tmp if [ "$exit" != 0 ] then echo "Unable to download $url: $head" >&2 exit 1 fi } download_artifact() { local file="$1"; shift local url="$1"; shift echo "Downloading $url.md5" get $url.md5 $file.md5 local expected_md5="`cat $file.md5`" if [ -r $file ] then if [ "$expected_md5" == "`calculate_md5 $file`" ] then echo "Artifact already downloaded." else rm -f "$file" fi return 0 fi echo "Downloading artifact: $url" get $url $file local actual_md5="`calculate_md5 $file`" if [ "$expected_md5" == "$actual_md5" ] then echo "Artifact downloaded." else echo "Invalid checksum. Expected $expected_md5, got $actual_md5" >&2 exit 1 fi } which() { /usr/bin/which "$1" 2>/dev/null } # Wrapper to cache the lookup of the xmlstarlet command. # Remember that xmlstarlet on at least SLES requires single quotes # instead of double quotes when building the selector, e.g.: # use [text()='zip'] instead of [text()="zip"]. _xmlstarlet="" xmlstarlet() { if [ ! -z "$_xmlstarlet" ] then "$_xmlstarlet" "$@" return fi _xmlstarlet="`which xmlstarlet`" if [ ! -z "$_xmlstarlet" ] then "$_xmlstarlet" "$@" return fi _xmlstarlet="`which xml`" if [ ! -z "$_xmlstarlet" ] then "$_xmlstarlet" "$@" return fi echo "Could not find xmlstarlet." >&2 exit 1 } resolve_version() { local group_id=`app-conf get maven.group_id` local artifact_id=`app-conf get maven.artifact_id` local classifier=`app-conf get app.classifier` local version=`app-conf get app.version` repo=`app-conf get maven.repo` if [[ ! $version == *-SNAPSHOT ]] then app-conf set app.resolved_version "$version" exit 0 fi echo "Resolving Maven version $version..." resolve_snapshot $group_id $artifact_id $version } resolve_snapshot() { local group_id=$1; shift local artifact_id=$1; shift local version=$1; shift repo=`app-conf get maven.repo` local group_id_slash=`slash $group_id` local base_path=$group_id_slash/$artifact_id/$version mkdir -p .app/cache/$base_path local l=.app/cache/$base_path/maven-metadata.xml local r=$repo/$base_path/maven-metadata.xml get $r $l x=$(xmlstarlet sel -t -m '/metadata/versioning/snapshot' -v timestamp -o ' ' -v buildNumber $l) set -- $x if [[ $# != 2 ]] then fatal "Unable extract a useful timestamp from maven-metadata.xml." fi snapshot_version="$1-$2" if [[ $snapshot_version == "" ]] then echo "Unable to resolve SNAPSHOT version for $group_id:$artifact_id:$version" exit 1 fi app-conf set maven.snapshot "$snapshot_version" app-conf set app.resolved_version "${version%-SNAPSHOT}-$snapshot_version" } download_version() { resolved_version="" target="" while getopts "v:f:" opt do case $opt in v) resolved_version="$OPTARG" ;; f) target="$OPTARG" ;; *) usage "Invalid option: $OPTARG" ;; esac done if [[ $resolved_version == "" || $target == "" ]] then usage fi repo=`app-conf get maven.repo` group_id=`app-conf get maven.group_id` artifact_id=`app-conf get maven.artifact_id` classifier=`app-conf get maven.classifier` version=`app-conf get app.version` group_id_slash=`slash $group_id` base_path=$group_id_slash/$artifact_id/$version file_name=$artifact_id-$resolved_version${classifier:+-}$classifier.zip mkdir -p .app/cache/$base_path l=.app/cache/$base_path/$file_name r=$repo/$base_path/$file_name echo "Downloading $group_id:$artifact_id:$resolved_version..." get $r $l rm -f "$target" ln -s "`pwd`/$l" "$target" } init() { while getopts "r:" opt do case $opt in r) app-conf set maven.repo "$OPTARG" shift 2 OPTIND=1 ;; *) usage "Invalid option: $OPTARG" ;; esac done local coordinates=${1-} x=${coordinates} x=${x//:/ } set -- $x case $# in 3) if [[ $1 == "" || $2 == "" || $3 == "" ]] then usage "Invalid Maven coordinates: $coordinates" fi group_id=$1; shift artifact_id=$1; shift classifier= version=$1; shift ;; 4) if [[ $1 == "" || $2 == "" || $3 == "" || $4 == "" ]] then usage "Invalid Maven coordinates: $coordinates" fi group_id=$1; shift artifact_id=$1; shift classifier=$1; shift version=$1; shift ;; *) usage "Invalid Maven coordinates: $coordinates" ;; esac app-conf set maven.group_id "$group_id" app-conf set maven.artifact_id "$artifact_id" [[ ! -z $classifier ]] && app-conf set maven.classifier "$classifier" app-conf set app.version "$version" } command="$1"; shift case "$command" in init) init "$@" ;; resolve-version) resolve_version ;; download-version) download_version "$@" ;; *) usage ;; esac exit 0