1. 程式人生 > 其它 >SpringBoot資源分離打包急速部署更新

SpringBoot資源分離打包急速部署更新

1.pom新增

<build>
        <finalName>system-exam</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true
</fork> <addResources>true</addResources> </configuration> </plugin> <!--打包jar --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <!--不打包資原始檔 --> <excludes> <exclude>*.**</exclude> <exclude>*/*
.xml</exclude> </excludes> <archive> <manifest> <addClasspath>true</addClasspath> <!--MANIFEST.MF 中 Class-Path 加入字首 --> <classpathPrefix>lib/</classpathPrefix> <!--jar包不包含唯一版本標識 --> <useUniqueVersions>false</useUniqueVersions> <!--指定入口類 --> <mainClass>com.safety.SafetyExamApplication</mainClass> </manifest> <manifestEntries> <!--MANIFEST.MF 中 Class-Path 加入資原始檔目錄 --> <Class-Path>./resources/</Class-Path> </manifestEntries> </archive> <outputDirectory>${project.build.directory}/exam</outputDirectory> </configuration> </plugin> <!--拷貝依賴 copy-dependencies --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory> ${project.build.directory}/exam/lib/ </outputDirectory> </configuration> </execution> </executions> </plugin> <!--拷貝資原始檔 copy-resources --> <plugin> <artifactId>maven-resources-plugin</artifactId> <executions> <execution> <id>copy-resources</id> <phase>package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <resources> <resource> <directory>src/main/resources</directory> </resource> </resources> <outputDirectory>${project.build.directory}/exam/resources</outputDirectory> </configuration> </execution> <execution> <id>copy-service</id> <phase>package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <resources> <resource> <directory>src/main/resources/bat</directory> </resource> </resources> <outputDirectory>${project.build.directory}/exam</outputDirectory> </configuration> </execution> </executions> </plugin> <!--spring boot repackage,依賴 maven-jar-plugin 打包的jar包 重新打包成 spring boot 的jar包 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!--重寫包含依賴,包含不存在的依賴,jar裡沒有pom裡的依賴 --> <includes> <include> <groupId>null</groupId> <artifactId>null</artifactId> </include> </includes> <layout>ZIP</layout> <!--使用外部配置檔案,jar包裡沒有資原始檔 --> <addResources>true</addResources> <outputDirectory>${project.build.directory}/exam</outputDirectory> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> <configuration> <!--配置jar包特殊標識 配置後,保留原檔案,生成新檔案 *-run.jar --> <!--配置jar包特殊標識 不配置,原檔案命名為 *.jar.original,生成新檔案 *.jar --> <!--<classifier>run</classifier> --> </configuration> </execution> </executions> </plugin> </plugins> </build>

2.mvn clean install

3.效果

4.指令碼

APP_NAME=system-exam.jar

#使用說明,用來提示輸入引數
usage() {
echo "Usage: sh 執行指令碼.sh [start|stop|restart|status]"
exit 1
}

#檢查程式是否在執行
is_exist(){
pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' `
#如果不存在返回1,存在返回0
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}

#啟動方法
start(){
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is already running. pid=${pid} ."
else
echo "準備執行"
#cd /mnt/java_project/gateway;
nohup java -Xms256m -Xmx512m -jar $APP_NAME  --server.port=8085 > /dev/null 2>&1 &
echo "執行完畢"
fi
}

#停止方法
stop(){
is_exist

if [ $? -eq "0" ]; then # 不為空
    echo "the service pid is ${pid} , kill it now !"
    kill -15 $pid
    #rm -rf $PID_NAME
    echo "wait for 5 seconds..."
    sleep 5
    #is_run_again=`ps -ef | grep ${pid}`
    is_exist
    if [ $? -eq "0" ]; then # 不為空
      echo "the service is running now , pid is ${pid} , kill it again !"
      kill -9  $pid
      sleep 2
      echo "the $JAR_NAME process stopped !"
    else
      echo "the ${JAR_NAME} is not running !"
    fi
  else
    echo "the ${JAR_NAME} is not running !"
  fi

}

#輸出執行狀態
status(){
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is running. Pid is ${pid}"
else
echo "${APP_NAME} is NOT running."
fi
}

#重啟
restart(){
stop
start
}

#根據輸入引數,選擇執行對應方法,不輸入則執行使用說明
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac