评论

收藏

[Unix] [白开水]-maven的命令程序mvn脚本分析

服务系统 服务系统 发布于:2021-07-21 12:23 | 阅读数:376 | 评论:0

最近公司将svn迁移到git,而且使用maven管理代码仓库,所以小小的研究了一下,为自动化持续集成做知识准备,以下是分析了maven的命令程序mvn:
  总体感觉与tomcat的启动脚本startup大同小异
#!/bin/sh
# ----------------------------------------------------------------------------
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
# ----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
# Maven2 Start Up Batch script
#
# Required ENV vars:
# ------------------
#   JAVA_HOME - location of a JDK home dir
#
# Optional ENV vars
# -----------------
#   M2_HOME - location of maven2's installed home dir
#   MAVEN_OPTS - parameters passed to the Java VM when running Maven
#   e.g. to debug Maven itself, use
#     set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
#   MAVEN_SKIP_RC - flag to disable loading of mavenrc files
# ----------------------------------------------------------------------------
#如果系统里有多JDK版本,就可以在这里指定JAVA_HOME,这样即使JAVA_HOME环境变量不是这个,也可以从这里弥补
#M2_HOME 也可以在这里指定,maven会在家目录生成.m2目录
#MAVEN_OPTS 这个是设maven启动参数的,有兴趣的同学可以研究下
JAVA_HOME=/usr/java/jdk1.6.0_38
if [ -z "$MAVEN_SKIP_RC" ] ; then
  if [ -f /etc/mavenrc ] ; then
  . /etc/mavenrc
  fi
  if [ -f "$HOME/.mavenrc" ] ; then
  . "$HOME/.mavenrc"
  fi
fi
#如果环境变量MAVEN_SKIP_RC存在,则从下面3个外部文件找是否设置maven的环境变量,
#下面是系统环境检查,和tomcat的startup.sh一曲同工
# OS specific support.  $var _must_ be set to either true or false.
cygwin=false;
darwin=false;
mingw=false
case "`uname`" in
  CYGWIN*) cygwin=true ;;
  MINGW*) mingw=true;;
  Darwin*) darwin=true      
       #
       # Look for the Apple JDKs first to preserve the existing behaviour, and then look
       # for the new JDKs provided by Oracle.
       # 
       if [[ -z "$JAVA_HOME" && -L /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK ]] ; then
       #
       # Apple JDKs
       #
       export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home
       fi
       
       if [[ -z "$JAVA_HOME" && -L /System/Library/Java/JavaVirtualMachines/CurrentJDK ]] ; then
       #
       # Apple JDKs
       #
       export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home
       fi
       
       if [[ -z "$JAVA_HOME" && -L "/Library/Java/JavaVirtualMachines/CurrentJDK" ]] ; then
       #
       # Oracle JDKs
       #
       export JAVA_HOME=/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home
       fi       
       ;;
esac
if [ -z "$JAVA_HOME" ] ; then
  if [ -r /etc/gentoo-release ] ; then
  JAVA_HOME=`java-config --jre-home`
  fi
fi
#此处貌似是为了照顾gentoo这个操作系统,特别加的,属于适应各操作系统方面的代码
if [ -z "$M2_HOME" ] ; then
  ## resolve links - $0 may be a link to maven's home
  PRG="$0"
#$0 ----当前shell名
  # need this for relative symlinks
  #test –h File  文件存在并且是一个符号链接(同-L) 
  while [ -h "$PRG" ] ; do
  ls=`ls -ld "$PRG"`
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '/.*' > /dev/null; then
    PRG="$link"
  else
    PRG="`dirname "$PRG"`/$link"
  fi
  done
  #这个是排除软链接,找出真实的命令运行目录
  #如果有童鞋分析过tomcat的startup.sh脚本,就可以发现,上面的脚本基本雷同
  
  
  saveddir=`pwd`
  M2_HOME=`dirname "$PRG"`/..
  # make it fully qualified
  M2_HOME=`cd "$M2_HOME" && pwd`
  cd "$saveddir"
  # echo Using m2 at $M2_HOME
