SpringBoot脚本启动及其pom与编译文件的配置

启动脚本

#!/usr/bin/env bash

MAIN_CLASS=启动类名称
SLEEP_TIMEREVAL_S=5
abs_path(){
    SOURCE="${BASH_SOURCE[0]}"
    while [ -h "${SOURCE}" ]; do
        DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
        SOURCE="$(readlink "${SOURCE}")"
        [[ ${SOURCE} != /* ]] && SOURCE="${DIR}/${SOURCE}"
    done
    echo "$( cd -P "$( dirname "${SOURCE}" )" && pwd )"
}

BIN=`abs_path`
SHELL_LOG="${BIN}/console.out"

function LOG(){
  currentTime=`date "+%Y-%m-%d %H:%M:%S.%3N"`
  echo -e "$currentTime [${1}] ($$) $2" | tee -a ${SHELL_LOG}
}

verify_java_env(){
  if [ "x${JAVA_HOME}" != "x" ]; then
    ${JAVA_HOME}/bin/java -version >/dev/null 2>&1
  else
    java -version >/dev/null 2>&1
  fi
  if [ $? -ne 0 ]; then
    cat 1>&2 <<EOF
+========================================================================+
| Error: Java Environment is not availiable, Please check your JAVA_HOME |
+------------------------------------------------------------------------+
EOF
  return 1
  fi
  return 0
}

#verify environment
verify_java_env
if [ $? -ne 0 ]; then
  exit $?
fi

if [[ ! ${JAVA_OPTS} ]]; then
    JAVA_OPTS=" -Xms2g -Xmx2g -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8"
fi

if [[ ! ${REMOTE_DEBUG_SWITCH} ]]; then
    REMOTE_DEBUG_SWITCH=false
fi

if [[ ! ${SERVICE_CONF_PATH} ]]; then
    SERVICE_CONF_PATH=${BIN}/../config
fi

if [[ ! ${SERVICE_LOG_PATH} ]]; then
    SERVICE_LOG_PATH=${BIN}/../logs
fi


LIB_PATH=${BIN}/../lib
USER_DIR=${BIN}/../
CLASSPATH=${LIB_PATH}"/*:"${SERVICE_CONF_PATH}":."
if [ ${REMOTE_DEBUG_SWITCH} == true ]; then
    JAVA_OPTS=${JAVA_OPTS}" -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=${REMOTE_DEBUG_PORT}"
fi
JAVA_OPTS=${JAVA_OPTS}" -XX:HeapDumpPath="${SERVICE_LOG_PATH}" -Dlog.path="${SERVICE_LOG_PATH}
JAVA_OPTS=${JAVA_OPTS}" -Duser.dir="${USER_DIR}
#JAVA_OPTS=${JAVA_OPTS}" -Dserver.port="${SERVER_PORT}" -Ddata.path="${DATA_PATH}"  -Dmail.username="${MAIL_USERNAME}" -Dmail.password="${MAIL_PASSWORD}
if [ "x"${PID_FILE_PATH} != "x" ]; then
  JAVA_OPTS=${JAVA_OPTS}" -Dpid.file="${PID_FILE_PATH}
fi
JAVA_OPTS=${JAVA_OPTS}" -Dlogging.config="${SERVICE_CONF_PATH}"/logback.xml"
JAVA_OPTS=${JAVA_OPTS}" -classpath "${CLASSPATH}

if [ "x${JAVA_HOME}" != "x" ]; then
  EXE_JAVA=${JAVA_HOME}"/bin/java "${JAVA_OPTS}" "${MAIN_CLASS}
  JPS=${JAVA_HOME}/bin/jps
else
  EXE_JAVA="java "${JAVA_OPTS}" "${MAIN_CLASS}
  JPS="jps"
fi


# check if the process still in jvm
status_class(){
    local p=""
    if [ "x"${PID_FILE_PATH} != "x" ]; then
      if [ -f ${PID_FILE_PATH} ]; then
        local pid_in_file=`cat ${PID_FILE_PATH} 2>/dev/null`
        if [ "x"${pid_in_file} !=  "x" ]; then
          p=`${JPS} -q | grep ${pid_in_file} | awk '{print $1}'`
        fi
      fi
    else
      p=`${JPS} -l | grep "$1" | awk '{print $1}'`
    fi
    if [ -n "$p" ]; then
        # echo "$1  is still running with pid $p"
        return 0
    else
        # echo "$1  does not appear in the java process table"
        return 1
    fi
}


wait_for_startup(){
    local now_s=`date '+%s'`
    local stop_s=$((${now_s} + $1))
    while [ ${now_s} -le ${stop_s} ];do
        status_class  ${MAIN_CLASS}
        if [ $? -eq 0 ]; then
            return 0
        fi
        sleep ${SLEEP_TIMEREVAL_S}
        now_s=`date '+%s'`
    done
    exit 1
}

wait_for_stop(){
    local now_s=`date '+%s'`
    local stop_s=$((${now_s} + $1))
    while [ ${now_s} -le ${stop_s} ];do
        status_class  ${MAIN_CLASS}
        if [ $? -eq 1 ]; then
            return 0
        fi
        sleep ${SLEEP_TIMEREVAL_S}
        now_s=`date '+%s'`
    done
    return 1
}

start_m(){
    status_class  ${MAIN_CLASS}
    if [ $? -eq 0 ]; then
        LOG INFO " has been started in process"
        exit 0
    fi
    LOG INFO ${EXE_JAVA}
    nohup ${EXE_JAVA} >${SHELL_LOG} 2>&1 &
    LOG INFO "Waiting  to start complete ..."
    wait_for_startup 20
    if [ $? -eq 0 ]; then
        LOG INFO " start success"
        return 0
    else
        LOG ERROR " start exceeded over 20s" >&2
        return 1
    fi
}

stop_m(){
    local p=""
    if [ "x"${PID_FILE_PATH} != "x" ]; then
      if [ -f ${PID_FILE_PATH} ]; then
        local pid_in_file=`cat ${PID_FILE_PATH} 2>/dev/null`
        if [ "x"${pid_in_file} !=  "x" ]; then
          p=`${JPS} -q | grep ${pid_in_file} | awk '{print $1}'`
        fi
      fi
    else
      p=`${JPS} -l | grep "${MAIN_CLASS}" | awk '{print $1}'`
    fi
    if [ -z "${p}" ]; then
        LOG INFO " didn't start successfully, not found in the java process table"
        return 0
    fi
    LOG INFO "Killing  (pid ${p}) ..."
    case "`uname`" in
        CYCGWIN*) taskkill /PID "${p}" ;;
        *) kill -SIGTERM "${p}" ;;
    esac
    LOG INFO "Waiting  to stop complete ..."
    wait_for_stop 20
    if [ $? -eq 0 ]; then
        LOG INFO " stop success"
        return 0
    else
        LOG ERROR " stop exceeded over 20s" >&2
        return 1
    fi
}

shutdown_m(){
    local p=""
    if [ "x"${PID_FILE_PATH} != "x" ]; then
      if [ -f ${PID_FILE_PATH} ]; then
        local pid_in_file=`cat ${PID_FILE_PATH} 2>/dev/null`
        if [ "x"${pid_in_file} !=  "x" ]; then
          p=`${JPS} -q | grep ${pid_in_file} | awk '{print $1}'`
        fi
      fi
    else
      p=`${JPS} -l | grep "${MAIN_CLASS}" | awk '{print $1}'`
    fi
    if [ -z "${p}" ]; then
         LOG INFO " didn't start successfully, not found in the java process table"
        return 0
    fi
    LOG INFO "Killing  (pid ${p}) ..."
    case "`uname`" in
        CYCGWIN*) taskkill /F /PID "${p}" ;;
        *) kill -9 "${p}" ;;
    esac
}

restart_m(){
    stop_m
    if [ $? -eq 0 ]; then
        start_m
        exit $?
    else
        LOG ERROR " restart fail" >&2
        exit 1
    fi
}

usage(){
    echo " usage is [start|stop|shutdown|restart]"
}

if [ ! $1 ]; then
    usage
    exit 1;
fi
case $1 in
    start) start_m;;
    stop) stop_m;;
    shutdown) shutdown_m;;
    restart) restart_m;;
    *)
       usage
       exit 1
     ;;
esac
exit $?

POM插件配置

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <configuration>
                        <excludes>
                            <exclude>**/*.yml</exclude>
                            <exclude>**/*.properties</exclude>
                            <exclude>**/*.sh</exclude>
                            <exclude>**/*.xml</exclude>
                        </excludes>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.4.0</version>
                    <executions>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>2.2.1</version>
                    <executions>
                        <execution>
                            <id>assemble</id>
                            <goals>
                                <goal>single</goal>
                            </goals>
                            <!-- install -->
                            <phase>package</phase>
                        </execution>
                    </executions>
                    <configuration>
                        <appendAssemblyId>false</appendAssemblyId>
                        <attach>false</attach>
                        <descriptors>
                            <descriptor>${basedir}/src/main/assembly/deploy.xml</descriptor>
                        </descriptors>
                        <finalName>${project.artifactId}_${project.version}_1</finalName>
                        <outputDirectory>${project.parent.basedir}/packages</outputDirectory>
                    </configuration>
                </plugin>
        </plugins>

编译文件

<?xml version="1.0" encoding="UTF-8"?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>dist</id>
    <formats>
        <format>tar.gz</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>document/lib</outputDirectory>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <lineEnding>unix</lineEnding>
            <directory>./src/main/bin</directory>
            <outputDirectory>document/bin</outputDirectory>
            <includes>
                <include>**/*</include>
            </includes>
            <fileMode>0755</fileMode>
        </fileSet>
        <fileSet>
            <directory>./src/main/logs</directory>
            <outputDirectory>document/logs</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>./src/main/resources</directory>
            <includes>
                <include>*.properties</include>
                <include>logback.xml</include>
                <include>application.yml</include>
                <include>mybatis-mapper/**</include>
                <include>i18n/**</include>
                <include>static/**</include>
            </includes>
            <outputDirectory>document/conf</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>src/main/lib</directory>
            <outputDirectory>document/lib</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值