目錄
上一篇:springboot2.2.X手冊:Easypoi導出excel,最新版的手感香不香?
現在很多應用都是springboot,spring提供了一種富JAR的打包方式,直接打包後就可以運行,非常方便。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
同時有很多公司,也會經常打包成docker file,直接部署到阿里雲上,簡單明瞭,畢竟人家提供了一體化的部署運維方案。
但是如果只是單獨採用富JAR包進行部署,會有一定的操作麻煩性
1、富JAR包每次打包後,很大很大,差不多幾百兆,每次上傳到服務器都是非常辛苦
2、每次發佈都是全量發佈,假如只是修改一個配置文件,那此時也是要全量發佈,當系統很大的時候,風險就會更大,畢竟改少不如該多
3、項目交接出現斷檔,在項目中,經常是有人離職有人來新入職,如果原來的代碼是很老的,作爲一個新人進來,是否有足夠的膽量進行把這個項目全部替換,別說有什麼交接文檔之類,很多公司在進行技術管理的時候,都非常混亂,不單單是小公司,某世界500強的公司也見識過,非常痛苦。
4、沒有上發佈系統的公司,現在發佈系統比較普遍了,jekines非常好用,可惜小編還是喜歡阿里的自動化發佈,而且,jekines不支持灰度。
5、推薦一款發佈系統walle,瓦力部署,界面優雅,操作簡單,支持灰度。
資源分離的核心
當我們不採用docker,自己進行手動部署的時候,最好是lib與資源分離,因爲每次lib裏面加載的包體,非常多,數量會非常龐大,這樣子動不動就幾百M。
我們分離的目的,就是把lib分離處理,資源文件分離處理,不用每次發佈的時候,都重新把lib裏面的包體再打包一遍,畢竟,正常情況下,裏面的包體是不變的,我們需要改變的經常只有資源文件或者業務包,而資源文件加上業務包,也往往只有幾百K的大小,沒必要爲了這個一行代碼,把整個項目全部重新打包了。
其實這裏也是可以打包成war包的,這樣子就可以部署到tomcat上面去,但是呢,小編不是很想,畢竟這樣子做的話,我還需要去部署tomcat在服務器上,好麻煩的說,所以這裏引入了執行腳本來做,本質上跟執行springboot自帶的jar包的打包方式是一樣的。
資源文件分離的構建配置
今天我們來介紹另外一種打包方式,就是資源文件分離的方式,可以直接複製使用
需要改名的地方,我已經標註:需要改名
<build>
<plugins>
<!-- 打JAR包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<!-- 不打包資源文件(配置文件和依賴包分開) </excludes> -->
<excludes>
<exclude>logback-spring.xml</exclude>
<exclude>*.yml</exclude>
</excludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<!-- MANIFEST.MF 中 Class-Path 加入前綴 -->
<classpathPrefix>lib/</classpathPrefix>
<!-- jar包不包含唯一版本標識 -->
<useUniqueVersions>false</useUniqueVersions>
<!--指定入口類 需要改名 -->
<mainClass>com.boots.run.BootsApiApplication</mainClass>
</manifest>
<manifestEntries>
<!--MANIFEST.MF 中 Class-Path 加入資源文件目錄 -->
<Class-Path>./resources/</Class-Path>
</manifestEntries>
</archive>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</plugin>
<!-- 該插件的作用是用於複製依賴的jar包到指定的文件夾裏 -->
<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}/lib/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- 該插件的作用是用於複製指定的文件 -->
<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>
<includes>
<include>*.properties</include>
<include>*.xml</include>
<include>*.yml</include>
</includes>
<excludes>
<exclude>*.jar</exclude>
</excludes>
</resource>
</resources>
<outputDirectory>${project.build.directory}/resources</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- SpringBoot 打包插件,把 maven-jar-plugin 打成的jar包重新打成可運行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}/resources</outputDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<!--配置jar包特殊標識 配置後,保留原文件,生成新文件 *-run.jar -->
<!--配置jar包特殊標識 不配置,原文件命名爲 *.jar.original,生成新文件 *.jar -->
<!--<classifier>run</classifier> -->
</configuration>
</execution>
</executions>
</plugin>
<!-- 對打包後的文件重新進行組裝 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<!-- 定義app名稱+版本號 -->
<property name="appName">${project.artifactId}-${project.version}</property>
<!-- 定義運行文件總路徑 -->
<property name="run">target/run</property>
<!-- 定義靜態資源文件路徑 -->
<property name="res">target/run/resources</property>
<!-- 複製啓動文件:需要改名 -->
<copy file="${project.basedir}/target/${appName}.jar" tofile="${run}/boots-api.jar" />
<!-- 複製包文件 -->
<copy todir="${run}/lib">
<fileset dir="target/lib" />
</copy>
<!-- 複製資源文件 -->
<copy todir="${res}">
<fileset dir="target/resources">
<include name="**/*.properties" />
<include name="**/*.xml" />
<include name="**/*.yml" />
</fileset>
</copy>
<!-- 複製啓動腳本 -->
<copy file="${project.basedir}/runfile/startup-dev.sh" tofile="${run}/startup-dev.sh" />
<copy file="${project.basedir}/runfile/startup-test.sh" tofile="${run}/startup-test.sh" />
<copy file="${project.basedir}/runfile/startup-pro.sh" tofile="${run}/startup-pro.sh" />
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<!-- 資源複製 -->
<resources>
<!-- 把src裏面的xml文件加入到jar包裏面 -->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<!-- 把resources裏面的freemaker文件夾移除 -->
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>freemaker/**</exclude>
</excludes>
<filtering>true</filtering>
</resource>
</resources>
</build>
打包運行
打包運行後,會在target生產一個run包,裏面都是可以執行的sh文件
備註:sh文件裏面的項目名稱,需要自己去改名
資源文件分離的話,當改一個資源文件的時候,不用這個包重新打包,雖然我還是喜歡docker file,也提供一下這種方案。
--END--
作者:@溪雲閣
原創作品,抄襲必究,轉載註明出處
如需要源碼,轉發,關注後私信我
部分圖片或代碼來源網絡,如侵權請聯繫刪除,謝謝!