写在前面
基于SpringBoot框架开发的代码,打包时默认将配置文件等信息全部打包进一个JAR包中,当仅仅需要修改配置文件时就变的比较麻烦,那怎么办呢?通过assembly则可以解决该问题。
具体步骤
①. 新增打包插件
<plugin>
<!-- 添加MAVEN提供的ASSEMBLY插件,默认使用spring-boot-maven-plugin打包,将项目所有的依赖打入到项目的JAR包里-->
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!--绑定到MAVEN的操作类型上-->
<phase>package</phase>
<!--仅仅运行一次-->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
②. 新增assembly.xml配置文件
配置文件的路径为:src/main/assembly/assembly.xml
③. 编写assembly.xml配置文件的内容
<assembly>
<!--
ID标签必须指定,ID会追加到打包的名字的末尾,项目名为:servicex-test-0.0.1-SNAPSHOT,
打包后的名字为:servicex-test-0.0.1-SNAPSHOT-bin.tar.gz
-->
<id>bin</id>
<!-- 打包文件的类型,如果要输出多个类型的打包文件,则定义多个 -->
<formats>
<format>tar.gz</format>
<!--<format>tar</format>-->
<!--<format>zip</format>-->
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<!--文件复制与设置-->
<fileSets>
<!--
0755: 即用户具有读/写/执行权限,组用户和其它用户具有读写权限;
0644: 即用户具有读写权限,组用户和其它用户具有只读权限;
-->
<!-- 1. 将src/main/assembly/bin目录下的所有文件输出到打包后的bin目录中 -->
<fileSet>
<directory>src/main/assembly/bin</directory>
<outputDirectory>bin</outputDirectory>
<fileMode>0755</fileMode>
<!--如果是执行脚本,一定要改为unix,否则当在windows上面时,脚本会出现dos编写问题-->
<lineEnding>unix</lineEnding>
<!-- 是否进行属性替换 -->
<filtered>true</filtered>
</fileSet>
<!-- 2. 将src/main/assembly/sql目录下的所有文件输出到打包后的sql目录中 -->
<fileSet>
<directory>src/main/assembly/sql</directory>
<outputDirectory>sql</outputDirectory>
<fileMode>0644</fileMode>
</fileSet>
<!-- 3. 将src/main/resources下配置文件打包到config目录 -->
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>/config</outputDirectory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
<include>**/*.yml</include>
</includes>
<!-- 是否进行属性替换 -->
<filtered>true</filtered>
</fileSet>
<!-- 4. 将项目启动JAR打包到lib目录中 -->
<fileSet>
<directory>target</directory>
<outputDirectory>lib</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
<!-- 5. 将项目说明文档打包到doc目录中 -->
<fileSet>
<directory>.</directory>
<outputDirectory>doc</outputDirectory>
<includes>
<include>*.md</include>
</includes>
<fileMode>0644</fileMode>
</fileSet>
<!-- 6. 将docs目录下的所有文件输出到打包后的doc目录中 -->
<fileSet>
<directory>doc</directory>
<outputDirectory>doc</outputDirectory>
<fileMode>0644</fileMode>
</fileSet>
<!-- 7. 将src/main/assembly/doc目录下的所有文件输出到打包后的doc目录中 -->
<fileSet>
<directory>src/main/assembly/doc</directory>
<outputDirectory>doc</outputDirectory>
<fileMode>0644</fileMode>
</fileSet>
</fileSets>
</assembly>
④. 定义服务的启停脚本
#!/bin/bash
# 定义变量 ##############################
# 项目名称
SERVER_NAME="${project.artifactId}"
# JAR名称
JAR_NAME="${project.build.finalName}.jar"
# 进入bin目录
cd `dirname $0`
# bin目录绝对路径
BIN_DIR=`pwd`
# 返回到上一级项目根目录路径
cd ..
# 打印项目根目录绝对路径
# `pwd` 执行系统命令并获得结果
DEPLOY_DIR=`pwd`
# 外部配置文件绝对目录,如果是目录需要/结尾,也可以直接指定文件
# 如果指定的是目录,spring则会读取目录中的所有配置文件
CONF_DIR=$DEPLOY_DIR/conf
# 加载外部log4j2文件的配置
LOG_BACK_FILE=logback.xml
echo "The command is as follow..."
echo "ps -ef | grep java | grep $JAR_NAME | grep -v grep | awk '{print \$2}'"
# 如果输入参数不对,给出提示
usage() {
echo "WARNING......"
echo "Tips, please use command: sh servicex-test.sh [start|stop|restart|status]. "
echo "For example: sh servicex-test.sh start "
exit 1
}
# 启动
start() {
PIDS=`ps -ef | grep java | grep "$JAR_NAME" | grep -v grep | awk '{print $2}'`
if [ -n "$PIDS" ]; then
echo "ERROR: The $SERVER_NAME already started!"
echo "THE PID IS: $PIDS"
exit 1
fi
# JVM Configuration
JAVA_OPTS=" -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true "
JAVA_MEM_OPTS=""
BITS=`java -version 2>&1 | grep -i 64-bit`
if [ -n "$BITS" ]; then
# 64位
JAVA_MEM_OPTS=" -server -Xmx512m -Xms512m -Xmn256m -XX:PermSize=128m -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 "
else
JAVA_MEM_OPTS=" -server -Xms512m -Xmx512m -XX:PermSize=128m -XX:SurvivorRatio=2 -XX:+UseParallelGC "
fi
# 日志配置文件
LOG_BACK_CONFIG=""
if [ -f "$CONF_DIR/$LOG_BACK_FILE" ]
then
LOG_BACK_CONFIG="-Dlogging.config=$CONF_DIR/$LOG_BACK_FILE"
fi
CONFIG_FILES=" $LOG_BACK_CONFIG -Dspring.config.location=$CONF_DIR/ "
echo -e "Starting the $SERVER_NAME ..."
nohup java $JAVA_OPTS $JAVA_MEM_OPTS $CONFIG_FILES -jar $DEPLOY_DIR/$JAR_NAME > /dev/null 2>&1 &
echo "The $SERVER_NAME started successfully..."
PIDS=`ps -ef | grep java | grep "$JAR_NAME" | grep -v grep | awk '{print $2}'`
echo "THE PID IS: $PIDS"
exit 0
}
# 停止
stop() {
PIDS=`ps -ef | grep java | grep "$JAR_NAME" | grep -v grep | awk '{print $2}'`
if [[ -z "$PIDS" ]]
then
echo "The $SERVER_NAME is stopped..."
else
echo -e "Stopping the $SERVER_NAME ..."
echo kill ${PIDS}
kill -9 ${PIDS}
echo "The $SERVER_NAME stopped successfully..."
exit 0
fi
}
# 状态
status() {
PIDS=`ps -ef | grep java | grep "$JAR_NAME" | grep -v grep | awk '{print $2}'`
if [ -n "$PIDS" ]; then
echo "The $SERVER_NAME is running..."
echo "THE PID IS: $PIDS"
exit 0
else
echo "The $SERVER_NAME is stopped..."
exit 0
fi
}
# 重启
restart() {
stop
start
}
# 根据输入参数执行对应方法,不输入则执行tips提示方法
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac
⑤. 项目的目录结构
⑥. 按常规方式打包
⑦. 输出项目发布包:servicex-test-1.0-SNAPSHOT-bin.tar.gz
版权声明:本文为goodjava2007原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。