mvnw 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. # http://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. # Maven Start Up Batch script
  22. #
  23. # Required ENV vars:
  24. # ------------------
  25. # JAVA_HOME - location of a JDK home dir
  26. #
  27. # Optional ENV vars
  28. # -----------------
  29. # M2_HOME - location of maven2's installed home dir
  30. # MAVEN_OPTS - parameters passed to the Java VM when running Maven
  31. # e.g. to debug Maven itself, use
  32. # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
  33. # MAVEN_SKIP_RC - flag to disable loading of mavenrc files
  34. # ----------------------------------------------------------------------------
  35. if [ -z "$MAVEN_SKIP_RC" ]; then
  36. if [ -f /etc/mavenrc ]; then
  37. . /etc/mavenrc
  38. fi
  39. if [ -f "$HOME/.mavenrc" ]; then
  40. . "$HOME/.mavenrc"
  41. fi
  42. fi
  43. # OS specific support. $var _must_ be set to either true or false.
  44. cygwin=false
  45. darwin=false
  46. mingw=false
  47. case "$(uname)" in
  48. CYGWIN*) cygwin=true ;;
  49. MINGW*) mingw=true ;;
  50. Darwin*)
  51. darwin=true
  52. # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home
  53. # See https://developer.apple.com/library/mac/qa/qa1170/_index.html
  54. if [ -z "$JAVA_HOME" ]; then
  55. if [ -x "/usr/libexec/java_home" ]; then
  56. JAVA_HOME="$(/usr/libexec/java_home)"
  57. export JAVA_HOME
  58. else
  59. JAVA_HOME="/Library/Java/Home"
  60. export JAVA_HOME
  61. fi
  62. fi
  63. ;;
  64. esac
  65. if [ -z "$JAVA_HOME" ]; then
  66. if [ -r /etc/gentoo-release ]; then
  67. JAVA_HOME=$(java-config --jre-home)
  68. fi
  69. fi
  70. if [ -z "$M2_HOME" ]; then
  71. ## resolve links - $0 may be a link to maven's home
  72. PRG="$0"
  73. # need this for relative symlinks
  74. while [ -h "$PRG" ]; do
  75. ls=$(ls -ld "$PRG")
  76. link=$(expr "$ls" : '.*-> \(.*\)$')
  77. if expr "$link" : '/.*' >/dev/null; then
  78. PRG="$link"
  79. else
  80. PRG="$(dirname "$PRG")/$link"
  81. fi
  82. done
  83. saveddir=$(pwd)
  84. M2_HOME=$(dirname "$PRG")/..
  85. # make it fully qualified
  86. M2_HOME=$(cd "$M2_HOME" && pwd)
  87. cd "$saveddir" || exit
  88. # echo Using m2 at $M2_HOME
  89. fi
  90. # For Cygwin, ensure paths are in UNIX format before anything is touched
  91. if $cygwin; then
  92. [ -n "$M2_HOME" ] &&
  93. M2_HOME=$(cygpath --unix "$M2_HOME")
  94. [ -n "$JAVA_HOME" ] &&
  95. JAVA_HOME=$(cygpath --unix "$JAVA_HOME")
  96. [ -n "$CLASSPATH" ] &&
  97. CLASSPATH=$(cygpath --path --unix "$CLASSPATH")
  98. fi
  99. # For Mingw, ensure paths are in UNIX format before anything is touched
  100. if $mingw; then
  101. [ -n "$M2_HOME" ] &&
  102. M2_HOME="$( (
  103. cd "$M2_HOME" || exit
  104. pwd
  105. ))"
  106. [ -n "$JAVA_HOME" ] &&
  107. JAVA_HOME="$( (
  108. cd "$JAVA_HOME" || exit
  109. pwd
  110. ))"
  111. fi
  112. if [ -z "$JAVA_HOME" ]; then
  113. javaExecutable="$(command -v javac)"
  114. if [ -n "$javaExecutable" ] && ! [ "$(expr "$javaExecutable" : '\([^ ]*\)')" = "no" ]; then
  115. # readlink(1) is not available as standard on Solaris 10.
  116. readLink=$(command -v readlink)
  117. if [ ! "$(expr "$readLink" : '\([^ ]*\)')" = "no" ]; then
  118. if $darwin; then
  119. javaHome="$(dirname "$javaExecutable")"
  120. javaExecutable="$(cd "$javaHome" && pwd -P)/javac"
  121. else
  122. javaExecutable="$(readlink -f "$javaExecutable")"
  123. fi
  124. javaHome="$(dirname "$javaExecutable")"
  125. javaHome=$(expr "$javaHome" : '\(.*\)/bin')
  126. JAVA_HOME="$javaHome"
  127. export JAVA_HOME
  128. fi
  129. fi
  130. fi
  131. if [ -z "$JAVACMD" ]; then
  132. if [ -n "$JAVA_HOME" ]; then
  133. if [ -x "$JAVA_HOME/jre/sh/java" ]; then
  134. # IBM's JDK on AIX uses strange locations for the executables
  135. JAVACMD="$JAVA_HOME/jre/sh/java"
  136. else
  137. JAVACMD="$JAVA_HOME/bin/java"
  138. fi
  139. else
  140. JAVACMD="$(command -v java)"
  141. fi
  142. fi
  143. if [ ! -x "$JAVACMD" ]; then
  144. echo "Error: JAVA_HOME is not defined correctly." >&2
  145. echo " We cannot execute $JAVACMD" >&2
  146. exit 1
  147. fi
  148. if [ -z "$JAVA_HOME" ]; then
  149. echo "Warning: JAVA_HOME environment variable is not set."
  150. fi
  151. CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher
  152. # traverses directory structure from process work directory to filesystem root
  153. # first directory with .mvn subdirectory is considered project base directory
  154. find_maven_basedir() {
  155. if [ -z "$1" ]; then
  156. echo "Path not specified to find_maven_basedir"
  157. return 1
  158. fi
  159. basedir="$1"
  160. wdir="$1"
  161. while [ "$wdir" != '/' ]; do
  162. if [ -d "$wdir"/.mvn ]; then
  163. basedir=$wdir
  164. break
  165. fi
  166. # workaround for JBEAP-8937 (on Solaris 10/Sparc)
  167. if [ -d "${wdir}" ]; then
  168. wdir=$(
  169. cd "$wdir/.." || exit
  170. pwd
  171. )
  172. fi
  173. # end of workaround
  174. done
  175. echo "${basedir}"
  176. }
  177. # concatenates all lines of a file
  178. concat_lines() {
  179. if [ -f "$1" ]; then
  180. tr -s '\n' ' ' <"$1"
  181. fi
  182. }
  183. BASE_DIR=$(find_maven_basedir "$(pwd)")
  184. if [ -z "$BASE_DIR" ]; then
  185. exit 1
  186. fi
  187. ##########################################################################################
  188. # Extension to allow automatically downloading the maven-wrapper.jar from Maven-central
  189. # This allows using the maven wrapper in projects that prohibit checking in binary data.
  190. ##########################################################################################
  191. if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then
  192. if [ "$MVNW_VERBOSE" = true ]; then
  193. echo "Found .mvn/wrapper/maven-wrapper.jar"
  194. fi
  195. else
  196. if [ "$MVNW_VERBOSE" = true ]; then
  197. echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..."
  198. fi
  199. if [ -n "$MVNW_REPOURL" ]; then
  200. jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
  201. else
  202. jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
  203. fi
  204. while IFS="=" read -r key value; do
  205. case "$key" in wrapperUrl)
  206. jarUrl="$value"
  207. break
  208. ;;
  209. esac
  210. done <"$BASE_DIR/.mvn/wrapper/maven-wrapper.properties"
  211. if [ "$MVNW_VERBOSE" = true ]; then
  212. echo "Downloading from: $jarUrl"
  213. fi
  214. wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar"
  215. if $cygwin; then
  216. wrapperJarPath=$(cygpath --path --windows "$wrapperJarPath")
  217. fi
  218. if command -v wget >/dev/null; then
  219. if [ "$MVNW_VERBOSE" = true ]; then
  220. echo "Found wget ... using wget"
  221. fi
  222. if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then
  223. wget "$jarUrl" -O "$wrapperJarPath"
  224. else
  225. wget --http-user="$MVNW_USERNAME" --http-password="$MVNW_PASSWORD" "$jarUrl" -O "$wrapperJarPath"
  226. fi
  227. elif command -v curl >/dev/null; then
  228. if [ "$MVNW_VERBOSE" = true ]; then
  229. echo "Found curl ... using curl"
  230. fi
  231. if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then
  232. curl -o "$wrapperJarPath" "$jarUrl" -f
  233. else
  234. curl --user "$MVNW_USERNAME":"$MVNW_PASSWORD" -o "$wrapperJarPath" "$jarUrl" -f
  235. fi
  236. else
  237. if [ "$MVNW_VERBOSE" = true ]; then
  238. echo "Falling back to using Java to download"
  239. fi
  240. javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java"
  241. # For Cygwin, switch paths to Windows format before running javac
  242. if $cygwin; then
  243. javaClass=$(cygpath --path --windows "$javaClass")
  244. fi
  245. if [ -e "$javaClass" ]; then
  246. if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then
  247. if [ "$MVNW_VERBOSE" = true ]; then
  248. echo " - Compiling MavenWrapperDownloader.java ..."
  249. fi
  250. # Compiling the Java class
  251. ("$JAVA_HOME/bin/javac" "$javaClass")
  252. fi
  253. if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then
  254. # Running the downloader
  255. if [ "$MVNW_VERBOSE" = true ]; then
  256. echo " - Running MavenWrapperDownloader.java ..."
  257. fi
  258. ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR")
  259. fi
  260. fi
  261. fi
  262. fi
  263. ##########################################################################################
  264. # End of extension
  265. ##########################################################################################
  266. export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"}
  267. if [ "$MVNW_VERBOSE" = true ]; then
  268. echo "$MAVEN_PROJECTBASEDIR"
  269. fi
  270. MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS"
  271. # For Cygwin, switch paths to Windows format before running java
  272. if $cygwin; then
  273. [ -n "$M2_HOME" ] &&
  274. M2_HOME=$(cygpath --path --windows "$M2_HOME")
  275. [ -n "$JAVA_HOME" ] &&
  276. JAVA_HOME=$(cygpath --path --windows "$JAVA_HOME")
  277. [ -n "$CLASSPATH" ] &&
  278. CLASSPATH=$(cygpath --path --windows "$CLASSPATH")
  279. [ -n "$MAVEN_PROJECTBASEDIR" ] &&
  280. MAVEN_PROJECTBASEDIR=$(cygpath --path --windows "$MAVEN_PROJECTBASEDIR")
  281. fi
  282. # Provide a "standardized" way to retrieve the CLI args that will
  283. # work with both Windows and non-Windows executions.
  284. MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $*"
  285. export MAVEN_CMD_LINE_ARGS
  286. WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
  287. exec "$JAVACMD" \
  288. $MAVEN_OPTS \
  289. -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \
  290. "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
  291. ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@"