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格式,目录结构:
原码的目录结构
目标的目录结构
打包后的目录结构.PNG
2、将包上传到服务器,并且自动部署
三、pom.xml的配置
1、打包时添加一些常用信息到META-INF/MANIFEST.MF(添加Class-Path信息,设置主类MainClass)
pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3</version>
<configuration>
<finalName>${project.artifactId}</finalName>
<excludes>
<exclude>**/*.properties</exclude>
<exclude>**/assembly.xml</exclude>
</excludes>
<archive>
<index>1</index>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.zhengjia.Application</mainClass>
</manifest>
<manifestEntries>
<mode>development</mode>
<Class-Path>conf/</Class-Path>
<version>${project.version}</version>
</manifestEntries>
</archive>
</configuration>
</plugin>
2、assembly配置
pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<finalName>${project.artifactId}-${project.version}</finalName>
<descriptors>
<descriptor>src/main/resources/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>copy-scripts</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
assembly.xml
<assembly>
<id>bin</id>
<formats>
<format>zip</format>
</formats>
<dependencySets>
<dependencySet>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>src/main/bin/</directory>
<outputDirectory></outputDirectory>
<includes>
<include>startServer.sh</include>
</includes>
<fileMode>0755</fileMode>
<lineEnding>unix</lineEnding>
</fileSet>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*.jar</include>
</includes>
<fileMode>0755</fileMode>
</fileSet>
<fileSet>
<directory>src/main/resources/</directory>
<outputDirectory>conf/</outputDirectory>
<includes>
<include>application.properties</include>
<include>application-dev.properties</include>
<include>application-pro.properties</include>
</includes>
<fileMode>0755</fileMode>
</fileSet>
</fileSets>
</assembly>
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
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>wagon-maven-plugin</artifactId>
<version>1.0</version>
</dependency>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>wagon-maven-plugin</artifactId>
<version>1.0</version>
<configuration>
<serverId>webserver</serverId>
<fromFile>${project.build.directory}/${project.artifactId}-${project.version}.zip</fromFile>
<url>scp://ip:port/bigscreen-api/zhengjia-api/</url>
<commands>
<command>cd /bigscreen-api/zhengjia-api/${project.artifactId}-${project.version}/;ls -al;./startServer.sh stop</command>
<command>rm -rf /bigscreen-api/zhengjia-api/${project.artifactId}-${project.version}</command>
<command>unzip /bigscreen-api/zhengjia-api/${project.artifactId}-${project.version}.zip -d /bigscreen-api/zhengjia-api/</command>
<command>cd /bigscreen-api/zhengjia-api/${project.artifactId}-${project.version};source /etc/profile;./startServer.sh start;ps -ef|grep ${project.artifactId}</command>
</commands>
<displayCommandOutputs>true</displayCommandOutputs>
</configuration>
</plugin>
<extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ssh</artifactId>
<version>2.10</version>
</extension>
</extensions>
maven中的setting
<server>
<id>webserver</id>
<username>root</username>
<password>*********</password>
</server>
四、执行命令,触发自动打包部署
mvn clean package wagon:upload-single wagon:sshexec
网友评论