|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# |
| 4 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | +# contributor license agreements. See the NOTICE file distributed with |
| 6 | +# this work for additional information regarding copyright ownership. |
| 7 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 8 | +# (the "License"); you may not use this file except in compliance with |
| 9 | +# the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +# |
| 19 | + |
| 20 | +# Determine the current working directory |
| 21 | +_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" |
| 22 | +# Preserve the calling directory |
| 23 | +_CALLING_DIR="$(pwd)" |
| 24 | +# Options used during compilation |
| 25 | +_COMPILE_JVM_OPTS="-Xms2g -Xmx2g -XX:ReservedCodeCacheSize=1g -Xss128m" |
| 26 | + |
| 27 | +if [ "$CI" ]; then |
| 28 | + export MAVEN_CLI_OPTS="--no-transfer-progress --errors --fail-fast" |
| 29 | +fi |
| 30 | + |
| 31 | +# Installs any application tarball given a URL, the expected tarball name, |
| 32 | +# and, optionally, a checkable binary path to determine if the binary has |
| 33 | +# already been installed |
| 34 | +## Arg1 - URL |
| 35 | +## Arg2 - Tarball Name |
| 36 | +## Arg3 - Checkable Binary |
| 37 | +install_app() { |
| 38 | + local remote_tarball="$1/$2" |
| 39 | + local local_tarball="${_DIR}/$2" |
| 40 | + local binary="${_DIR}/$3" |
| 41 | + |
| 42 | + # setup `curl` and `wget` silent options if we're running on Jenkins |
| 43 | + local curl_opts="-L" |
| 44 | + local wget_opts="" |
| 45 | + curl_opts="--progress-bar ${curl_opts}" |
| 46 | + wget_opts="--progress=bar:force ${wget_opts}" |
| 47 | + |
| 48 | + if [ -z "$3" -o ! -f "$binary" ]; then |
| 49 | + # check if we already have the tarball |
| 50 | + # check if we have curl installed |
| 51 | + # download application |
| 52 | + rm -f "$local_tarball" |
| 53 | + [ ! -f "${local_tarball}" ] && [ $(command -v curl) ] && \ |
| 54 | + echo "exec: curl ${curl_opts} ${remote_tarball}" 1>&2 && \ |
| 55 | + curl ${curl_opts} "${remote_tarball}" > "${local_tarball}" |
| 56 | + # if the file still doesn't exist, lets try `wget` and cross our fingers |
| 57 | + [ ! -f "${local_tarball}" ] && [ $(command -v wget) ] && \ |
| 58 | + echo "exec: wget ${wget_opts} ${remote_tarball}" 1>&2 && \ |
| 59 | + wget ${wget_opts} -O "${local_tarball}" "${remote_tarball}" |
| 60 | + # if both were unsuccessful, exit |
| 61 | + [ ! -f "${local_tarball}" ] && \ |
| 62 | + echo -n "ERROR: Cannot download $2 with cURL or wget; " && \ |
| 63 | + echo "please install manually and try again." && \ |
| 64 | + exit 2 |
| 65 | + cd "${_DIR}" && tar -xzf "$2" |
| 66 | + rm -rf "$local_tarball" |
| 67 | + fi |
| 68 | +} |
| 69 | + |
| 70 | +function get_os_type(){ |
| 71 | + local unameOsOut=$(uname -s) |
| 72 | + local osType |
| 73 | + case "${unameOsOut}" in |
| 74 | + Linux*) osType=linux;; |
| 75 | + Darwin*) osType=darwin;; |
| 76 | + CYGWIN*) osType=windows;; |
| 77 | + MINGW*) osType=windows;; |
| 78 | + *) osType="UNKNOWN:${unameOsOut}" |
| 79 | + esac |
| 80 | + echo "$osType" |
| 81 | +} |
| 82 | + |
| 83 | +function get_os_arch(){ |
| 84 | + local unameArchOut="$(uname -m)" |
| 85 | + local arch |
| 86 | + case "${unameArchOut}" in |
| 87 | + x86_64*) arch=amd64;; |
| 88 | + arm64*) arch=aarch64;; |
| 89 | + *) arch="UNKNOWN:${unameOsOut}" |
| 90 | + esac |
| 91 | + echo "$arch" |
| 92 | +} |
| 93 | + |
| 94 | +# Determine the Maven version from the root pom.xml file and |
| 95 | +# install maven under the build/ folder if needed. |
| 96 | +install_mvn() { |
| 97 | + local MVND_VERSION=`grep "<mvnd.version>" "${_DIR}/../pom.xml" | head -n1 | awk -F '[<>]' '{print $3}'` |
| 98 | + local MVN_VERSION=`grep "<maven.version>" "${_DIR}/../pom.xml" | head -n1 | awk -F '[<>]' '{print $3}'` |
| 99 | + MVND_BIN="$(command -v mvnd)" |
| 100 | + if [ "$MVND_BIN" ]; then |
| 101 | + local MVND_DETECTED_VERSION="$(mvnd -v 2>&1| grep '(mvnd)' | awk '{print $5}')" |
| 102 | + local MVN_DETECTED_VERSION="$(mvnd -v 2>&1 | grep 'Apache Maven' | awk 'NR==2 {print $3}')" |
| 103 | + fi |
| 104 | + # See simple version normalization: http://stackoverflow.com/questions/16989598/bash-comparing-version-numbers |
| 105 | + function version { echo "$@" | awk -F. '{ printf("%03d%03d%03d\n", $1,$2,$3); }'; } |
| 106 | + |
| 107 | + if [ $(version $MVND_DETECTED_VERSION) -ne $(version $MVND_VERSION) ] |
| 108 | + then |
| 109 | + local APACHE_MIRROR=${APACHE_MIRROR:-'https://downloads.apache.org'} |
| 110 | + local OS_TYPE=$(get_os_type) |
| 111 | + local ARCH=$(get_os_arch) |
| 112 | + |
| 113 | + install_app \ |
| 114 | + "${APACHE_MIRROR}/maven/mvnd/${MVND_VERSION}" \ |
| 115 | + "maven-mvnd-${MVND_VERSION}-${OS_TYPE}-${ARCH}.tar.gz" \ |
| 116 | + "maven-mvnd-${MVND_VERSION}-${OS_TYPE}-${ARCH}/bin/mvnd" |
| 117 | + |
| 118 | + MVND_BIN="${_DIR}/maven-mvnd-${MVND_VERSION}-${OS_TYPE}-${ARCH}/bin/mvnd" |
| 119 | + else |
| 120 | + if [ $(version $MVN_DETECTED_VERSION) -ne $(version $MVN_VERSION) ]; then |
| 121 | + echo "Mvnd $MVND_DETECTED_VERSION embedded maven version $MVN_DETECTED_VERSION is not equivalent to $MVN_VERSION required in pom." |
| 122 | + exit 1 |
| 123 | + fi |
| 124 | + fi |
| 125 | +} |
| 126 | + |
| 127 | +install_mvn |
| 128 | + |
| 129 | +cd "${_CALLING_DIR}" |
| 130 | + |
| 131 | +# Set any `mvn` options if not already present |
| 132 | +export MAVEN_OPTS=${MAVEN_OPTS:-"$_COMPILE_JVM_OPTS"} |
| 133 | + |
| 134 | +echo "Using \`mvnd\` from path: $MVND_BIN" 1>&2 |
| 135 | +${MVND_BIN} $MAVEN_CLI_OPTS "$@" |
0 commit comments