通過Maven構建打包Spring boot,並將config配置文件提取到jar文件外

如果通過不同的IDE打包,着實會覺得依賴性太大,並且容易出現錯誤,操作也比較複雜 

同時,spring-boot-maven-plugin的使用感覺,相關配置太少,並且無法滿足方便部署和運行的需求。 


這裏我們使用了,Maven的如下插件 下載 

maven-jar-plugin,負責將應用程序打包成可執行的jar文件 
maven-assembly-plugin,負責將整個項目按照自定義的目錄結構打成最終的壓縮包,方便實際部署 



需求1,將依賴的jar提取到可運行的jar文件之外,我們使用maven-jar-plugin來實現 

比如我的項目最終的打包目錄如下 下載 
02dce810-ef41-337d-8277-b44e7ccbf58e.png 
代碼目錄結構如下 
1508ba27-b384-3d7e-8426-9218f2bc0cdc.png 

最終的可運行文件jar文件並不包含依賴的jar包,所有依賴的jar包都放在和ps.jar平行的lib文件夾內,這樣如果以後想快速部署,就不用每一次都把體積很大的lib包都要傳一遍,除非依賴包有所變化,當然這些都是後續如果想這麼做的前提,我這裏還是爲了使部署的文件比較規整 
這裏的maven-jar-plugin的配置文件如下 下載 

