使用springboot打包的时候,springboot支持jar包和war包两种方式,个人感觉还是jar包的方式比war的方式
方便、简洁。但是在日常项目开发中依赖许多的第三方jar包,使得打成的jar包体积特别大。
部署jar包的时候,必须把整个jar传上去,如果服务器支持外网,带宽比较大,网速比较好还行,如果服务器
网络速度太慢,那么在部署的时候就比麻烦了。
面对这种场景,参考了一些资料,我觉得把第三方jar包和资源文件分离,在此做些记录。
我采用把项目自身 jar 文件打包在 zip 包根目录下,其他所有依赖包打包在lib文件夹下,
资源文件全部打包在resources文件目录下,项目运行的脚本文件打包在bin文件目录下。
打包后,将src下面的打包成了jar,这个jar和一般打包方式的jar没什么区别,都是运行这个jar包。只是将项目中依赖的jar包,全部都放到了lib文件下,把资源文件都放到了对应的resources下。bin目录放了jar包启动的脚本文件。
org.apache.maven.plugins
maven-jar-plugin
bin/**
config/**
static/**
templates/**
*.yml
${project.build.directory}
org.apache.maven.plugins
maven-dependency-plugin
copy-dependencies
package
copy-dependencies
${project.build.directory}/lib
false
false
true
org.apache.maven.plugins
maven-resources-plugin
copy-resources
package
copy-resources
UTF-8
src/main/resources
${project.build.directory}/resources
org.springframework.boot
spring-boot-maven-plugin
true
null
null
ZIP
true
${project.build.directory}
repackage
org.apache.maven.plugins
maven-surefire-plugin
true
maven-assembly-plugin
false
assembly.xml
make-assembly
package
single
注意:在spring-boot-maven-plugin中配置了包含本地jar依赖的配置
true
${project.artifactId}
zip
false
${project.artifactId}-${project.version}
${project.build.directory}
${artifact.artifactId}-${artifact.version}.jar
src/main/resources
${file.separator}resources
../bin/
${file.separator}bin
lib/
${file.separator}lib
true
lib
runtime
*:*
${groupId}:${artifactId}
org.springframework.boot:spring-boot-devtools
在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