配置文件外置,用shell脚本启动停止项目,拿xxl-job-admin项目作为例子(不知道这个项目的,百度一下就可以找到了),部署好的目录结构如下图。
bin目录存放startup.sh和shutdown.sh,启动/停止项目脚本
conf目录存放配置文件,application.properties、logback.xml等。
logs目录是启动项目时生成的
1. 打包
maven直接打包项目,将打包好的jar包放置某个目录下。打成jar包时项目中的配置文件可有可无,反正用不到。
2. 修改配置文件
application.properties文件,其中的port以及数据源连接信息根据项目部署的环境进行修改
logback.xml文件,可以不修改直接使用,可以修改对应的log.path,使生成的日志文件和项目在同一个目录下,改成 logs/xxl-jog-admin.log
将以上两个配置文件修改好后放入conf目录中
3. 编写启动/停止脚本
startup.sh(根据实际情况修改即可)
#!/bin/sh
#JAVA_DEBUG='-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000'
script_abs=$(readlink -f "$0")
script_dir=$(dirname $script_abs)
root_dir=$(cd $script_dir/../;pwd)
echo "root dir is $root_dir"
logs_dir=$root_dir/logs
echo "logs dir is $logs_dir"
if [ ! -d "$logs_dir" ]; then
echo "logs dir not exist,create logs dir $logs_dir"
mkdir $logs_dir
fi
cd $root_dir
# 部署项目环境下的jdk地址下的/bin/java,根据实际情况修改
JAVA_EXE='/home/jdk/bin/java'
echo $JAVA_EXE
if [ -z $JAVA_EXE ]
then
echo 'can not found java'
exit 1
fi
cd $root_dir
JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${root_dir}/logs/java_heapdump.hprof -Dfile.encoding=UTF-8"
# 对应的jar包名称,根据实际情况修改
JAVA_OPT="${JAVA_OPT} -jar ${root_dir}/xxl-job-admin-2.3.1.jar"
# 启动时使用的配置文件位置
JAVA_OPT="${JAVA_OPT} --spring.config.additional-location=${root_dir}/conf/application.properties"
JAVA_OPT="${JAVA_OPT} --logging.config=${root_dir}/conf/logback.xml"
nohup $JAVA_EXE $JAVA_OPT >>$logs_dir/app.log 2>&1 & echo $! >$script_dir/pidfile.txt
shutdown.sh(直接复制粘贴就好,startup.sh启动时会生成一个pidfile.txt文件,文件里存的是项目启动之后的pid,shutdown.sh直接kill这个pid就会停止项目)
#!/bin/sh
script_abs=$(readlink -f "$0")
script_dir=$(dirname $script_abs)
cd $script_dir
PID=`cat pidfile.txt`
kill $PID
while true
do
ls /proc/$PID/cmdline >/dev/null 2>&1
if [ $? == 0 ]
then
sleep 1
else
break
fi
echo "closing......"
done
rm -f pidfile.txt
编写好sh文件之后直接放入bin目录下,基本上这样就完成了。
进入bin目录,输入 ./startup.sh ./shutdown.sh 这两个命令分别是启动和停止项目。(不懂的话可以去学习shell)
项目依赖外置的打包方式以及部署
1. 配置
<build>
<!--最终jar包的名称不包含版本号-->
<finalName>${project.artifactId}</finalName>
<plugins>
<!-- 旧的打包方式jar里面包含依赖 -->
<!--<plugin>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-maven-plugin</artifactId>-->
<!--</plugin>-->
<!-- 新的打包方式jar里面不包含依赖 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<!--jar包中不含以下文件-->
<excludes>
<exclude>application.properties</exclude>
<exclude>application-bjlt.properties</exclude>
<exclude>application-bjltcs.properties</exclude>
<exclude>application-dev.properties</exclude>
<exclude>application-pro.properties</exclude>
<exclude>logback.xml</exclude>
<exclude>notegw.xml</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-config-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/conf</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<includeScope>compile</includeScope>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
</plugins>
</build>
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>distribution</id>
<formats>
<format>tar.gz</format>
</formats>
<baseDirectory>notegw</baseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}</directory>
<includes>
<include>*.jar</include>
<include>lib/*.jar</include>
</includes>
<outputDirectory></outputDirectory>
</fileSet>
<fileSet>
<directory>${project.build.directory}/conf</directory>
<outputDirectory>conf</outputDirectory>
</fileSet>
<fileSet>
<directory>${basedir}/bin</directory>
<fileMode>0777</fileMode>
<outputDirectory>bin</outputDirectory>
</fileSet>
<fileSet>
<directory>${basedir}</directory>
<includes>
<include>README.txt</include>
<include>releasenotes.txt</include>
</includes>
</fileSet>
</fileSets>
</assembly>
2. 启动脚本
startup.sh文件启动脚本
#! /bin/bash
script_abs=$(readlink -f "$0")
script_dir=$(dirname $script_abs)
root_dir=$(cd $script_dir/../;pwd)
logs_dir=$root_dir/logs
echo "logs dir is $logs_dir"
if [ ! -d "$logs_dir" ]; then
echo "logs dir not exist,create logs dir $logs_dir"
mkdir $logs_dir
fi
cd $root_dir
JAVA=/home/ccpbx2/jdk-17.0.2+8/bin/java
JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${root_dir}/logs/java_heapdump.hprof -Dfastjson.parser.safeMode=true"
JAVA_OPT="${JAVA_OPT} -Dapp.home=${root_dir}"
APP_ARGS="--spring.config.location=${root_dir}/conf/"
APP_ARGS="${APP_ARGS} --logging.config=${root_dir}/conf/logback.xml"
nohup $JAVA $JAVA_OPT \
-Dfile.encoding=UTF-8 -cp lib/*:notegw.jar com.suntek.web.notegw.NotegwSpringbootApplication $APP_ARGS >>$logs_dir/app.log 2>&1 & echo $! >$script_dir/pidfile.txt
停止项目脚本
#!/bin/sh
script_abs=$(readlink -f "$0")
script_dir=$(dirname $script_abs)
cd $script_dir
PID=`cat pidfile.txt`
kill $PID
while true
do
ls /proc/$PID/cmdline >/dev/null 2>&1
if [ $? == 0 ]
then
sleep 1
else
break
fi
echo "closing......"
done