mvnw 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #!/bin/sh
  2. # ----------------------------------------------------------------------------
  3. # Licensed to the Apache Software Foundation (ASF) under one
  4. # or more contributor license agreements. See the NOTICE file
  5. # distributed with this work for additional information
  6. # regarding copyright ownership. The ASF licenses this file
  7. # to you under the Apache License, Version 2.0 (the
  8. # "License"); you may not use this file except in compliance
  9. # with the License. You may obtain a copy of the License at
  10. #
  11. # https://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing,
  14. # software distributed under the License is distributed on an
  15. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. # KIND, either express or implied. See the License for the
  17. # specific language governing permissions and limitations
  18. # under the License.
  19. # ----------------------------------------------------------------------------
  20. # ----------------------------------------------------------------------------
  21. # Apache Maven Wrapper startup batch script, version 3.3.2
  22. #
  23. # Optional ENV vars
  24. # -----------------
  25. # JAVA_HOME - location of a JDK home dir, required when download maven via java source
  26. # MVNW_REPOURL - repo url base for downloading maven distribution
  27. # MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven
  28. # MVNW_VERBOSE - true: enable verbose log; debug: trace the mvnw script; others: silence the output
  29. # ----------------------------------------------------------------------------
  30. set -euf
  31. [ "${MVNW_VERBOSE-}" != debug ] || set -x
  32. # OS specific support.
  33. native_path() { printf %s\\n "$1"; }
  34. case "$(uname)" in
  35. CYGWIN* | MINGW*)
  36. [ -z "${JAVA_HOME-}" ] || JAVA_HOME="$(cygpath --unix "$JAVA_HOME")"
  37. native_path() { cygpath --path --windows "$1"; }
  38. ;;
  39. esac
  40. # set JAVACMD and JAVACCMD
  41. set_java_home() {
  42. # For Cygwin and MinGW, ensure paths are in Unix format before anything is touched
  43. if [ -n "${JAVA_HOME-}" ]; then
  44. if [ -x "$JAVA_HOME/jre/sh/java" ]; then
  45. # IBM's JDK on AIX uses strange locations for the executables
  46. JAVACMD="$JAVA_HOME/jre/sh/java"
  47. JAVACCMD="$JAVA_HOME/jre/sh/javac"
  48. else
  49. JAVACMD="$JAVA_HOME/bin/java"
  50. JAVACCMD="$JAVA_HOME/bin/javac"
  51. if [ ! -x "$JAVACMD" ] || [ ! -x "$JAVACCMD" ]; then
  52. echo "The JAVA_HOME environment variable is not defined correctly, so mvnw cannot run." >&2
  53. echo "JAVA_HOME is set to \"$JAVA_HOME\", but \"\$JAVA_HOME/bin/java\" or \"\$JAVA_HOME/bin/javac\" does not exist." >&2
  54. return 1
  55. fi
  56. fi
  57. else
  58. JAVACMD="$(
  59. 'set' +e
  60. 'unset' -f command 2>/dev/null
  61. 'command' -v java
  62. )" || :
  63. JAVACCMD="$(
  64. 'set' +e
  65. 'unset' -f command 2>/dev/null
  66. 'command' -v javac
  67. )" || :
  68. if [ ! -x "${JAVACMD-}" ] || [ ! -x "${JAVACCMD-}" ]; then
  69. echo "The java/javac command does not exist in PATH nor is JAVA_HOME set, so mvnw cannot run." >&2
  70. return 1
  71. fi
  72. fi
  73. }
  74. # hash string like Java String::hashCode
  75. hash_string() {
  76. str="${1:-}" h=0
  77. while [ -n "$str" ]; do
  78. char="${str%"${str#?}"}"
  79. h=$(((h * 31 + $(LC_CTYPE=C printf %d "'$char")) % 4294967296))
  80. str="${str#?}"
  81. done
  82. printf %x\\n $h
  83. }
  84. verbose() { :; }
  85. [ "${MVNW_VERBOSE-}" != true ] || verbose() { printf %s\\n "${1-}"; }
  86. die() {
  87. printf %s\\n "$1" >&2
  88. exit 1
  89. }
  90. trim() {
  91. # MWRAPPER-139:
  92. # Trims trailing and leading whitespace, carriage returns, tabs, and linefeeds.
  93. # Needed for removing poorly interpreted newline sequences when running in more
  94. # exotic environments such as mingw bash on Windows.
  95. printf "%s" "${1}" | tr -d '[:space:]'
  96. }
  97. # parse distributionUrl and optional distributionSha256Sum, requires .mvn/wrapper/maven-wrapper.properties
  98. while IFS="=" read -r key value; do
  99. case "${key-}" in
  100. distributionUrl) distributionUrl=$(trim "${value-}") ;;
  101. distributionSha256Sum) distributionSha256Sum=$(trim "${value-}") ;;
  102. esac
  103. done <"${0%/*}/.mvn/wrapper/maven-wrapper.properties"
  104. [ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in ${0%/*}/.mvn/wrapper/maven-wrapper.properties"
  105. case "${distributionUrl##*/}" in
  106. maven-mvnd-*bin.*)
  107. MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/
  108. case "${PROCESSOR_ARCHITECTURE-}${PROCESSOR_ARCHITEW6432-}:$(uname -a)" in
  109. *AMD64:CYGWIN* | *AMD64:MINGW*) distributionPlatform=windows-amd64 ;;
  110. :Darwin*x86_64) distributionPlatform=darwin-amd64 ;;
  111. :Darwin*arm64) distributionPlatform=darwin-aarch64 ;;
  112. :Linux*x86_64*) distributionPlatform=linux-amd64 ;;
  113. *)
  114. echo "Cannot detect native platform for mvnd on $(uname)-$(uname -m), use pure java version" >&2
  115. distributionPlatform=linux-amd64
  116. ;;
  117. esac
  118. distributionUrl="${distributionUrl%-bin.*}-$distributionPlatform.zip"
  119. ;;
  120. maven-mvnd-*) MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ ;;
  121. *) MVN_CMD="mvn${0##*/mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;;
  122. esac
  123. # apply MVNW_REPOURL and calculate MAVEN_HOME
  124. # maven home pattern: ~/.m2/wrapper/dists/{apache-maven-<version>,maven-mvnd-<version>-<platform>}/<hash>
  125. [ -z "${MVNW_REPOURL-}" ] || distributionUrl="$MVNW_REPOURL$_MVNW_REPO_PATTERN${distributionUrl#*"$_MVNW_REPO_PATTERN"}"
  126. distributionUrlName="${distributionUrl##*/}"
  127. distributionUrlNameMain="${distributionUrlName%.*}"
  128. distributionUrlNameMain="${distributionUrlNameMain%-bin}"
  129. MAVEN_USER_HOME="${MAVEN_USER_HOME:-${HOME}/.m2}"
  130. MAVEN_HOME="${MAVEN_USER_HOME}/wrapper/dists/${distributionUrlNameMain-}/$(hash_string "$distributionUrl")"
  131. exec_maven() {
  132. unset MVNW_VERBOSE MVNW_USERNAME MVNW_PASSWORD MVNW_REPOURL || :
  133. exec "$MAVEN_HOME/bin/$MVN_CMD" "$@" || die "cannot exec $MAVEN_HOME/bin/$MVN_CMD"
  134. }
  135. if [ -d "$MAVEN_HOME" ]; then
  136. verbose "found existing MAVEN_HOME at $MAVEN_HOME"
  137. exec_maven "$@"
  138. fi
  139. case "${distributionUrl-}" in
  140. *?-bin.zip | *?maven-mvnd-?*-?*.zip) ;;
  141. *) die "distributionUrl is not valid, must match *-bin.zip or maven-mvnd-*.zip, but found '${distributionUrl-}'" ;;
  142. esac
  143. # prepare tmp dir
  144. if TMP_DOWNLOAD_DIR="$(mktemp -d)" && [ -d "$TMP_DOWNLOAD_DIR" ]; then
  145. clean() { rm -rf -- "$TMP_DOWNLOAD_DIR"; }
  146. trap clean HUP INT TERM EXIT
  147. else
  148. die "cannot create temp dir"
  149. fi
  150. mkdir -p -- "${MAVEN_HOME%/*}"
  151. # Download and Install Apache Maven
  152. verbose "Couldn't find MAVEN_HOME, downloading and installing it ..."
  153. verbose "Downloading from: $distributionUrl"
  154. verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName"
  155. # select .zip or .tar.gz
  156. if ! command -v unzip >/dev/null; then
  157. distributionUrl="${distributionUrl%.zip}.tar.gz"
  158. distributionUrlName="${distributionUrl##*/}"
  159. fi
  160. # verbose opt
  161. __MVNW_QUIET_WGET=--quiet __MVNW_QUIET_CURL=--silent __MVNW_QUIET_UNZIP=-q __MVNW_QUIET_TAR=''
  162. [ "${MVNW_VERBOSE-}" != true ] || __MVNW_QUIET_WGET='' __MVNW_QUIET_CURL='' __MVNW_QUIET_UNZIP='' __MVNW_QUIET_TAR=v
  163. # normalize http auth
  164. case "${MVNW_PASSWORD:+has-password}" in
  165. '') MVNW_USERNAME='' MVNW_PASSWORD='' ;;
  166. has-password) [ -n "${MVNW_USERNAME-}" ] || MVNW_USERNAME='' MVNW_PASSWORD='' ;;
  167. esac
  168. if [ -z "${MVNW_USERNAME-}" ] && command -v wget >/dev/null; then
  169. verbose "Found wget ... using wget"
  170. wget ${__MVNW_QUIET_WGET:+"$__MVNW_QUIET_WGET"} "$distributionUrl" -O "$TMP_DOWNLOAD_DIR/$distributionUrlName" || die "wget: Failed to fetch $distributionUrl"
  171. elif [ -z "${MVNW_USERNAME-}" ] && command -v curl >/dev/null; then
  172. verbose "Found curl ... using curl"
  173. curl ${__MVNW_QUIET_CURL:+"$__MVNW_QUIET_CURL"} -f -L -o "$TMP_DOWNLOAD_DIR/$distributionUrlName" "$distributionUrl" || die "curl: Failed to fetch $distributionUrl"
  174. elif set_java_home; then
  175. verbose "Falling back to use Java to download"
  176. javaSource="$TMP_DOWNLOAD_DIR/Downloader.java"
  177. targetZip="$TMP_DOWNLOAD_DIR/$distributionUrlName"
  178. cat >"$javaSource" <<-END
  179. public class Downloader extends java.net.Authenticator
  180. {
  181. protected java.net.PasswordAuthentication getPasswordAuthentication()
  182. {
  183. return new java.net.PasswordAuthentication( System.getenv( "MVNW_USERNAME" ), System.getenv( "MVNW_PASSWORD" ).toCharArray() );
  184. }
  185. public static void main( String[] args ) throws Exception
  186. {
  187. setDefault( new Downloader() );
  188. java.nio.file.Files.copy( java.net.URI.create( args[0] ).toURL().openStream(), java.nio.file.Paths.get( args[1] ).toAbsolutePath().normalize() );
  189. }
  190. }
  191. END
  192. # For Cygwin/MinGW, switch paths to Windows format before running javac and java
  193. verbose " - Compiling Downloader.java ..."
  194. "$(native_path "$JAVACCMD")" "$(native_path "$javaSource")" || die "Failed to compile Downloader.java"
  195. verbose " - Running Downloader.java ..."
  196. "$(native_path "$JAVACMD")" -cp "$(native_path "$TMP_DOWNLOAD_DIR")" Downloader "$distributionUrl" "$(native_path "$targetZip")"
  197. fi
  198. # If specified, validate the SHA-256 sum of the Maven distribution zip file
  199. if [ -n "${distributionSha256Sum-}" ]; then
  200. distributionSha256Result=false
  201. if [ "$MVN_CMD" = mvnd.sh ]; then
  202. echo "Checksum validation is not supported for maven-mvnd." >&2
  203. echo "Please disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2
  204. exit 1
  205. elif command -v sha256sum >/dev/null; then
  206. if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | sha256sum -c >/dev/null 2>&1; then
  207. distributionSha256Result=true
  208. fi
  209. elif command -v shasum >/dev/null; then
  210. if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | shasum -a 256 -c >/dev/null 2>&1; then
  211. distributionSha256Result=true
  212. fi
  213. else
  214. echo "Checksum validation was requested but neither 'sha256sum' or 'shasum' are available." >&2
  215. echo "Please install either command, or disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2
  216. exit 1
  217. fi
  218. if [ $distributionSha256Result = false ]; then
  219. echo "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised." >&2
  220. echo "If you updated your Maven version, you need to update the specified distributionSha256Sum property." >&2
  221. exit 1
  222. fi
  223. fi
  224. # unzip and move
  225. if command -v unzip >/dev/null; then
  226. unzip ${__MVNW_QUIET_UNZIP:+"$__MVNW_QUIET_UNZIP"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -d "$TMP_DOWNLOAD_DIR" || die "failed to unzip"
  227. else
  228. tar xzf${__MVNW_QUIET_TAR:+"$__MVNW_QUIET_TAR"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -C "$TMP_DOWNLOAD_DIR" || die "failed to untar"
  229. fi
  230. printf %s\\n "$distributionUrl" >"$TMP_DOWNLOAD_DIR/$distributionUrlNameMain/mvnw.url"
  231. mv -- "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" "$MAVEN_HOME" || [ -d "$MAVEN_HOME" ] || die "fail to move MAVEN_HOME"
  232. clean || :
  233. exec_maven "$@"