Skip to content

Instantly share code, notes, and snippets.

@sawano
Last active September 27, 2018 11:32
Show Gist options
  • Save sawano/a73cb9a26075439ecf23f220bc0a9464 to your computer and use it in GitHub Desktop.
Save sawano/a73cb9a26075439ecf23f220bc0a9464 to your computer and use it in GitHub Desktop.
Fetching artifacts from Nexus 3

Script for fetching artifacts from Nexus 3 (bash)

"Forked" from https://gist.github.com/cedricwalter/e7739aab3d370ef83f1a13b8322e50be#gistcomment-2649005

Modifications:

  • Using grep instead of xmllint to be compatible with more *nix installations.
  • Adding support for when there's only one artifact in Nexus.

Example: getArtifact "my-group-id" "my-artifact" "LATEST"

A script for downloading and starting an executable jar (e.g. Spring Boot) could look like this:

#!/bin/bash -e

GROUP_ID=$1
ARTIFACT=$2
VERSION=$3
JAVA_ARGS=$4


function getArtifact() {
    # http://redsymbol.net/articles/unofficial-bash-strict-mode/
    set -euo pipefail
    IFS=$'\n\t'

    groupId=$1
    artifactId=$2
    version=$3

    # optional
    classifier=${4:-}
    type=${5:-jar}
    repo=${6:-snapshots}

    # Nexus 2
    #base="http://nexus.example.com/service/local/repositories/${repo}/content"
    # Nexus 3
    base="http://nexus.example.com/service/local/repositories/${repo}/content"
    if [[ $classifier != "" ]]; then
      classifier="-${classifier}"
    fi

    groupIdUrl="${groupId//.//}"
    filename="${artifactId}-${version}${classifier}.${type}"

    if [[ "${version}" == "LATEST" || "${version}" == *SNAPSHOT* ]] ; then
      if [[ "${version}" == "LATEST" ]] ; then
        metadataUri="${base}/${groupIdUrl}/${artifactId}/maven-metadata.xml"
        echo "Fetching metadata from: ${metadataUri} ..."
        version=$(grep -oPm1 "(?<=<latest>)[^<]+" <(curl -s ${metadataUri})) || true
        if [[ -z "${version}" ]] ; then
            # If there's only one artifact in Nexus then there's no <latest> tag
            version=$(grep -oPm1 "(?<=<version>)[^<]+" <(curl -s ${metadataUri}))
        fi
        echo "LATEST version is: ${version}"
      fi
      versionSpecificMetadataUri="${base}/${groupIdUrl}/${artifactId}/${version}/maven-metadata.xml"
      echo "Fetching metadata for version ${version} from: ${versionSpecificMetadataUri}..."
      metadata=$(curl -s ${versionSpecificMetadataUri})
      timestamp=$(grep -oPm1 "(?<=<timestamp>)[^<]+" <<<"${metadata}")
      buildnumber=$(grep -oPm1 "(?<=<buildNumber>)[^<]+" <<<"${metadata}")
      url="${base}/${groupIdUrl}/${artifactId}/${version}/${artifactId}-${version%-SNAPSHOT}-${timestamp}-${buildnumber}${classifier}.${type}"
      echo "Fetching jar from ${url} ..."
      curl -s -L -o ${filename} ${url}
    else
      url="${base}/${groupIdUrl}/${artifactId}/${version}/${artifactId}-${version}${classifier}.${type}"
      echo "Fetching jar from ${url} ..."
      curl -s -L -o ${filename} ${url}
    fi

    echo "Renaming ${filename} to app.jar"
    mv "${filename}" app.jar
}

echo "Downloading artifact: '${GROUP_ID}:${ARTIFACT}:${VERSION}'"
getArtifact $1 $2 $3

echo "Starting app..."

set -x
java ${JAVA_ARGS} -jar app.jar
set +x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment