mvnw 9.6 KB

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