fi
#cd进入mvn的应用目录
# For Cygwin, ensure paths are in UNIX format before anything is touched
if $cygwin ; then
  [ -n "$M2_HOME" ] &&
  M2_HOME=`cygpath --unix "$M2_HOME"`
  [ -n "$JAVA_HOME" ] &&
  JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
  [ -n "$CLASSPATH" ] &&
  CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
fi
# For Migwn, ensure paths are in UNIX format before anything is touched
if $mingw ; then
  [ -n "$M2_HOME" ] &&
  M2_HOME="`(cd "$M2_HOME"; pwd)`"
  [ -n "$JAVA_HOME" ] &&
  JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`"
  # TODO classpath?
fi
#上面一大串都是适应mingw和cygwin环境的,直接略过,下面才是干货
#test –z 字符串 字符串的长度为零  
#如果没有设置JAVA_HOME,程序就自己去找JAVA_HOME
if [ -z "$JAVA_HOME" ]; then
  javaExecutable="`which javac`"
  if [ -n "$javaExecutable" -a ! "`expr "$javaExecutable" : '\([^ ]*\)'`" = "no" ]; then
  #test –n 字符串的长度非零 
  #test 表达式1 –a 表达式2  两个表达式都为真 
  #link=`expr "$ls" : '.*-> \(.*\)$'` 模拟后: expr 'lrwxrwxrwx 1 root root 19 3月  17 10:12 ./bbb.sh -> /root/shell/test.sh' : '.*-> \(.*\)$'
  #很明确的发现是用expr来提取/root/shell/test.sh的内容
  #而这个循环就可以明确其目的,排除命令为链接,找出命令真正的目录,防止后面的命令出错 
  # readlink(1) is not available as standard on Solaris 10.
    #[^]      # 匹配不在指定组内的字符
    #上面的判读,主要是判断which javac,是否存在....
  readLink=`which readlink`
  if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then
    javaExecutable="`readlink -f "$javaExecutable"`"
    javaHome="`dirname "$javaExecutable"`"
    javaHome=`expr "$javaHome" : '\(.*\)/bin'`
    JAVA_HOME="$javaHome"
    export JAVA_HOME
  fi
  fi
fi
#如果没有设置JAVACMD,脚本就会在JAVA_HOME目录下去找
if [ -z "$JAVACMD" ] ; then
  if [ -n "$JAVA_HOME"  ] ; then
  if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
    # IBM's JDK on AIX uses strange locations for the executables
    JAVACMD="$JAVA_HOME/jre/sh/java"
  else
    JAVACMD="$JAVA_HOME/bin/java"
  fi
  else
  JAVACMD="`which java`"
  fi
fi
#test –x File  文件存在并且可执行 
#javac命令用不了,那就报错了鸟~~~
if [ ! -x "$JAVACMD" ] ; then
  echo "Error: JAVA_HOME is not defined correctly."
  echo "  We cannot execute $JAVACMD"
  exit 1
fi
#没有定义JAVA_HOME,脚本有自己找不到,就会在这里报错
if [ -z "$JAVA_HOME" ] ; then
  echo "Warning: JAVA_HOME environment variable is not set."
fi
CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher
# For Cygwin, switch paths to Windows format before running java
if $cygwin; then
  [ -n "$M2_HOME" ] &&
  M2_HOME=`cygpath --path --windows "$M2_HOME"`
  [ -n "$JAVA_HOME" ] &&
  JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
  [ -n "$CLASSPATH" ] &&
  CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
fi
#上面一堆代码的目的,就是为了下面一句代码提供变量.....
exec "$JAVACMD" \
  $MAVEN_OPTS \
  -classpath "${M2_HOME}"/boot/plexus-classworlds-*.jar \
  "-Dclassworlds.conf=${M2_HOME}/bin/m2.conf" \
  "-Dmaven.home=${M2_HOME}"  \
  ${CLASSWORLDS_LAUNCHER} "$@"



关注下面的标签,发现更多相似文章