springboot 提取项目配置文件、避免被打入 jar 包中(打入 jar 包中不便于部署和运维时修改)
提取项目配置文件、避免被打入 jar 包中(打入 jar 包中不便于部署和运维时修改)
操作步骤:
一、按照以下配置pom.xml,
二、配置 src/main/assembly/assembly.xml,
三、使用maven打包后,得到 exp-web-1.2.1-SNAPSHOT-assembly.zip 压缩包,
四、在linux下解压到需要部署项目的路径下, 运行bin/start.sh 即可运行。
五、conf为分离出来的配置文件,lib为分离出来的jar,bin为运行脚本,
每次可只替换修改的jar即可
1. pom.xml文件添加
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> <resource> <directory>src/main/scripts</directory> <filtering>true</filtering> </resource> </resources> <plugins> <!-- 提取项目配置文件、避免被打入 jar 包中(打入 jar 包中不便于部署和运维时修改) --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <excludes> <exclude>assembly.xml</exclude> <exclude>*.sh</exclude> <exclude>application*.yml</exclude> <exclude>logback-spring.xml</exclude> <exclude>redisson.yaml</exclude> <exclude>config.properties</exclude> <exclude>static/**</exclude> <exclude>attachment/**</exclude> </excludes> </configuration> <executions> <execution> <id>make-jar</id> <phase>package</phase> </execution> </executions> </plugin> <!-- 抽取第三方依赖 jar 包,到独立 lib 目录中 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.6</version> <executions> <execution> <!-- this is used for inheritance merges --> <id>make-tar</id> <!-- bind to the packaging phase --> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <skipAssembly>false</skipAssembly> <descriptors> <descriptor>src/main/assembly/assembly.xml</descriptor> </descriptors> </configuration> </execution> </executions> </plugin> </plugins> <finalName>experiment</finalName> </build>
2. 单独集成 assembly.xml
<assembly> <id>assembly</id> <formats> <!-- 其他可选格式 gzip/zip/tar.gz/ --> <format>zip</format> </formats> <includeBaseDirectory>true</includeBaseDirectory> <dependencySets> <dependencySet> <outputDirectory>lib</outputDirectory> </dependencySet> </dependencySets> <fileSets> <fileSet> <directory>src/main/scripts</directory> <outputDirectory>bin</outputDirectory> <fileMode>0755</fileMode> <filtered>false</filtered> <lineEnding>unix</lineEnding> <excludes> <exclude>*.sh.*</exclude> </excludes> </fileSet> <fileSet> <directory>src/main/resources</directory> <excludes> <exclude>banner.txt</exclude> </excludes> <outputDirectory>conf</outputDirectory> <fileMode>0644</fileMode> <filtered>false</filtered> </fileSet> </fileSets> </assembly>
3. 启动脚本 start.sh, stop.sh
start.sh
#!/bin/bash cd `dirname $0` BIN_DIR=`pwd` cd .. DEPLOY_DIR=`pwd` CONF_DIR=$DEPLOY_DIR/conf DATE_DIR=`date +%Y-%m-%d` LOG_FILE=$DEPLOY_DIR/logs/$DATE_DIR/experiment-info-log.log #com/xy/experiment/ExperimentApplication.java APPLICATION_CLASS="com.xy.experiment.ExperimentApplication" # SERVER_NAME SERVER_NAME="exp-web" if [ -z "$SERVER_NAME" ]; then SERVER_NAME=`hostname` fi PIDS=`ps -ef | grep java | grep "$CONF_DIR" |awk '{print $2}'` if [ -n "$PIDS" ]; then echo "[ERROR] The $SERVER_NAME already started!" echo "PID: $PIDS" exit 1 fi LOGS_DIR="" if [ -n "$LOGS_FILE" ]; then LOGS_DIR=`dirname $LOGS_FILE` else LOGS_DIR=$DEPLOY_DIR/logs fi if [ ! -d $LOGS_DIR ]; then mkdir $LOGS_DIR fi JAVA_OPTS=" -Djava.net.preferIPv4Stack=true -Dfile.encoding=utf-8" JAVA_DEBUG_OPTS="" if [ "$1" = "debug" ]; then JAVA_DEBUG_OPTS=" -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n " fi JAVA_MEM_OPTS="" BITS=`java -version 2>&1 | grep -i 64-bit` if [ -n "$BITS" ]; then JAVA_MEM_OPTS=" -server -DJM.LOG.PATH=$LOGS_DIR -XX:MaxDirectMemorySize=256M -Xmx512M -Xms512M -XX:MaxMetaspaceSize=512M -XX:MetaspaceSize=512M -XX:+ExplicitGCInvokesConcurrentAndUnloadsClasses -XX:+CMSClassUnloadingEnabled -XX:+ParallelRefProcEnabled -XX:+CMSScavengeBeforeRemark -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=../logs/dump.hprof -XX:+PrintGCDetails -Xloggc:./logs/gc.log -XX:+PrintGCTimeStamps " else JAVA_MEM_OPTS=" -server -Xms1g -Xmx1g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -XX:SurvivorRatio=2 -XX:+UseParallelGC " fi echo -e "Starting the $SERVER_NAME ..." nohup java -Dapplication.name=$SERVER_NAME $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_DEBUG_OPTS -classpath $DEPLOY_DIR/lib/sp-dubbo-2.3.0.jar:$DEPLOY_DIR/conf:$DEPLOY_DIR/lib/* $APPLICATION_CLASS > /dev/null 2>&1 & COUNT=0 while [ $COUNT -lt 1 ]; do echo -e ".\c" sleep 1 COUNT=`ps -f | grep java | grep "$DEPLOY_DIR" | awk '{print $2}' | wc -l` if [ $COUNT -gt 0 ]; then break fi done echo "OK!" PIDS=`ps -f | grep java | grep "$DEPLOY_DIR" | awk '{print $2}'` echo "PID: $PIDS" echo "The $SERVER_NAME start success!" echo -e "waiting to tail the log file $LOG_FILE " while [ ! -e "$LOG_FILE" ]; do echo -e ".\c" sleep 1 done echo "" tail -f $LOG_FILE
stop.sh
#!/bin/bash cd /exp SERVER_NAME="exp-web" if [ -z "$SERVER_NAME" ]; then SERVER_NAME=`hostname` fi PIDS=`ps -ef | grep java | grep /exp |awk '{print $2}'` if [ -z "$PIDS" ]; then echo "[ERROR] The $SERVER_NAME does not started!" exit 1 fi echo -e "Stopping the $SERVER_NAME ...\c" for PID in $PIDS; do kill $PID > /dev/null 2>&1 done echo "OK!" echo "PID:$PIDS"