2020-08-14

2020-08-14 19:09:34

springboot打包依賴包和組態檔分離

前言:springboot專案打包使用spring-boot-maven-plugin外掛,預設會將依賴包和組態檔統統打進可執行jar檔案中,使得jar檔案太臃腫,則正式環境修改組態檔較麻煩,因此需要將組態檔和依賴jar包分離。如下:

<!-- 構建設定 -->
<build>
    <finalName>${project.name}</finalName>
    <sourceDirectory>src/main/java</sourceDirectory>
    <!-- 控制資原始檔的拷貝 -->
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
                <include>**/*.yml</include>
            </includes>
            <filtering>false</filtering>
            <!--本地註釋掉,伺服器放開-->
            <targetPath>${project.build.directory}/${project.name}/config</targetPath>
        </resource>
        <resource>
            <directory>src/main/bin</directory>
            <targetPath>${project.build.directory}/${project.name}/bin</targetPath>
        </resource>
        <!--<resource>-->
            <!--<directory>src/main/java</directory>-->
            <!--<includes>-->
                <!--<include>**/*.xml</include>-->
            <!--</includes>-->
        <!--</resource>-->
    </resources>
    <plugins>
        <!-- 指定啓動類,將依賴打成外部jar包 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <!-- 生成的jar中,不要包含pom.xml和pom.properties這兩個檔案 -->
                    <addMavenDescriptor>false</addMavenDescriptor>
                    <manifest>
                        <!-- 是否要把第三方jar加入到類構建路徑 -->
                        <addClasspath>true</addClasspath>
                        <!-- 外部依賴jar包的最終位置 -->
                        <classpathPrefix>${project.name}/lib/</classpathPrefix>
                        <mainClass>com.epri.dcloud.data.LoopcloseriskanalyzeApplication</mainClass>
                    </manifest>
                    <!-- (組態檔外接目錄) -->
                    <manifestEntries>
                        <Class-Path>config/</Class-Path>
                    </manifestEntries>
                </archive>
                <excludes>
                    <exclude>**/*.properties</exclude>
                    <exclude>**/*.sh</exclude>
                    <exclude>**/*.xml</exclude>
                    <exclude>**/*.yml</exclude>
                </excludes>
                <outputDirectory>${project.build.directory}/${project.name}</outputDirectory>
            </configuration>
        </plugin>
        <!-- 拷貝依賴的jar包到lib目錄 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>
                            ${project.build.directory}/${project.name}/lib
                        </outputDirectory>
                        <excludeTransitive>false</excludeTransitive>
                        <stripVersion>false</stripVersion>
                        <includeScope>runtime</includeScope>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- 打包發佈時,跳過單元測試 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
    </plugins>
</build>

最後目錄:

bin目錄下啓動指令碼

 內容:

#JDK所在路徑
#JAVA_HOME="/home/dcenter/jdk1.8.0_91/bin/java"
#Java程式所在的目錄(classes的上一級目錄)
APP_HOME=$(dirname $(cd "$(dirname "$0")"; pwd))
cd ${APP_HOME}
#日誌寫入位置
LOG_HOME="${APP_HOME}/log"
#應用名
APP_NAME="loopcloser_risk"
#版本
APP_VERSION="1.0.0"
#拼湊完整的路徑
#APP="${APP_HOME}/${APP_NAME}-${APP_VERSION}.jar"
APP="${APP_HOME}/${APP_NAME}.jar"
echo ${APP}
#mkdir -p ${LOG_HOME}
#cd ${LOG_HOME}
#java虛擬機器啓動參數
#JAVA_OPTS=" -Dfile.encoding=UTF8 -Duser.timezone=GMT+08 -ms512m -mx512m -Xmn256m -Djava.awt.headless=true -XX:MaxPermSize=128m"
#JAVA_OPTS=" -ms512m -mx512m -Xmn256m -Djava.awt.headless=true -XX:MaxPermSize=128m"
JAR=$APP
prog=$APP_NAME
psid=0

checkpid() {
   javaps=`jps -l | grep $APP`
 
   if [ -n "$javaps" ]; then
      psid=`echo $javaps | awk '{print $1}'`
   else
      psid=0
   fi
}
start() {
   checkpid
 
   if [ $psid -ne 0 ]; then
      echo "================================"
      echo "warn: $APP_NAME already started! (pid=$psid)"
      echo "================================"
   else
      echo -n "Starting $APP_NAME ..."
      nohup java  -jar $APP >/dev/null  2>&1 &
      checkpid
      if [ $psid -ne 0 ]; then
         echo "(pid=$psid) [OK]"
      else
         echo "[Failed]"
      fi
   fi
}

stop() {
   

    checkpid
 
   if [ $psid -ne 0 ]; then
      echo -n "Stopping $APP_NAME ...(pid=$psid) "
       kill -9 $psid
      if [ $? -eq 0 ]; then
         echo "[OK]"
      else
         echo "[Failed]"
      fi
 
      checkpid
      if [ $psid -ne 0 ]; then
         stop
      fi
   else
      echo "================================"
      echo "warn: $APP_NAME is not running"
      echo "================================"
   fi
}

restart() {
   checkpid
 
   if [ $psid -ne 0 ];  then
      echo "$APP_NAME is running! (pid=$psid)"
   else
      echo "$APP_NAME is not running"
   fi
}

status() {
   pid=$(ps -ef | grep $JAR | grep -v 'grep ' | awk '{print $2}')

   if [  -e $pid ];then
      echo -e $"$prog hasn't run\t\t[OK]"
   else
      echo -e $"$prog is running\t\t[OK]"
   fi
}


case "$1" in
   start)
          start
          ;;
   stop)
          stop
          ;;
   status)
          status
          ;;
        restart)
                restart
                ;;

   *)
   echo -e "Usage: $0 {start|stop|status|restart}"
       exit 2
esac

啓動:

bash ./bin/loopclose.sh start