thealkeshgupta/dsasandbox-java-execution-runner
0
1#!/bin/sh2# ----------------------------------------------------------------------------3# Licensed to the Apache Software Foundation (ASF) under one4# or more contributor license agreements. See the NOTICE file5# distributed with this work for additional information6# regarding copyright ownership. The ASF licenses this file7# to you under the Apache License, Version 2.0 (the8# "License"); you may not use this file except in compliance9# with the License. You may obtain a copy of the License at10#11# http://www.apache.org/licenses/LICENSE-2.012#13# Unless required by applicable law or agreed to in writing,14# software distributed under the License is distributed on an15# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY16# KIND, either express or implied. See the License for the17# specific language governing permissions and limitations18# under the License.19# ----------------------------------------------------------------------------20 21# ----------------------------------------------------------------------------22# Apache Maven Wrapper startup batch script, version 3.3.423#24# Optional ENV vars25# -----------------26# JAVA_HOME - location of a JDK home dir, required when download maven via java source27# MVNW_REPOURL - repo url base for downloading maven distribution28# MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven29# MVNW_VERBOSE - true: enable verbose log; debug: trace the mvnw script; others: silence the output30# ----------------------------------------------------------------------------31 32set -euf33[ "${MVNW_VERBOSE-}" != debug ] || set -x34 35# OS specific support.36native_path() { printf %s\\n "$1"; }37case "$(uname)" in38CYGWIN* | MINGW*)39 [ -z "${JAVA_HOME-}" ] || JAVA_HOME="$(cygpath --unix "$JAVA_HOME")"40 native_path() { cygpath --path --windows "$1"; }41 ;;42esac43 44# set JAVACMD and JAVACCMD45set_java_home() {46 # For Cygwin and MinGW, ensure paths are in Unix format before anything is touched47 if [ -n "${JAVA_HOME-}" ]; then48 if [ -x "$JAVA_HOME/jre/sh/java" ]; then49 # IBM's JDK on AIX uses strange locations for the executables50 JAVACMD="$JAVA_HOME/jre/sh/java"51 JAVACCMD="$JAVA_HOME/jre/sh/javac"52 else53 JAVACMD="$JAVA_HOME/bin/java"54 JAVACCMD="$JAVA_HOME/bin/javac"55 56 if [ ! -x "$JAVACMD" ] || [ ! -x "$JAVACCMD" ]; then57 echo "The JAVA_HOME environment variable is not defined correctly, so mvnw cannot run." >&258 echo "JAVA_HOME is set to \"$JAVA_HOME\", but \"\$JAVA_HOME/bin/java\" or \"\$JAVA_HOME/bin/javac\" does not exist." >&259 return 160 fi61 fi62 else63 JAVACMD="$(64 'set' +e65 'unset' -f command 2>/dev/null66 'command' -v java67 )" || :68 JAVACCMD="$(69 'set' +e70 'unset' -f command 2>/dev/null71 'command' -v javac72 )" || :73 74 if [ ! -x "${JAVACMD-}" ] || [ ! -x "${JAVACCMD-}" ]; then75 echo "The java/javac command does not exist in PATH nor is JAVA_HOME set, so mvnw cannot run." >&276 return 177 fi78 fi79}80 81# hash string like Java String::hashCode82hash_string() {83 str="${1:-}" h=084 while [ -n "$str" ]; do85 char="${str%"${str#?}"}"86 h=$(((h * 31 + $(LC_CTYPE=C printf %d "'$char")) % 4294967296))87 str="${str#?}"88 done89 printf %x\\n $h90}91 92verbose() { :; }93[ "${MVNW_VERBOSE-}" != true ] || verbose() { printf %s\\n "${1-}"; }94 95die() {96 printf %s\\n "$1" >&297 exit 198}99 100trim() {101 # MWRAPPER-139:102 # Trims trailing and leading whitespace, carriage returns, tabs, and linefeeds.103 # Needed for removing poorly interpreted newline sequences when running in more104 # exotic environments such as mingw bash on Windows.105 printf "%s" "${1}" | tr -d '[:space:]'106}107 108scriptDir="$(dirname "$0")"109scriptName="$(basename "$0")"110 111# parse distributionUrl and optional distributionSha256Sum, requires .mvn/wrapper/maven-wrapper.properties112while IFS="=" read -r key value; do113 case "${key-}" in114 distributionUrl) distributionUrl=$(trim "${value-}") ;;115 distributionSha256Sum) distributionSha256Sum=$(trim "${value-}") ;;116 esac117done <"$scriptDir/.mvn/wrapper/maven-wrapper.properties"118[ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties"119 120case "${distributionUrl##*/}" in121maven-mvnd-*bin.*)122 MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/123 case "${PROCESSOR_ARCHITECTURE-}${PROCESSOR_ARCHITEW6432-}:$(uname -a)" in124 *AMD64:CYGWIN* | *AMD64:MINGW*) distributionPlatform=windows-amd64 ;;125 :Darwin*x86_64) distributionPlatform=darwin-amd64 ;;126 :Darwin*arm64) distributionPlatform=darwin-aarch64 ;;127 :Linux*x86_64*) distributionPlatform=linux-amd64 ;;128 *)129 echo "Cannot detect native platform for mvnd on $(uname)-$(uname -m), use pure java version" >&2130 distributionPlatform=linux-amd64131 ;;132 esac133 distributionUrl="${distributionUrl%-bin.*}-$distributionPlatform.zip"134 ;;135maven-mvnd-*) MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ ;;136*) MVN_CMD="mvn${scriptName#mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;;137esac138 139# apply MVNW_REPOURL and calculate MAVEN_HOME140# maven home pattern: ~/.m2/wrapper/dists/{apache-maven-<version>,maven-mvnd-<version>-<platform>}/<hash>141[ -z "${MVNW_REPOURL-}" ] || distributionUrl="$MVNW_REPOURL$_MVNW_REPO_PATTERN${distributionUrl#*"$_MVNW_REPO_PATTERN"}"142distributionUrlName="${distributionUrl##*/}"143distributionUrlNameMain="${distributionUrlName%.*}"144distributionUrlNameMain="${distributionUrlNameMain%-bin}"145MAVEN_USER_HOME="${MAVEN_USER_HOME:-${HOME}/.m2}"146MAVEN_HOME="${MAVEN_USER_HOME}/wrapper/dists/${distributionUrlNameMain-}/$(hash_string "$distributionUrl")"147 148exec_maven() {149 unset MVNW_VERBOSE MVNW_USERNAME MVNW_PASSWORD MVNW_REPOURL || :150 exec "$MAVEN_HOME/bin/$MVN_CMD" "$@" || die "cannot exec $MAVEN_HOME/bin/$MVN_CMD"151}152 153if [ -d "$MAVEN_HOME" ]; then154 verbose "found existing MAVEN_HOME at $MAVEN_HOME"155 exec_maven "$@"156fi157 158case "${distributionUrl-}" in159*?-bin.zip | *?maven-mvnd-?*-?*.zip) ;;160*) die "distributionUrl is not valid, must match *-bin.zip or maven-mvnd-*.zip, but found '${distributionUrl-}'" ;;161esac162 163# prepare tmp dir164if TMP_DOWNLOAD_DIR="$(mktemp -d)" && [ -d "$TMP_DOWNLOAD_DIR" ]; then165 clean() { rm -rf -- "$TMP_DOWNLOAD_DIR"; }166 trap clean HUP INT TERM EXIT167else168 die "cannot create temp dir"169fi170 171mkdir -p -- "${MAVEN_HOME%/*}"172 173# Download and Install Apache Maven174verbose "Couldn't find MAVEN_HOME, downloading and installing it ..."175verbose "Downloading from: $distributionUrl"176verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName"177 178# select .zip or .tar.gz179if ! command -v unzip >/dev/null; then180 distributionUrl="${distributionUrl%.zip}.tar.gz"181 distributionUrlName="${distributionUrl##*/}"182fi183 184# verbose opt185__MVNW_QUIET_WGET=--quiet __MVNW_QUIET_CURL=--silent __MVNW_QUIET_UNZIP=-q __MVNW_QUIET_TAR=''186[ "${MVNW_VERBOSE-}" != true ] || __MVNW_QUIET_WGET='' __MVNW_QUIET_CURL='' __MVNW_QUIET_UNZIP='' __MVNW_QUIET_TAR=v187 188# normalize http auth189case "${MVNW_PASSWORD:+has-password}" in190'') MVNW_USERNAME='' MVNW_PASSWORD='' ;;191has-password) [ -n "${MVNW_USERNAME-}" ] || MVNW_USERNAME='' MVNW_PASSWORD='' ;;192esac193 194if [ -z "${MVNW_USERNAME-}" ] && command -v wget >/dev/null; then195 verbose "Found wget ... using wget"196 wget ${__MVNW_QUIET_WGET:+"$__MVNW_QUIET_WGET"} "$distributionUrl" -O "$TMP_DOWNLOAD_DIR/$distributionUrlName" || die "wget: Failed to fetch $distributionUrl"197elif [ -z "${MVNW_USERNAME-}" ] && command -v curl >/dev/null; then198 verbose "Found curl ... using curl"199 curl ${__MVNW_QUIET_CURL:+"$__MVNW_QUIET_CURL"} -f -L -o "$TMP_DOWNLOAD_DIR/$distributionUrlName" "$distributionUrl" || die "curl: Failed to fetch $distributionUrl"200elif set_java_home; then201 verbose "Falling back to use Java to download"202 javaSource="$TMP_DOWNLOAD_DIR/Downloader.java"203 targetZip="$TMP_DOWNLOAD_DIR/$distributionUrlName"204 cat >"$javaSource" <<-END205 public class Downloader extends java.net.Authenticator206 {207 protected java.net.PasswordAuthentication getPasswordAuthentication()208 {209 return new java.net.PasswordAuthentication( System.getenv( "MVNW_USERNAME" ), System.getenv( "MVNW_PASSWORD" ).toCharArray() );210 }211 public static void main( String[] args ) throws Exception212 {213 setDefault( new Downloader() );214 java.nio.file.Files.copy( java.net.URI.create( args[0] ).toURL().openStream(), java.nio.file.Paths.get( args[1] ).toAbsolutePath().normalize() );215 }216 }217 END218 # For Cygwin/MinGW, switch paths to Windows format before running javac and java219 verbose " - Compiling Downloader.java ..."220 "$(native_path "$JAVACCMD")" "$(native_path "$javaSource")" || die "Failed to compile Downloader.java"221 verbose " - Running Downloader.java ..."222 "$(native_path "$JAVACMD")" -cp "$(native_path "$TMP_DOWNLOAD_DIR")" Downloader "$distributionUrl" "$(native_path "$targetZip")"223fi224 225# If specified, validate the SHA-256 sum of the Maven distribution zip file226if [ -n "${distributionSha256Sum-}" ]; then227 distributionSha256Result=false228 if [ "$MVN_CMD" = mvnd.sh ]; then229 echo "Checksum validation is not supported for maven-mvnd." >&2230 echo "Please disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2231 exit 1232 elif command -v sha256sum >/dev/null; then233 if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | sha256sum -c - >/dev/null 2>&1; then234 distributionSha256Result=true235 fi236 elif command -v shasum >/dev/null; then237 if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | shasum -a 256 -c >/dev/null 2>&1; then238 distributionSha256Result=true239 fi240 else241 echo "Checksum validation was requested but neither 'sha256sum' or 'shasum' are available." >&2242 echo "Please install either command, or disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2243 exit 1244 fi245 if [ $distributionSha256Result = false ]; then246 echo "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised." >&2247 echo "If you updated your Maven version, you need to update the specified distributionSha256Sum property." >&2248 exit 1249 fi250fi251 252# unzip and move253if command -v unzip >/dev/null; then254 unzip ${__MVNW_QUIET_UNZIP:+"$__MVNW_QUIET_UNZIP"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -d "$TMP_DOWNLOAD_DIR" || die "failed to unzip"255else256 tar xzf${__MVNW_QUIET_TAR:+"$__MVNW_QUIET_TAR"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -C "$TMP_DOWNLOAD_DIR" || die "failed to untar"257fi258 259# Find the actual extracted directory name (handles snapshots where filename != directory name)260actualDistributionDir=""261 262# First try the expected directory name (for regular distributions)263if [ -d "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" ]; then264 if [ -f "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain/bin/$MVN_CMD" ]; then265 actualDistributionDir="$distributionUrlNameMain"266 fi267fi268 269# If not found, search for any directory with the Maven executable (for snapshots)270if [ -z "$actualDistributionDir" ]; then271 # enable globbing to iterate over items272 set +f273 for dir in "$TMP_DOWNLOAD_DIR"/*; do274 if [ -d "$dir" ]; then275 if [ -f "$dir/bin/$MVN_CMD" ]; then276 actualDistributionDir="$(basename "$dir")"277 break278 fi279 fi280 done281 set -f282fi283 284if [ -z "$actualDistributionDir" ]; then285 verbose "Contents of $TMP_DOWNLOAD_DIR:"286 verbose "$(ls -la "$TMP_DOWNLOAD_DIR")"287 die "Could not find Maven distribution directory in extracted archive"288fi289 290verbose "Found extracted Maven distribution directory: $actualDistributionDir"291printf %s\\n "$distributionUrl" >"$TMP_DOWNLOAD_DIR/$actualDistributionDir/mvnw.url"292mv -- "$TMP_DOWNLOAD_DIR/$actualDistributionDir" "$MAVEN_HOME" || [ -d "$MAVEN_HOME" ] || die "fail to move MAVEN_HOME"293 294clean || :295exec_maven "$@"296 