springboot中自动化打包部署
目录
一、maven插件
二、目的
--1、打包成zip格式,目录结构
--2、将包上传到服务器,并且自动部署
三、pom.xml的配置
--1、打包时添加一些常用信息到META-INF/MANIFEST.MF
--2、assembly配置
--3、shell脚本
--4、自动化部署配置
四、执行命令,触发自动打包部署
一、maven插件
plugin | function |
---|---|
maven-jar-plugin | maven 默认打包插件,用来创建 project jar,负责将应用程序打包成可执行的jar文件。可在此处设置主类,manifest,排除对应的配置文件等 |
maven-assembly-plugin | 支持定制化打包方式,负责将整个项目按照自定义的目录结构打成最终的压缩包,方便实际部署。可在此处设置打包拷贝路径,配置,以及打包好的jar文件等 |
wagon-maven-plugin | 将包上传到服务器,并支持定制shell命令执行 |
二、目的
1、打包成zip格式,目录结构:
原码的目录结构
目标的目录结构
2、将包上传到服务器,并且自动部署
三、pom.xml的配置
1、打包时添加一些常用信息到META-INF/MANIFEST.MF(添加Class-Path信息,设置主类MainClass)
pom.xml
org.apache.maven.plugins
maven-jar-plugin
2.3
${project.artifactId}
**/*.properties
**/assembly.xml
1
true
true
true
lib/
com.zhengjia.Application
development
conf/
${project.version}
2、assembly配置
pom.xml
org.apache.maven.plugins
maven-assembly-plugin
2.5.5
false
${project.artifactId}-${project.version}
src/main/resources/assembly.xml
copy-scripts
package
single
assembly.xml
bin
zip
true
lib
false
src/main/bin/
startServer.sh
0755
unix
${project.build.directory}
*.jar
0755
src/main/resources/
conf/
application.properties
application-dev.properties
application-pro.properties
0755
3、shell脚本
#!/usr/bin/env bash
#@Copyright (C) Locket, Inc.
SERVER_NAME=zhengjia-api
#脚本文件名称
FINDNAME=$0
#检查脚本是否存在
#FINDNAME=`ls -ld $FINDNAME | awk '{print $NF}'` #查找文件,管道后取最后一个字段作为文件名
while [ -h $FINDNAME ] ; do FINDNAME=`ls -ld $FINDNAME | awk '{print $NF}'` ; done
APP_HOME=`echo $FINDNAME | sed -e 's@/[^/]*$@@'`
unset FINDNAME
if [ "$APP_HOME" = '.' ]; then
APP_HOME=$(echo `pwd` | sed 's/\/bin//')
else
APP_HOME=$(echo $APP_HOME | sed 's/\/bin//')
fi
echo "APP_HOME: $APP_HOME"
# Create pid file
# -d 检测文件是否是目录,如果是,则返回 true。
if [ ! -d "$APP_HOME"/pids ]; then
mkdir "$APP_HOME"/pids
fi
HEAP_MEMORY=512m
MAX_HEAP_MEMORY=2048m
PERM_MEMORY=256m
MAX_PERM_MEMORY=512m
PIDFILE="$APP_HOME"/pids/$SERVER_NAME.pid
#echo "PIDFILE = $PIDFILE"
start() {
if test -e "$PIDFILE"
then
if test -d "/proc"/$(cat "$PIDFILE")
then
echo "$SERVER_NAME service still running, please stop it first"
exit 1
fi
else
touch "$PIDFILE"
fi
if test -e "$APP_HOME"/$SERVER_NAME.jar
then
echo "starting $SERVER_NAME ... "
JAVA_OPTS="-server -XX:+HeapDumpOnOutOfMemoryError "
shift
ARGS=($*)
for ((i=0; i<${#ARGS[@]}; i++)); do
case "${ARGS[$i]}" in
-D*) JAVA_OPTS="${JAVA_OPTS} ${ARGS[$i]}" ;;
-Heap*) HEAP_MEMORY="${ARGS[$i+1]}" ;;
-Perm*) PERM_MEMORY="${ARGS[$i+1]}" ;;
esac
done
JAVA_OPTS="${JAVA_OPTS} -Xms${HEAP_MEMORY} -Xmx${MAX_HEAP_MEMORY} -XX:PermSize=${PERM_MEMORY} -XX:MaxPermSize=${MAX_PERM_MEMORY} -Dapp.name=$SERVER_NAME"
echo "start jvm args ${JAVA_OPTS}"
nohup java $JAVA_OPTS -Dfile.encoding=UTF-8 -Duser.dir="$APP_HOME" -jar "."/$SERVER_NAME.jar >/dev/null 2>&1 &
echo $! > "$PIDFILE"
echo "started $SERVER_NAME OK"
else
echo "could not find $SERVER_NAME"
exit 1
fi
}
stop() {
if test -e "$PIDFILE"
then
echo "stopping server"
if kill -TERM `cat "$PIDFILE"`
then
sleep 2
loop_check_process_status 3
elif
kill -KILL `cat "$PIDFILE"`
then
sleep 2
loop_check_process_status 3
elif
kill -9 `cat "$PIDFILE"`
then
echo "server stop OK"
/bin/rm "$PIDFILE"
fi
else
echo "no server running"
exit 1
fi
}
restart() {
echo "restarting server"
stop
start "$1"
}
info() {
echo "System Information:"
echo "****************************"
echo `head -n 1 /etc/issue`
echo `uname -a`
echo
echo "JAVA_HOME=$JAVA_HOME"
echo `$JAVA_HOME/bin/java -version`
echo
echo "APP_HOME=$APP_HOME"
}
loop_check_process_status() {
COUNT=$1
KILLED=false
for (( i = 0; i < $COUNT; i++));do
if test -d "$APP_HOME"/$(cat "$PIDFILE")
then
sleep 10
else
KILLED=true
break
fi
done
if $KILLED; then
echo "server stop OK"
/bin/rm "$PIDFILE"
else
echo "server stop failed"
fi
}
case $1 in
start)
start "$2"
;;
stop)
stop
;;
restart)
restart "$2"
;;
info)
info
;;
*)
echo "Usage: $0 {start|stop|restart}" >&2
;;
esac
exit 0
使用
./startServer.sh (start|stop|restart)
4、自动化部署配置
pom.xml
org.codehaus.mojo
wagon-maven-plugin
1.0
org.codehaus.mojo
wagon-maven-plugin
1.0
webserver
${project.build.directory}/${project.artifactId}-${project.version}.zip
scp://ip:port/bigscreen-api/zhengjia-api/
cd /bigscreen-api/zhengjia-api/${project.artifactId}-${project.version}/;ls -al;./startServer.sh stop
rm -rf /bigscreen-api/zhengjia-api/${project.artifactId}-${project.version}
unzip /bigscreen-api/zhengjia-api/${project.artifactId}-${project.version}.zip -d /bigscreen-api/zhengjia-api/
cd /bigscreen-api/zhengjia-api/${project.artifactId}-${project.version};source /etc/profile;./startServer.sh start;ps -ef|grep ${project.artifactId}
true
org.apache.maven.wagon
wagon-ssh
2.10
maven中的setting
webserver
root
*********
四、执行命令,触发自动打包部署
mvn clean package wagon:upload-single wagon:sshexec