将以下配置跟到最后就行(注意是</project>前), 具体需要自定义的项, 看配置中的注释的自定义项
pom.xml配置
<profiles>
<profile>
<!-- 本地开发环境 -->
<id>dev</id>
<properties>
<profiles.active>dev</profiles.active>
</properties>
<!--默认激活-->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!-- 测试环境 -->
<id>test</id>
<properties>
<profiles.active>test</profiles.active>
</properties>
</profile>
<profile>
<!-- 生产环境 -->
<id>product</id>
<properties>
<profiles.active>product</profiles.active>
</properties>
</profile>
</profiles>
<build>
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<!-- 资源根目录排除各环境的配置,使用单独的资源目录来指定 -->
<excludes>
<exclude>dev/*</exclude>
<exclude>test/*</exclude>
<exclude>product/*</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources/${profiles.active}</directory>
</resource>
</resources>
<plugins>
<!-- jar 打包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<!--自定义项: Springboot启动类的全限定名-->
<mainClass>com.xxx.xxx.xxxApplication</mainClass>
</manifest>
<manifestEntries>
<Class-Path>./</Class-Path>
</manifestEntries>
</archive>
<!--自定义项: 打包时,需要排除的文件或文件夹-->
<excludes>
<exclude>application.properties</exclude>
<exclude>logback.xml</exclude>
<exclude>i18n/**</exclude>
<exclude>template/**</exclude>
</excludes>
<outputDirectory>target/${project.name}/</outputDirectory>
</configuration>
</plugin>
<!-- 打包跳过测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<!-- copy 依赖包 -->
<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>
<!--依赖jar复制到的文件夹路径-->
<outputDirectory>target/${project.name}/lib/</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<!-- 不同环境的资源文件 -->
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<!--将指定resource文件复制到的目标文件夹-->
<outputDirectory>target/${project.name}/config</outputDirectory>
<resources>
<resource>
<!--自定义项: 复制src/main/resources/目录下的template/i18n/script文件夹到config目录下,若不需要复制resources下的其他文件夹内容,就不需要这个<resource>-->
<directory>src/main/resources/</directory>
<include>template/**</include>
<include>i18n/**</include>
<include>script/**</include>
</resource>
<resource>
<!--复制src/main/resources/${profiles.active}目录下的application.properties/logback.xml文件到config目录下-->
<directory>src/main/resources/${profiles.active}</directory>
<!-- 自定义项: 需要copy ${profiles.active}文件夹下的哪些文件-->
<includes>
<include>application.properties</include>
<include>logback.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
打包后文件结构
分离后的启动方法:
1) 简单手动启动
java -DGID=./ -Dloader.path=./lib -jar xxx.jar --logging.config=./config/logback.xml
参数说明:
-DGID=./ #项目根目录路径
-Dloader.path=./lib #jar包依赖所在路径
--logging.config=./config/logback-spring.xml #日志配置文件路径
2) 脚本启动
Linux版项目启动脚本daemon.sh(JVM参数根据自己情况进行修改)
注意:
-XX:MaxMetaspaceSize 和 -XX:MetaspaceSize 是JDK1.8的
脚本操作方式
开启: sh daemon.sh start
关闭: sh daemon.sh stop
重启: sh daemon.sh restart
#!/bin/sh
BASE_DIR=$(cd "$(dirname "$0")"; pwd)/
cd $BASE_DIR
APP_NAME=包名(不含.jar)
# 环境名, 开发dev,测试test,生产product, 在使用application-test.properties配置文件进行区分环境配置的时候有效)
APP_ACTIVE=test
LIBS=./lib
function startApp()
{
echo "trying start app $APP_NAME at [${BASE_DIR}] ......................"
JVM_PARAM="
-server
-Xmx1536M
-Xms1536M
-Xmn1024M
-XX:MaxMetaspaceSize=512M
-XX:MetaspaceSize=256M
-XX:GCTimeRatio=19
-XX:+ClassUnloading
-XX:+UseConcMarkSweepGC
-Xloggc:./logs/gc.log
"
nohup java -DGID=$BASE_DIR -Dloader.path=$LIBS $JVM_PARAM -jar "$APP_NAME".jar --spring.profiles.active=$APP_ACTIVE --logging.config=config/logback.xml > /dev/null 2>&1 &
rm -rf "$APP_NAME".log
echo "success start app $APP_NAME at [${BASE_DIR}] ......................"
}
function stopApp()
{
echo "trying stop app $APP_NAME at [${BASE_DIR}] ......................"
PID=$(ps -ef|grep $BASE_DIR|grep $APP_NAME|grep -v grep|grep -v kill|awk '{ print $2 }')
if [ ${PID} ]; then
echo 'Stop Process...'
kill -15 $PID
fi
sleep 5
PID=$(ps -ef|grep $BASE_DIR|grep $APP_NAME|grep -v grep|grep -v kill|awk '{ print $2 }')
if [ ${PID} ]; then
echo 'Kill Process!'
kill -9 $PID
else
echo 'Stop Success!'
fi
echo "success stop app $APP_NAME at [${BASE_DIR}] ......................"
}
function restartApp()
{
stopApp
sleep 1
startApp
exit
}
function displayHelp()
{
echo ' '
echo ' Please Attach a parameter when run this shell '
echo ' Parameters:'
echo ' start : Start Application in Service Mode(Log to File)'
echo ' stop : Stop The Application'
echo ' restart: Restart The Application'
echo ' '
}
case $1 in
start)
startApp
;;
stop)
stopApp
;;
restart)
restartApp
;;
*)
displayHelp
;;
esac
logbak.xml
logbak日志配置文件, 相关参数可自行修改
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径-->
<property name="LOG_HOME" value="logs/" />
<property name="project-name" value="项目名" />
<springProperty scope="context" name="logLevel" source="logback.log.level"/>
<!-- 彩色日志 -->
<!-- 彩色日志依赖的渲染类 -->
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
<!-- 彩色日志格式 -->
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:-}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<!--<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){green} %clr(${LOG_LEVEL_PATTERN:-%5p}){red} %clr(${PID:-}){magenta} %clr(---){blue} %clr([%30.30t]){yellow} %clr(%-40.40logger{39}){cyan} %clr(:){blue} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}" />-->
<!-- 控制台输出日志 -->
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<!-- 日志级别过滤INFO以下 -->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<!-- 普通log输出 -->
<appender name="dailyRolling"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/${project-name}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--日志文件输出的文件名-->
<fileNamePattern>${LOG_HOME}/%d{yyyy,aux}/%d{yyyy-MM,aux}/${project-name}.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<!--日志文件保留天数-->
<maxHistory>3</maxHistory><!-- 3天的文件 -->
<totalSizeCap>10GB</totalSizeCap><!-- 上限大小,最大不超过10G -->
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
<pattern>%d [%thread] %-5level %logger{80} - %msg%n</pattern>
</encoder>
</appender>
<!-- error级别错误日志文件 -->
<appender name="errorDailyRolling" class="ch.qos.logback.core.FileAppender">
<file>${LOG_HOME}/${project-name}-error.log</file>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<append>true</append>
<encoder>
<pattern>%d [%thread] %-5level %logger{80} - %msg%n</pattern>
</encoder>
</appender>
<!-- 日志输出级别 -->
<root level="INFO">
<appender-ref ref="stdout" />
<appender-ref ref="dailyRolling" />
<appender-ref ref="errorDailyRolling" />
</root>
</configuration>