如果通過不同的IDE打包,着實會覺得依賴性太大,並且容易出現錯誤,操作也比較複雜
同時,spring-boot-maven-plugin的使用感覺,相關配置太少,並且無法滿足方便部署和運行的需求。
這裏我們使用了,Maven的如下插件 下載
maven-jar-plugin,負責將應用程序打包成可執行的jar文件
maven-assembly-plugin,負責將整個項目按照自定義的目錄結構打成最終的壓縮包,方便實際部署
需求1,將依賴的jar提取到可運行的jar文件之外,我們使用maven-jar-plugin來實現
比如我的項目最終的打包目錄如下 下載
代碼目錄結構如下
最終的可運行文件jar文件並不包含依賴的jar包,所有依賴的jar包都放在和ps.jar平行的lib文件夾內,這樣如果以後想快速部署,就不用每一次都把體積很大的lib包都要傳一遍,除非依賴包有所變化,當然這些都是後續如果想這麼做的前提,我這裏還是爲了使部署的文件比較規整
這裏的maven-jar-plugin的配置文件如下 下載
Xml代碼 下載
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<!-- 添加index則不從mainfest中讀取classpath,而是從Index.list中讀取 -->
<!-- <index>true</index> -->
<manifest>
<mainClass>com.vmpay.pay.App</mainClass>
<!-- to create a class path to your dependecies you have to fill true
in this field -->
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<!--<classpathLayoutType>custom</classpathLayoutType> <customClasspathLayout>
lib/$${artifact.groupId}.$${artifact.artifactId}.$${artifact.extension} </customClasspathLayout> -->
</manifest>
<manifestEntries>
<Class-Path>./</Class-Path>
</manifestEntries>
</archive>
<excludes>
<exclude>config/**</exclude>
</excludes>
</configuration>
</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代碼 下載
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<!-- not append assembly id in release file name -->
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/build/package.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
重點的就是package.xml的路徑了,使用maven-assembly-plugin的相關配置實際上都在這個文件裏面
package.xml的文件內容
Xml代碼 下載
<?xml version="1.0" encoding="UTF-8"?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>package</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>bin</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<scope>runtime</scope>
<!-- <unpack>false</unpack> -->
<excludes>
<!-- <exclude>${project.name}-${project.version}</exclude> -->
<exclude>${groupId}:${artifactId}</exclude>
</excludes>
</dependencySet>
</dependencySets>
</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代碼 下載
###啓動
#!/bin/sh
moduleName="ps"
pidPath="/var/run/$moduleName-tpid"
rm -f $pidPath
nohup java -jar ./$moduleName.jar -server -Xms1024m -Xmx2048m -Xss256k > ./run.log 2>&1 &
echo $! > $pidPath
stop.sh
Shell代碼 下載
###停止
moduleName="ps"
tpid=`cat /var/run/$moduleName-tpid | awk '{print $1}'`
tpid=`ps -aef | grep $tpid | awk '{print $2}' |grep $tpid`
if [ ${tpid} ]; then
kill -9 $tpid
fi