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
原文地址:https://www.cnblogs.com/xyzxy/p/14871079.html