1、部署jar包的痛点
使用springboot打包的时候,springboot支持jar包和war包两种方式,个人感觉还是jar包的方式比war的方式
方便、简洁。但是在日常项目开发中依赖许多的第三方jar包,使得打成的jar包体积特别大。
部署jar包的时候,必须把整个jar传上去,如果服务器支持外网,带宽比较大,网速比较好还行,如果服务器
网络速度太慢,那么在部署的时候就比麻烦了。
面对这种场景,参考了一些资料,我觉得把第三方jar包和资源文件分离,在此做些记录。
2、如何分离lib和资源文件
我采用把项目自身 jar 文件打包在 zip 包根目录下,其他所有依赖包打包在lib文件夹下,
资源文件全部打包在resources文件目录下,项目运行的脚本文件打包在bin文件目录下。
打包前后项目结构变化
打包后,将src下面的打包成了jar,这个jar和一般打包方式的jar没什么区别,都是运行这个jar包。只是将项目中依赖的jar包,全部都放到了lib文件下,把资源文件都放到了对应的resources下。bin目录放了jar包启动的脚本文件。
打包的项目结构如下:
打包后的项目结构
操作步骤
- 配置pom.xml、修改build
<build>
<plugins>
<!--打包jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<!--不打包资源文件 -->
<excludes>
<exclude>bin/**</exclude>
<exclude>config/**</exclude>
<exclude>static/**</exclude>
<exclude>templates/**</exclude>
<exclude>*.yml</exclude>
</excludes>
<!--
<archive>
<manifest>
<addClasspath>true</addClasspath>
// MANIFEST.MF 中 Class-Path 加入前缀,不需要配置,在外部命令行指定
<classpathPrefix>lib/</classpathPrefix>
// jar包不包含唯一版本标识
<useUniqueVersions>false</useUniqueVersions>
` // 指定入口类,如果程序里只有一个main方法就建议不要手动配置
<mainClass>org.woodwhale.king.KingApplication</mainClass>
</manifest>
<manifestEntries>
// MANIFEST.MF 中 Class-Path 加入资源文件目录,不需要配置,在外部命令行指
<Class-Path>./resources/</Class-Path>
</manifestEntries>
</archive>
-->
<!--
生成到 target 目录下,
也可以放到指定目录,例如: ${project.build.directory}/boot
-->
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</plugin>
<!--拷贝依赖 copy-dependencies -->
<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>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<!--拷贝资源文件 copy-resources -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<encoding>UTF-8</encoding>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<outputDirectory>${project.build.directory}/resources</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!--
spring boot repackage,
依赖 maven-jar-plugin 打包的jar包
重新打包成 spring boot的jar包
-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 包含本地 jar 依赖 -->
<includeSystemScope>true</includeSystemScope>
<!-- 重写包含依赖,包含不存在的依赖,jar里没有pom里的依赖 -->
<includes>
<include>
<groupId>null</groupId>
<artifactId>null</artifactId>
</include>
</includes>
<layout>ZIP</layout>
<!-- 使用外部配置文件,jar包里没有资源文件 -->
<addResources>true</addResources>
<outputDirectory>${project.build.directory}</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-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<!-- 使用assembly打zip包 -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<!-- assembly配置文件位置 -->
<descriptor>assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
注意:在spring-boot-maven-plugin中配置了包含本地jar依赖的配置
<includeSystemScope>true</includeSystemScope>
- assembly.xml配置
在项目根目录下存在放置assembly.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.artifactId}</id>
<formats>
<format>zip</format>
</formats>
<!-- 压缩包下是否生成和项目名相同的根目录 -->
<includeBaseDirectory>false</includeBaseDirectory>
<baseDirectory>${project.artifactId}-${project.version}</baseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>${artifact.artifactId}-${artifact.version}.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>${file.separator}resources</outputDirectory>
</fileSet>
<fileSet>
<directory>../bin/</directory>
<outputDirectory>${file.separator}bin</outputDirectory>
</fileSet>
<fileSet>
<directory>lib/</directory>
<outputDirectory>${file.separator}lib</outputDirectory>
</fileSet>
</fileSets>
<!-- 使用项目的artifact,第三方 jar 打包进zip文件的 lib 目录 -->
<dependencySets>
<dependencySet>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<scope>runtime</scope>
<includes>
<include>*:*</include>
</includes>
<excludes>
<exclude>${groupId}:${artifactId}</exclude>
<exclude>org.springframework.boot:spring-boot-devtools</exclude>
</excludes>
</dependencySet>
</dependencySets>
</assembly>
- 配置脚本
脚本解释:最为关键的就是运行jar 文件的时候携带-Dloader.path=.,resources,lib
在bin下新建config.ini文件,指定 jar 的名称及版本信息
//jar包名称
ARTIFACTID=***
VERSION=0.0.1
新建windows下脚本,start.bat文件
ECHO off
ECHO "checking config.ini..."
SET ROOT=%~dp0
SET CONFIG_FILE=%ROOT%config.ini
REM **从配置文件中读取内容**
FOR /F "tokens=1,2 delims==" %%i IN (%CONFIG_FILE%) DO (
SET %%i=%%j
)
SET APP_NAME=%ARTIFACTID%-%VERSION%
IF "%APP_NAME%" == "" (
ECHO "this config.ini is not exist,please check this config file."
GOTO End
) ELSE (
ECHO "checking JAVA_HOME config from checking config.ini..."
GOTO OkPath
)
:OkPath
echo "check java_home..."
if not "%JAVA_HOME%" == "" GOTO OkJHome
:OkJHome
if exist "%JAVA_HOME%\bin\java.exe" GOTO Runserver
:Runserver
SET JAR_NAME=%APP_NAME%.jar
SET APP_CONFIG=-Dloader.path=.,resources,lib
ECHO "111:%RUN_JAVA%"
ECHO "Starting the %JAR_NAME%"
ECHO "%JAVA_HOME%\bin\java -Xms512m -Xmx512m -jar %APP_CONFIG% %JAR_NAME%"
CD ..
%JAVA_HOME%\bin\java.exe -Xms512m -Xmx512m -jar %APP_CONFIG% %JAR_NAME%
GOTO End
:End
PAUSE
新建linux下shell脚本,start.sh文件
shell 脚本里需要配置JAVA_HOME路径和 jar 文件名
#!/bin/sh
## java env
export JAVA_HOME=/usr/java/jdk1.8.0_162
API_NAME=./king-0.0.1.jar
export JRE_HOME=JAVA_HOME/jre
API_CONFIG=.,resources,lib
JAR_NAME=$API_NAME.jar
PID=$API_NAME.pid
usage() {
echo "Usage: sh startup.sh [start|stop|restart|status]"
exit 1
}
is_exist(){
pid=`ps -ef|grep $JAR_NAME|grep -v grep|awk '{print $2}' `
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}
start(){
is_exist
if [ $? -eq "0" ]; then
echo ">>> ${JAR_NAME} is already running PID=${pid} <<<"
else
nohup $JRE_HOME/bin/java -Xms256m -Xmx512m -jar -Dloader.path=$API_CONFIG $JAR_NAME >/dev/null 2>&1 &
echo $! > $PID
echo ">>> start $JAR_NAME successed PID=$! <<<"
fi
}
stop(){
#is_exist
pidf=$(cat $PID)
#echo "$pidf"
echo ">>> api PID = $pidf begin kill $pidf <<<"
kill $pidf
rm -rf $PID
sleep 2
is_exist
if [ $? -eq "0" ]; then
echo ">>> api 2 PID = $pid begin kill -9 $pid <<<"
kill -9 $pid
sleep 2
echo ">>> $JAR_NAME process stopped <<<"
else
echo ">>> ${JAR_NAME} is not running <<<"
fi
}
status(){
is_exist
if [ $? -eq "0" ]; then
echo ">>> ${JAR_NAME} is running PID is ${pid} <<<"
else
echo ">>> ${JAR_NAME} is not running <<<"
fi
}
restart(){
stop
start
}
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac
exit 0