springboot如何使用assembly打包項(xiàng)目和啟動(dòng)腳本
使用assembly打包項(xiàng)目和啟動(dòng)腳本
圖為項(xiàng)目的具體布局

其中bin 是 啟動(dòng)腳本目錄
assembly 是打包配置
pom.xml關(guān)鍵配置
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.6</version> <executions> <execution> <id>bundle</id> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <descriptors> <descriptor>src/main/assembly/repository.xml</descriptor> </descriptors> </configuration> </execution> </executions> </plugin>
repository.xml配置
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>project</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false
</includeBaseDirectory> <!-- disable the creation of root's distribution dir in the archive -->
<fileSets>
<!-- config files -->
<fileSet>
<directory>${basedir}/src/main/resources</directory>
<excludes></excludes>
<includes>
<include>*.properties</include>
<include>*.docx</include>
<include>*.xml</include>
</includes>
<fileMode>0644</fileMode>
<outputDirectory>config</outputDirectory>
</fileSet>
<fileSet>
<directory>${basedir}/src/main/resources/doc</directory>
<excludes></excludes>
<includes>
<include>*.docx</include>
</includes>
<fileMode>0644</fileMode>
<outputDirectory>config/doc</outputDirectory>
</fileSet>
<!-- scripts -->
<fileSet>
<directory>bin</directory>
<includes>
<include>*.sh</include>
</includes>
<fileMode>0755</fileMode>
<outputDirectory>/bin</outputDirectory>
</fileSet>
<!-- executable jar -->
<fileSet>
<directory>target</directory>
<outputDirectory>lib</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
</assembly>start.sh
#!/bin/sh
#功能簡(jiǎn)介:?jiǎn)?dòng)上層目錄下的jar文件
#參數(shù)簡(jiǎn)介:
# $1:jar文件名(包含后綴名)
# 注意:jar文件必須位于startup.sh目錄的上一層目錄。
#啟動(dòng)參數(shù)
#JAVA_OPTS="-server -Xms400m -Xmx400m -Xmn300m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -Xverify:none -XX:+DisableExplicitGC -Djava.awt.headless=true"
#JAR_NAME=`ls -lt ${parent_dir}/lib | head -2 | tail -1 | awk '{print $NF}'`
this_dir="$( cd "$( dirname "$0" )" && pwd )"
parent_dir=`dirname "${this_dir}"`
JAR_NAME=`ls -lt ${parent_dir}/lib | head -2 | tail -1 | awk '{print $NF}'`
log_dir="${parent_dir}/logs"
log_file="${log_dir}/catalina.out"
jar_file="${parent_dir}/lib/${JAR_NAME}"
echo -e "${jar_file}"
#參數(shù)個(gè)數(shù)<1或者參數(shù)空值時(shí),中斷執(zhí)行
#if [ $# -lt 1 ] || [ -z $1 ]; then
# echo -e "\033[31m請(qǐng)輸入要部署的jar包名稱(chēng)!\033[0m"
# exit 1
#fi
#日志文件夾不存在,則創(chuàng)建
if [ ! -d "${log_dir}" ]; then
mkdir "${log_dir}"
fi
#父目錄下jar文件存在
if [ -f "${jar_file}" ]; then
#啟動(dòng)jar包;重定向標(biāo)準(zhǔn)錯(cuò)誤輸出到文件,丟掉標(biāo)準(zhǔn)輸出
#java $JAVA_OPTS -jar ${jar_file} 1>/dev/null 2>"${log_file}" &
java -jar ${jar_file} --spring.config.location=${parent_dir}/config/application.properties 1>/dev/null 2>"${log_file}" &
exit 0
else
echo -e "\033[31m${jar_file}文件不存在!\033[0m"
exit 1
fispringboot assembly打包及windows執(zhí)行腳本
在使用assembly來(lái)打包springboot項(xiàng)目前,說(shuō)一說(shuō)springboot項(xiàng)目傳統(tǒng)方式打包部署時(shí)的幾個(gè)痛點(diǎn):
- 1.springboot的jar包中不包含項(xiàng)目依賴(lài)的jar包,無(wú)法直接java運(yùn)行,需要把依賴(lài)的jar包同時(shí)拷貝過(guò)去,并且在java中的-classpath參數(shù)中指定這些jar包;
- 2.配置文件application.yml也打包到j(luò)ar包中了,修改參數(shù)時(shí)很麻煩,先解壓出來(lái),修改,然后再放回jar包,部署一般都得改參數(shù),很繁瑣;
本文提供的打包方式,使得springboot項(xiàng)目的部署、執(zhí)行更加方便、簡(jiǎn)單。
本文采用assembly打包方式,生成zip包,部署時(shí)直接解壓zip包,執(zhí)行bin目錄的start文件就可以運(yùn)行。
1、pom.xml文件添加plugin
先修改pom文件的plugins,如下
? <plugin>
? ? <groupId>org.apache.maven.plugins</groupId>
? ? <artifactId>maven-jar-plugin</artifactId>
? ? <configuration>
? ? ? <archive>
? ? ? ? <manifest>
? ? ? ? ? <addClasspath>true</addClasspath>
? ? ? ? ? <classpathPrefix>lib/</classpathPrefix>
? ? ? ? ? <mainClass>xxx.xxx.xxx</mainClass>
? ? ? ? </manifest>
? ? ? </archive>
? ? </configuration>
? </plugin>
? <plugin>
? ? <groupId>org.apache.maven.plugins</groupId>
? ? <artifactId>maven-dependency-plugin</artifactId>
? ? <executions>
? ? ? <execution>
? ? ? ? <id>copy-lib</id>
? ? ? ? <phase>prepare-package</phase>
? ? ? ? <goals>
? ? ? ? ? <goal>copy-dependencies</goal>
? ? ? ? </goals>
? ? ? ? <configuration>
? ? ? ? ? <outputDirectory>${project.build.directory}/lib</outputDirectory>
? ? ? ? ? <overWriteReleases>false</overWriteReleases>
? ? ? ? ? <overWriteSnapshots>false</overWriteSnapshots>
? ? ? ? ? <overWriteIfNewer>true</overWriteIfNewer>
? ? ? ? ? <includeScope>compile</includeScope>
? ? ? ? </configuration>
? ? ? </execution>
? ? </executions>
? </plugin>
? <plugin>
? ? <artifactId>maven-assembly-plugin</artifactId>
? ? <configuration>
? ? ? <appendAssemblyId>false</appendAssemblyId>
? ? ? <descriptors>
? ? ? ? <descriptor>src/main/resources/assembly.xml</descriptor>
? ? ? </descriptors>
? ? </configuration>
? ? <executions>
? ? ? <execution>
? ? ? ? <id>make-assembly</id>
? ? ? ? <phase>package</phase>
? ? ? ? <goals>
? ? ? ? ? <goal>single</goal>
? ? ? ? </goals>
? ? ? </execution>
? ? </executions>
? </plugin>2、創(chuàng)建assembly.xml文件
在resource目錄下創(chuàng)建assembly.xml文件,配置文件放到/config目錄,執(zhí)行批處理文件放到/bin目錄,依賴(lài)jar包放到/lib目錄,執(zhí)行的jar包放到zip根目錄
內(nèi)容如下:
<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>${basedir}/src/main/resources</directory>
? ? ? ? <includes>
? ? ? ? ? ? <include>*.yml</include>
? ? ? ? </includes>
? ? ? ? <filtered>true</filtered>
? ? ? ? <outputDirectory>${file.separator}config</outputDirectory>
? ? </fileSet>
? ? <fileSet>
? ? ? ? <directory>src/main/resources/runScript</directory>
? ? ? ? <outputDirectory>${file.separator}bin</outputDirectory>
? ? </fileSet>
? ? <fileSet>
? ? ? ? <directory>${project.build.directory}/lib</directory>
? ? ? ? <outputDirectory>${file.separator}lib</outputDirectory>
? ? ? ? <includes>
? ? ? ? ? ? <include>*.jar</include>
? ? ? ? </includes>
? ? </fileSet>
? ? <fileSet>
? ? ? ? <directory>${project.build.directory}</directory>
? ? ? ? <outputDirectory>${file.separator}</outputDirectory>
? ? ? ? <includes>
? ? ? ? ? ? <include>*.jar</include>
? ? ? ? </includes>
? ? </fileSet>
</fileSets>
</assembly>3、編寫(xiě)執(zhí)行的批處理文件(或shell腳本)
在resource下建立runScript目錄,在runScript目錄建立Start.bat文件,腳本如下:
@echo on set LIB_JARS="" cd …\lib for %%i in (*) do ( set LIB_JARS=!LIB_JARS!;…\lib%%i ) echo !LIB_JARS! cd … set START_JARS="" for %%i in (*.jar) do ( set START_JARS=%%i ) echo !START_JARS! java -Xms64m -Xmx1024m -XX:MaxPermSize=64M -classpath …\config;%LIB_JARS% -jar %START_JARS% pause
到這步打包的zip包就可以直接解壓執(zhí)行了,不過(guò)springboot的jar還不干凈,配置文件、assembly.xml、resource下的runScript目錄都打包到j(luò)ar包里了,這些文件都需要排除,需要配置pom.xml的resources
4、springboot打包的jar包中排除不必要的文件及目錄
配置pom.xml的resources,如下:
<resources> ? <resource> ? ? <directory>src/main/resources</directory> ? ? <excludes> ? ? ? <exclude>**/application.yml</exclude> ? ? ? <exclude>**/assembly.xml</exclude> ? ? ? <exclude>runScript/**</exclude> ? ? </excludes> ? </resource> </resources>
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)時(shí)間片輪轉(zhuǎn)調(diào)度算法的示例代碼
時(shí)間片輪轉(zhuǎn)調(diào)度是一種最古老,最簡(jiǎn)單,最公平且使用最廣的算法,這篇文章主要為大家介紹了如何利用Java實(shí)現(xiàn)這一算法,需要的可以參考一下2023-07-07
ServletContext讀取web資源_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了ServletContext讀取web資源,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
SpringBoot整合TKMyBatis實(shí)現(xiàn)單表增刪改查操作
據(jù)說(shuō)tk.mybatis能夠讓我不寫(xiě)sql代碼就可以所有單表操作問(wèn)題,作為熱愛(ài)偷懶的我,怎么能放過(guò)這種機(jī)會(huì)。talk is cheap, show me the code。趕緊搞個(gè)例子爽一把先2023-01-01
如何利用Java輸出鏈表中倒數(shù)第k個(gè)結(jié)點(diǎn)
這篇文章主要給大家介紹了關(guān)于如何利用Java輸出鏈表中倒數(shù)第k個(gè)結(jié)點(diǎn)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-12-12
java中字符串轉(zhuǎn)整數(shù)及MyAtoi方法的實(shí)現(xiàn)
這篇文章主要介紹了java中字符串轉(zhuǎn)整數(shù)及MyAtoi方法的實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-05-05
Springboot+MDC+traceId日志中打印唯一traceId
本文主要介紹了Springboot+MDC+traceId日志中打印唯一traceId,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
Java多線(xiàn)程及分布式爬蟲(chóng)架構(gòu)原理解析
這篇文章主要介紹了Java多線(xiàn)程及分布式爬蟲(chóng)架構(gòu)原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10