Xml代碼  下載 

  1. <plugin>  

  2.                 <groupId>org.apache.maven.plugins</groupId>  

  3.                 <artifactId>maven-jar-plugin</artifactId>  

  4.                 <version>2.6</version>  

  5.                 <configuration>  

  6.                     <archive>  

  7.                     <!-- 添加index則不從mainfest中讀取classpath,而是從Index.list中讀取 -->  

  8. <!--                         <index>true</index> -->  

  9.                         <manifest>  

  10.                             <mainClass>com.vmpay.pay.App</mainClass>  

  11.                             <!-- to create a class path to your dependecies you have to fill true   

  12.                                 in this field -->  

  13.                             <addClasspath>true</addClasspath>  

  14.                             <classpathPrefix>lib/</classpathPrefix>  

  15.                             <!--<classpathLayoutType>custom</classpathLayoutType> <customClasspathLayout>   

  16.                                 lib/$${artifact.groupId}.$${artifact.artifactId}.$${artifact.extension} </customClasspathLayout> -->  

  17.                         </manifest>  

  18.                         <manifestEntries>  

  19.                             <Class-Path>./</Class-Path>  

  20.                         </manifestEntries>  

  21.                     </archive>  

  22.                     <excludes>  

  23.                         <exclude>config/**</exclude>  

  24.                     </excludes>  

  25.                 </configuration>  

  26.             </plugin>  


其中manifest的部分是核心,在可執行的jar文件中,打包後會在jar文件內的META-INF文件夾下,生成一個MANIFEST.MF文件,裏面記錄了可執行文件的一些相關配置,比如像上面一段代碼中所配置的內容,這裏面就配置了可執行jar文件未來讀取classpath的相對目錄位置在什麼地方,以及引入的jar文件都有哪些,上面的配置就是classpath目錄是./(稍後會解釋爲什麼) 
mainClass配置表示,哪個class作爲程序的入口來執行 
addClasspath配置表示,是否將依賴的classpath一起打包 
classpathPrefix配置表示,依賴的classpath的前綴,也就是打包後生成的MANIFEST.MF文件裏,引入的jar文件都會加上前綴,lib/,比如fastjson-1.2.7.jar,在mainfest文件裏就會是lib/fastjson-1.2.7.jar 
excludes配置表示,排除哪些文件夾不被打包進去 

其實maven-jar-plugin主要就是配置了MANIFEST.MF這個文件而已,就是讓可執行文件知道自己怎麼執行,加載哪些文件執行的描述,剩下的工作交由maven-assembly-plugin來處理 

在pom文件中配置類似如下 

Xml代碼  下載 

  1. <plugin>  

  2.                 <artifactId>maven-assembly-plugin</artifactId>  

  3.                 <configuration>  

  4.                     <!-- not append assembly id in release file name -->  

  5.                     <appendAssemblyId>false</appendAssemblyId>  

  6.                     <descriptors>  

  7.                         <descriptor>src/main/build/package.xml</descriptor>  

  8.                     </descriptors>  

  9.                 </configuration>  

  10.                 <executions>  

  11.                     <execution>  

  12.                         <id>make-assembly</id>  

  13.                         <phase>package</phase>  

  14.                         <goals>  

  15.                             <goal>single</goal>  

  16.                         </goals>  

  17.                     </execution>  

  18.                 </executions>  

  19.             </plugin>  



重點的就是package.xml的路徑了,使用maven-assembly-plugin的相關配置實際上都在這個文件裏面 
package.xml的文件內容 

Xml代碼  下載 

  1. <?xml version="1.0" encoding="UTF-8"?>  

  2. <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

  3.   xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">  

  4.     <id>package</id>  

  5.     <formats>  

  6.         <format>zip</format>  

  7.     </formats>  

  8.     <includeBaseDirectory>true</includeBaseDirectory>  

  9.     <fileSets>  

  10.         <fileSet>  

  11.             <directory>bin</directory>  

  12.             <outputDirectory>/</outputDirectory>  

  13.         </fileSet>  

  14.         <fileSet>  

  15.             <directory>src/main/resources</directory>  

  16.             <outputDirectory>/</outputDirectory>  

  17.         </fileSet>  

  18.         <fileSet>  

  19.             <directory>${project.build.directory}</directory>  

  20.             <outputDirectory>/</outputDirectory>  

  21.             <includes>  

  22.                 <include>*.jar</include>  

  23.             </includes>  

  24.         </fileSet>  

  25.     </fileSets>  

  26.     <dependencySets>  

  27.         <dependencySet>  

  28.             <outputDirectory>lib</outputDirectory>  

  29.             <scope>runtime</scope>  

  30. <!--             <unpack>false</unpack> -->  

  31.             <excludes>  

  32. <!--                 <exclude>${project.name}-${project.version}</exclude> -->  

  33.                 <exclude>${groupId}:${artifactId}</exclude>  

  34.             </excludes>  

  35.         </dependencySet>  

  36.     </dependencySets>  

  37. </assembly>  


其他相關配置可參看官方文檔 下載 
[url] 
http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html#class_unpackOptions 
[/url] 

這裏面我配置了,最終壓縮的文件格式,爲zip,也就是最終打包出來的是一個zip的文件,然後發佈到服務器上進行解壓部署,相關我要的配置都在這個壓縮包內,解壓即可直接使用 

下面的fileSets中配置了我需要將那些文件打包到我的最終壓縮包中, 
我的配置文件包括了啓動腳本bin文件夾,裏面放着shell的啓動腳本, 
相關的配置文件src/main/resources,裏面放着整個程序提取的properties等相關的配置文件 
最終可運行的jar文件,使用了${project.build.directory}變量,也就是通過maven-jar-plugin生成的那個jar文件 
dependencySets裏面配置了依賴庫最終輸出到lib文件夾下,與上面的maven-jar-plugin配置生成的manifest文件路徑相對應,這樣可運行jar就會按照manifest的路徑來找相應的文件進行加載 


start.sh 

Shell代碼  下載 

  1. ###啓動  

  2.   

  3. #!/bin/sh  

  4.   

  5. moduleName="ps"  

  6. pidPath="/var/run/$moduleName-tpid"  

  7.   

  8. rm -f $pidPath  

  9.   

  10. nohup java -jar ./$moduleName.jar -server -Xms1024m -Xmx2048m -Xss256k > ./run.log 2>&1 &  

  11.   

  12. echo $! > $pidPath  


stop.sh 

Shell代碼  下載 

  1. ###停止  

  2.   

  3. moduleName="ps"  

  4.   

  5. tpid=`cat /var/run/$moduleName-tpid | awk '{print $1}'`  

  6. tpid=`ps -aef | grep $tpid | awk '{print $2}' |grep $tpid`  

  7. if [ ${tpid} ]; then  

  8. kill -9 $tpid  

  9. fi  



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章