# spring-boot-assembly
> 1. 在spring boot项目中使用maven profiles和maven assembly插件根据不同环境打包成tar.gz或者zip
> 2. 将spring boot项目中的配置文件提取到外部config目录中
> 3. 将spring boot项目中的启动jar包移动到boot目录中
> 4. 将spring boot项目中的第三方依赖jar包移动到外部lib目录中
> 5. bin目录中是启动,停止,重启服务命令
> 6. 打包后的目录结构类似于tomcat/maven目录结构
## 代码托管
> **[Github](https://github.com/singgel/SpringBoot-Templates/tree/master/spring-boot-assembly)**
### 主要插件
1. maven-assembly-plugin
2. maven-jar-plugin
3. spring-boot-maven-plugin
4. maven-dependency-plugin
5. maven-resources-plugin
### CHANGELOG
[CHANGELOG](https://github.com/singgel/SpringBoot-Templates/tree/master/spring-boot-assembly/blob/master/CHANGELOG.md)
#### 1.maven-assembly-plugin 配置assembly.xml文件路径
```xml
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
```
#### 2.assembly.xml打包配置文件
```xml
<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<!-- 可自定义,这里指定的是项目环境 -->
<!-- spring-boot-assembly-local-1.0.RELEASE.tar.gz -->
<id>${profileActive}-${project.version}</id>
<!-- 打包的类型,如果有N个,将会打N个类型的包 -->
<formats>
<format>tar.gz</format>
<!--<format>zip</format>-->
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<!--
0755->即用户具有读/写/执行权限,组用户和其它用户具有读写权限;
0644->即用户具有读写权限,组用户和其它用户具有只读权限;
-->
<!-- 将src/bin目录下的所有文件输出到打包后的bin目录中 -->
<fileSet>
<directory>${basedir}/src/bin</directory>
<outputDirectory>bin</outputDirectory>
<fileMode>0755</fileMode>
<includes>
<include>**.sh</include>
<include>**.bat</include>
</includes>
</fileSet>
<!-- 指定输出target/classes中的配置文件到config目录中 -->
<fileSet>
<directory>${basedir}/target/classes</directory>
<outputDirectory>config</outputDirectory>
<fileMode>0644</fileMode>
<includes>
<include>application.yml</include>
<include>application-${profileActive}.yml</include>
<include>mapper/**/*.xml</include>
<include>static/**</include>
<include>templates/**</include>
<include>*.xml</include>
<include>*.properties</include>
</includes>
</fileSet>
<!-- 将第三方依赖打包到lib目录中 -->
<fileSet>
<directory>${basedir}/target/lib</directory>
<outputDirectory>lib</outputDirectory>
<fileMode>0755</fileMode>
</fileSet>
<!-- 将项目启动jar打包到boot目录中 -->
<fileSet>
<directory>${basedir}/target</directory>
<outputDirectory>boot</outputDirectory>
<fileMode>0755</fileMode>
<includes>
<include>${project.build.finalName}.jar</include>
</includes>
</fileSet>
<!-- 包含根目录下的文件 -->
<fileSet>
<directory>${basedir}</directory>
<includes>
<include>NOTICE</include>
<include>LICENSE</include>
<include>*.md</include>
</includes>
</fileSet>
</fileSets>
</assembly>
```
#### 3.spring-boot-maven-plugin 排除启动jar包中依赖的jar包
```xml
<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>
```
#### 4.maven-jar-plugin 自定义maven jar打包内容
```xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<!-- 项目启动类 -->
<mainClass>Application</mainClass>
<!-- 依赖的jar的目录前缀 -->
<classpathPrefix>../lib</classpathPrefix>
<addClasspath>true</addClasspath>
</manifest>
</archive>
<includes>
<!-- 只打包指定目录的文件 -->
<include>com/hks/assembly/**</include>
</includes>
</configuration>
</plugin>
```
#### 5.maven-dependency-plugin 复制项目的依赖包到指定目录
```xml
<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>
```
#### 6.maven-resources-plugin
```xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
</plugin>
```
```xml
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application.yml</include>
<include>application-${profileActive}.yml</include>
<include>mapper/**/*.xml</include>
<include>static/**</include>
<include>templates/**</include>
<include>*.xml</include>
<include>*.properties</include>
</includes>
</resource>
```
#### 7.maven profiles配置
```xml
<!--MAVEN打包选择运行环境-->
<!-- 1:local(默认) 本地 2:dev:开发环境 3:test 4:uat 用户验收测试 5.pro:生产环境-->
<profiles>
<profile>
<id>local</id>
<properties>
<profileActive>local</profileActive>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>dev</id>
<properties>
<profileActive>dev</profileActive>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<profileActive>test</profileActive>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
<profile>
<id>uat</id>
<properties>
<profileActive>uat</profileActive>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</acti