Artifactory - uploading 3rd party artifacts with Maven

Uploading artifacts to Artifactory via web interface is really clunky… Let’s script it!

#!/bin/bash

ARTIFACTORY_URL="https://artifactory.example.com/artifactory"
ARTIFACTORY_REPO="ext-release-local"

MVN_USERNAME="maven"
MVN_PASSWORD="password"

[[ -z $DEBUG ]] || set -x

umask 0077

if [ $# -lt 4 ]; then
    echo "usage: $( basename "$0" ) groupId artifactId version filename"
    exit 1
fi

GROUP_ID="$1"
ARTIFACT_ID="$2"
VERSION="$3"
FILE="$4"
PACKAGING="${5:-jar}"

TEMP_SETTINGS="$( mktemp --suffix=.xml )"

trap 'rm -rf ${TEMP_SETTINGS}; exit' EXIT INT TERM

cat <<EOF > "${TEMP_SETTINGS}"
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <servers>
        <server>
            <id>${ARTIFACTORY_REPO}</id>
            <username>${MVN_USERNAME}</username>
            <password>${MVN_PASSWORD}</password>
        </server>
    </servers>
</settings>
EOF

mvn -e -s "${TEMP_SETTINGS}" deploy:deploy-file \
    -Dpackaging="${PACKAGING}" \
    -DrepositoryId="${ARTIFACTORY_REPO}" \
    -Durl="${ARTIFACTORY_URL}/${ARTIFACTORY_REPO}" \
    -DgroupId="${GROUP_ID}" \
    -DartifactId="${ARTIFACT_ID}" \
    -Dversion="${VERSION}" \
    -Dfile="${FILE}"