mvnw 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. # Maven2 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. # TODO classpath?
  110. fi
  111. if [ -z "$JAVA_HOME" ]; then
  112. javaExecutable="$(which javac)"
  113. if [ -n "$javaExecutable" ] && ! [ "$(expr \"$javaExecutable\" : '\([^ ]*\)')" = "no" ]; then
  114. # readlink(1) is not available as standard on Solaris 10.
  115. readLink=$(which readlink)
  116. if [ ! $(expr "$readLink" : '\([^ ]*\)') = "no" ]; then
  117. if $darwin; then
  118. javaHome="$(dirname \"$javaExecutable\")"
  119. javaExecutable="$(cd \"$javaHome\" && pwd -P)/javac"
  120. else
  121. javaExecutable="$(readlink -f \"$javaExecutable\")"
  122. fi
  123. javaHome="$(dirname \"$javaExecutable\")"
  124. javaHome=$(expr "$javaHome" : '\(.*\)/bin')
  125. JAVA_HOME="$javaHome"
  126. export JAVA_HOME
  127. fi
  128. fi
  129. fi
  130. if [ -z "$JAVACMD" ]; then
  131. if [ -n "$JAVA_HOME" ]; then
  132. if [ -x "$JAVA_HOME/jre/sh/java" ]; then
  133. # IBM's JDK on AIX uses strange locations for the executables
  134. JAVACMD="$JAVA_HOME/jre/sh/java"
  135. else
  136. JAVACMD="$JAVA_HOME/bin/java"
  137. fi
  138. else
  139. JAVACMD="$(which java)"
  140. fi
  141. fi
  142. if [ ! -x "$JAVACMD" ]; then
  143. echo "Error: JAVA_HOME is not defined correctly." >&2
  144. echo " We cannot execute $JAVACMD" >&2
  145. exit 1
  146. fi
  147. if [ -z "$JAVA_HOME" ]; then
  148. echo "Warning: JAVA_HOME environment variable is not set."
  149. fi
  150. CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher
  151. # traverses directory structure from process work directory to filesystem root
  152. # first directory with .mvn subdirectory is considered project base directory
  153. find_maven_basedir() {
  154. if [ -z "$1" ]; then
  155. echo "Path not specified to find_maven_basedir"
  156. return 1
  157. fi
  158. basedir="$1"
  159. wdir="$1"
  160. while [ "$wdir" != '/' ]; do
  161. if [ -d "$wdir"/.mvn ]; then
  162. basedir=$wdir
  163. break
  164. fi
  165. # workaround for JBEAP-8937 (on Solaris 10/Sparc)
  166. if [ -d "${wdir}" ]; then
  167. wdir=$(
  168. cd "$wdir/.."
  169. pwd
  170. )
  171. fi
  172. # end of workaround
  173. done
  174. echo "${basedir}"
  175. }
  176. # concatenates all lines of a file
  177. concat_lines() {
  178. if [ -f "$1" ]; then
  179. echo "$(tr -s '\n' ' ' <"$1")"
  180. fi
  181. }
  182. BASE_DIR=$(find_maven_basedir "$(pwd)")
  183. if [ -z "$BASE_DIR" ]; then
  184. exit 1
  185. fi
  186. ##########################################################################################
  187. # Extension to allow automatically downloading the maven-wrapper.jar from Maven-central
  188. # This allows using the maven wrapper in projects that prohibit checking in binary data.
  189. ##########################################################################################
  190. if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then
  191. if [ "$MVNW_VERBOSE" = true ]; then
  192. echo "Found .mvn/wrapper/maven-wrapper.jar"
  193. fi
  194. else
  195. if [ "$MVNW_VERBOSE" = true ]; then
  196. echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..."
  197. fi
  198. jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar"
  199. while IFS="=" read key value; do
  200. case "$key" in wrapperUrl)
  201. jarUrl="$value"
  202. break
  203. ;;
  204. esac
  205. done <"$BASE_DIR/.mvn/wrapper/maven-wrapper.properties"
  206. if [ "$MVNW_VERBOSE" = true ]; then
  207. echo "Downloading from: $jarUrl"
  208. fi
  209. wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar"
  210. if command -v wget >/dev/null; then
  211. if [ "$MVNW_VERBOSE" = true ]; then
  212. echo "Found wget ... using wget"
  213. fi
  214. wget "$jarUrl" -O "$wrapperJarPath"
  215. elif command -v curl >/dev/null; then
  216. if [ "$MVNW_VERBOSE" = true ]; then
  217. echo "Found curl ... using curl"
  218. fi
  219. curl -o "$wrapperJarPath" "$jarUrl"
  220. else
  221. if [ "$MVNW_VERBOSE" = true ]; then
  222. echo "Falling back to using Java to download"
  223. fi
  224. javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java"
  225. if [ -e "$javaClass" ]; then
  226. if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then
  227. if [ "$MVNW_VERBOSE" = true ]; then
  228. echo " - Compiling MavenWrapperDownloader.java ..."
  229. fi
  230. # Compiling the Java class
  231. ("$JAVA_HOME/bin/javac" "$javaClass")
  232. fi
  233. if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then
  234. # Running the downloader
  235. if [ "$MVNW_VERBOSE" = true ]; then
  236. echo " - Running MavenWrapperDownloader.java ..."
  237. fi
  238. ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR")
  239. fi
  240. fi
  241. fi
  242. fi
  243. ##########################################################################################
  244. # End of extension
  245. ##########################################################################################
  246. export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"}
  247. if [ "$MVNW_VERBOSE" = true ]; then
  248. echo $MAVEN_PROJECTBASEDIR
  249. fi
  250. MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS"
  251. # For Cygwin, switch paths to Windows format before running java
  252. if $cygwin; then
  253. [ -n "$M2_HOME" ] &&
  254. M2_HOME=$(cygpath --path --windows "$M2_HOME")
  255. [ -n "$JAVA_HOME" ] &&
  256. JAVA_HOME=$(cygpath --path --windows "$JAVA_HOME")
  257. [ -n "$CLASSPATH" ] &&
  258. CLASSPATH=$(cygpath --path --windows "$CLASSPATH")
  259. [ -n "$MAVEN_PROJECTBASEDIR" ] &&
  260. MAVEN_PROJECTBASEDIR=$(cygpath --path --windows "$MAVEN_PROJECTBASEDIR")
  261. fi
  262. WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
  263. exec "$JAVACMD" \
  264. $MAVEN_OPTS \
  265. -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \
  266. "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
  267. ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@"