使用maven-assembly-plugin進行配置分離
assembly.xml配置文件

配置文件內容
<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<!-- 可自定義,這里指定的是項目環境 -->
<!-- xxx.tar.gz -->
<id>${name}</id>
<!-- 打包的類型,如果有N個,將會打N個類型的包 -->
<formats>
<format>tar.gz</format>
<format>zip</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<!-- 配置文件打包-打包至config目錄下 -->
<fileSet>
<directory>src/main/resources/</directory>
<outputDirectory>config</outputDirectory>
<fileMode>0644</fileMode>
<includes>
<include>application.yml</include>
<include>*.xml</include>
<include>*.properties</include>
<!--可以自己添加配置文件-->
<include>common/*.properties</include>
</includes>
</fileSet>
<!-- 啟動文件目錄 -->
<fileSet>
<directory>${basedir}/src/bin</directory>
<outputDirectory>bin</outputDirectory>
<fileMode>0755</fileMode>
<includes>
<include>**.sh</include>
<include>**.bat</include>
</includes>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<!-- 依賴庫 -->
<outputDirectory>lib</outputDirectory>
<scope>runtime</scope>
<fileMode>0755</fileMode>
<excludes>
<exclude>${project.groupId}:${project.artifactId}</exclude>
</excludes>
</dependencySet>
<dependencySet>
<outputDirectory>boot</outputDirectory>
<fileMode>0755</fileMode>
<includes>
<include>${project.groupId}:${project.artifactId}</include>
</includes>
</dependencySet>
</dependencySets>
</assembly>
Pom.xml文件配置
pom文件中build屬性的配置
<build>
<!-- 打包后的啟動jar名稱 -->
<finalName>message</finalName>
<plugins>
<!-- 用於排除jar中依賴包 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>ZIP</layout>
<includes>
<!-- 項目啟動jar包中排除依賴包 -->
<include>
<groupId>non-exists</groupId>
<artifactId>non-exists</artifactId>
</include>
</includes>
</configuration>
</plugin>
<!-- 將依賴cp到lib目錄下 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>target/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<includeScope>compile</includeScope>
</configuration>
</execution>
</executions>
</plugin>
<!-- maven編譯 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- 不同版本需要制定具體的版本進行編譯 -->
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<!-- 打包時跳過測試 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<!-- 將項目中代碼文件打成jar包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<excludes>
<!-- 打包后的jar包中不包括配置文件 -->
<!-- 通常是指classpath下目錄下的文件,這樣可以避免編寫時的找不到相應文件 -->
<exclude>*.xml</exclude>
<exclude>*.properties</exclude>
<exclude>*.yml</exclude>
</excludes>
<archive>
<manifest>
<!-- 項目啟動類 -->
<mainClass>xx.xxx.Application</mainClass>
<!-- 依賴的jar的目錄前綴 -->
<classpathPrefix>../lib/</classpathPrefix>
<addClasspath>true</addClasspath>
</manifest>
<!-- 將config目錄加入classpath目錄 -->
<manifestEntries>
<Class-Path>../config/</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<!-- 打包插件 -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<descriptors>
<descriptor>src/main/resources/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
運行:maven package

運行
1.直接運行 java -jar xxxx.jar即可
2.編寫shell/bash腳本
在bin目錄下編寫腳本文件如下:
shell
#! /bin/sh HOME = '/opt/xxx/boot' JAR_HOME = 'xxx.jar' cd $HOME nohup java -jar $JAR_HOME
bash
