maven 打包教程

使用 maven-assembly-plugin 插件打包 java 应用(方法一)

注意:该示例不适合在 pom.xml 中引用了 <scope>system</scope> 的 jar包

参考文档

目录结构

 1$ tree /d/dev/workspaces/rocketmq-demo
 2/d/dev/workspaces/rocketmq-demo
 3├── pom.xml
 4└── src
 5    └── main
 6        ├── assembly
 7        │   └── assembly.xml
 8        ├── bin
 9        │   ├── start.sh
10        │   └── stop.sh
11        ├── java
12        │   └── org
13        │       └── example
14        │           └── App.java
15        └── resources
16            ├── application.yaml
17            ├── data.json
18            └── logback.xml

结构说明:

  • assembly.xml 文件描述了使用 maven-assembly-plugin 插件进行打包的规则
  • bin 目录下的是启动和关闭应用的脚本

重要文件内容和详述

pom.xml 文件内容

  1<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3    <modelVersion>4.0.0</modelVersion>
  4
  5    <groupId>org.example</groupId>
  6    <artifactId>rocketmq-demo</artifactId>
  7    <version>1.0</version>
  8    <packaging>jar</packaging>
  9
 10    <name>rocketmq-demo</name>
 11    <url>http://maven.apache.org</url>
 12
 13    <properties>
 14        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 15        <java.version>1.8</java.version>
 16    </properties>
 17
 18    <dependencies>
 19        <dependency>
 20            <groupId>ch.qos.logback</groupId>
 21            <artifactId>logback-classic</artifactId>
 22            <version>1.2.12</version>
 23        </dependency>
 24
 25        <dependency>
 26            <groupId>org.slf4j</groupId>
 27            <artifactId>slf4j-api</artifactId>
 28            <version>1.7.32</version>
 29        </dependency>
 30    </dependencies>
 31
 32    <build>
 33        <plugins>
 34            <!-- 指定JDK编译版本 -->
 35            <plugin>
 36                <groupId>org.apache.maven.plugins</groupId>
 37                <artifactId>maven-compiler-plugin</artifactId>
 38                <version>3.2</version>
 39                <configuration>
 40                    <source>1.8</source>
 41                    <target>1.8</target>
 42                    <encoding>UTF-8</encoding>
 43                </configuration>
 44            </plugin>
 45
 46            <!-- 打包跳过测试 -->
 47            <plugin>
 48                <groupId>org.apache.maven.plugins</groupId>
 49                <artifactId>maven-surefire-plugin</artifactId>
 50                <version>2.22.2</version>
 51                <configuration>
 52                    <skipTests>true</skipTests>
 53                </configuration>
 54            </plugin>
 55
 56            <plugin>
 57                <groupId>org.apache.maven.plugins</groupId>
 58                <artifactId>maven-jar-plugin</artifactId>
 59                <version>3.4.2</version>
 60                <configuration>
 61                    <archive>
 62                        <manifest>
 63                            <!--运行jar包时运行的主类,要求类全名-->
 64                            <mainClass>org.example.App</mainClass>
 65                            <!-- 是否指定项目classpath下的依赖 -->
 66                            <addClasspath>true</addClasspath>
 67                            <!-- 指定依赖的时候声明前缀;如果本工程的artifact和依赖的jar在同一目录,可不用指定 -->
 68                            <classpathPrefix>./lib</classpathPrefix>
 69                        </manifest>
 70                    </archive>
 71                </configuration>
 72            </plugin>
 73
 74            <plugin>
 75                <groupId>org.apache.maven.plugins</groupId>
 76                <artifactId>maven-assembly-plugin</artifactId>
 77                <version>3.7.1</version>
 78                <executions>
 79                    <!-- 配置执行器 -->
 80                    <execution>
 81                        <id>make-assembly</id>
 82                        <!-- 绑定到package生命周期阶段上 -->
 83                        <phase>package</phase>
 84                        <goals>
 85                            <!-- 只运行一次 -->
 86                            <goal>single</goal>
 87                        </goals>
 88                        <configuration>
 89                            <!-- 生成包的末尾添加assembly id,一般关闭 -->
 90                            <appendAssemblyId>false</appendAssemblyId>
 91                            <!-- 打包后的文件名 -->
 92                            <finalName>${project.artifactId}-${project.version}</finalName>
 93                            <!-- 加载指定的配置文件 -->
 94                            <descriptors>
 95                                <descriptor>src/main/assembly/assembly.xml</descriptor>
 96                            </descriptors>
 97                        </configuration>
 98                    </execution>
 99                </executions>
100            </plugin>
101        </plugins>
102    </build>
103</project>

assembly.xml 文件内容

 1<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.2.0"
 2          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.2.0 http://maven.apache.org/xsd/assembly-2.2.0.xsd">
 4    <!-- 唯一ID -->
 5    <id>app-assembly</id>
 6
 7    <!-- 打包文件格式,有zip、tar、tar.gz、tar.bz2、jar、war。可以同时定义多个format -->
 8    <formats>
 9        <format>zip</format>
10    </formats>
11
12    <!-- 压缩包下是否生成和项目名相同的根目录 -->
13    <includeBaseDirectory>false</includeBaseDirectory>
14
15    <fileSets>
16        <!-- 可执行脚本的文件配置 -->
17        <fileSet>
18            <!-- 脚本所在的文件夹,以及打包后将脚本输出到哪个文件夹中 -->
19            <directory>src/main/bin</directory>
20            <outputDirectory>rocketmq-demo</outputDirectory>
21            <!-- 哪些文件会被提取;所有文件可以用*.* -->
22            <includes>
23                <include>*.sh</include>
24            </includes>
25            <!-- 文件权限及编码 -->
26            <fileMode>0755</fileMode>
27            <lineEnding>unix</lineEnding>
28        </fileSet>
29
30        <!-- 项目自身构建的artifact文件配置 -->
31        <fileSet>
32            <!-- 这里的target是maven-compiler-plugin生成该项目的jar包的位置;也可用${project.build.directory}代替 -->
33            <directory>target</directory>
34            <outputDirectory>rocketmq-demo</outputDirectory>
35            <includes>
36                <include>*.jar</include>
37            </includes>
38            <fileMode>0644</fileMode>
39        </fileSet>
40    </fileSets>
41
42    <dependencySets>
43        <dependencySet>
44            <!-- 这里是将该项目依赖的所有jar包存入lib文件夹中 -->
45            <outputDirectory>rocketmq-demo/lib</outputDirectory>
46            <!-- 是否将项目自身的 artifact(如生成的 jar 包)包含到输出文件中 -->
47            <useProjectArtifact>false</useProjectArtifact>
48            <!-- 是否将项目附件(如源代码、配置文件等)包含到输出文件中 -->
49            <useProjectAttachments>true</useProjectAttachments>
50            <!-- 将scope为runtime的依赖包打包-->
51            <scope>runtime</scope>
52        </dependencySet>
53    </dependencySets>
54</assembly>

启动脚本 start.sh

1#!/bin/sh
2BASE_PATH=$(cd `dirname $0`; pwd)
3echo 'Base Path ====>> '$BASE_PATH
4
5JAR_FILE='rocketmq-demo-1.0.jar'
6echo 'Jar File ====>> '$JAR_FILE
7
8nohup java -jar $JAR_FILE >/dev/null 2>&1 &
9echo '应用已启动...'

停止脚本 stop.sh

1#!/bin/sh
2pid=`ps -ef |grep 'rocketmq-demo-1.0' | grep -v grep | awk '{print $2}'`
3kill -9 $pid
4echo '应用已停止...'

App.java

 1package org.example;
 2
 3import org.slf4j.Logger;
 4import org.slf4j.LoggerFactory;
 5
 6public class App {
 7
 8    private static final Logger logger = LoggerFactory.getLogger(App.class);
 9
10    public static void main(String[] args) {
11        logger.info("Hello World!");
12    }
13}

logback.xml 文件内容

 1<?xml version="1.0" encoding="UTF-8"?>
 2<configuration>
 3    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
 4        <file>logs/app.log</file>
 5        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
 6            <fileNamePattern>logs/app.%d{yyyy-MM-dd}.log</fileNamePattern>
 7            <maxHistory>30</maxHistory>
 8            <totalSizeCap>10GB</totalSizeCap>
 9        </rollingPolicy>
10
11        <encoder>
12            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %level %logger{36}:%L - %msg%n</pattern>
13        </encoder>
14    </appender>
15
16    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
17        <encoder>
18            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %level %logger{36}:%L - %msg%n</pattern>
19        </encoder>
20    </appender>
21
22    <root level="DEBUG">
23        <appender-ref ref="FILE" />
24        <appender-ref ref="STDOUT" />
25    </root>
26</configuration>

使用总结

总体来说,maven-assembly-plugin 插件结合 maven-compiler-plugin 、 maven-surefire-plugin 、maven-jar-plugin 插件后, 功能十分完善且强大。
比如脚本、资源文件等,都可以在该插件中打包,形成一个完整的压缩包后,进行分发。
但是,本人在使用过程中有一个瑕疵,就是如果 maven 引用了本地jar包,例如:

1        <dependency>
2            <groupId>com.cpsdc.cpdsp</groupId>
3            <artifactId>ms-sdk</artifactId>
4            <version>1.0.2</version>
5            <scope>system</scope>
6            <systemPath>${basedir}/libs/ms-sdk-1.0.2.jar</systemPath>
7        </dependency>

始终无法把本地jar包打包到可分发的压缩包中(也不知是否没有找对方法),
所以只能通过如下方式,先把本地jar包打包到本地仓库中,再在maven中引用方可正确打包:

1$ mvn install:install-file -DgroupId=${group_id} -DartifactId=${artifact_id} -Dversion=${version} -Dpackaging=jar -Dfile=${file_path}

使用 maven-assembly-plugin 插件打包 java 应用(方法二)

注意:该示例可以打包在 pom.xml 中引用了 <scope>system</scope> 的 jar包,但是启动命令和方法一不同。
详细请看下文

目录结构

参考方法一的 maven-assembly-plugin 的目录结构

重要文件内容和详述

没有列出的文件,说明和方法一的文件内容一致

pom.xml 文件内容

 1<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3    <modelVersion>4.0.0</modelVersion>
 4
 5    <groupId>org.example</groupId>
 6    <artifactId>rocketmq-demo</artifactId>
 7    <version>1.0</version>
 8    <packaging>jar</packaging>
 9
10    <name>rocketmq-demo</name>
11    <url>http://maven.apache.org</url>
12
13    <properties>
14        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15        <java.version>1.8</java.version>
16    </properties>
17
18    <dependencies>
19        <dependency>
20            <groupId>ch.qos.logback</groupId>
21            <artifactId>logback-classic</artifactId>
22            <version>1.2.12</version>
23        </dependency>
24
25        <dependency>
26            <groupId>org.slf4j</groupId>
27            <artifactId>slf4j-api</artifactId>
28            <version>1.7.32</version>
29        </dependency>
30    </dependencies>
31
32    <build>
33        <plugins>
34            <!-- 指定JDK编译版本 -->
35            <plugin>
36                <groupId>org.apache.maven.plugins</groupId>
37                <artifactId>maven-compiler-plugin</artifactId>
38                <version>3.2</version>
39                <configuration>
40                    <source>1.8</source>
41                    <target>1.8</target>
42                    <encoding>UTF-8</encoding>
43                </configuration>
44            </plugin>
45
46            <!-- 打包跳过测试 -->
47            <plugin>
48                <groupId>org.apache.maven.plugins</groupId>
49                <artifactId>maven-surefire-plugin</artifactId>
50                <version>2.22.2</version>
51                <configuration>
52                    <skipTests>true</skipTests>
53                </configuration>
54            </plugin>
55
56            <plugin>
57                <groupId>org.apache.maven.plugins</groupId>
58                <artifactId>maven-assembly-plugin</artifactId>
59                <version>3.7.1</version>
60                <executions>
61                    <!-- 配置执行器 -->
62                    <execution>
63                        <id>make-assembly</id>
64                        <!-- 绑定到package生命周期阶段上 -->
65                        <phase>package</phase>
66                        <goals>
67                            <!-- 只运行一次 -->
68                            <goal>single</goal>
69                        </goals>
70                        <configuration>
71                            <!-- 生成包的末尾添加assembly id,一般关闭 -->
72                            <appendAssemblyId>false</appendAssemblyId>
73                            <!-- 打包后的文件名 -->
74                            <finalName>${project.artifactId}-${project.version}</finalName>
75                            <!-- 加载指定的配置文件 -->
76                            <descriptors>
77                                <descriptor>src/main/assembly/assembly.xml</descriptor>
78                            </descriptors>
79                        </configuration>
80                    </execution>
81                </executions>
82            </plugin>
83        </plugins>
84    </build>
85</project>

assembly.xml 文件内容

 1<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.2.0"
 2          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.2.0 http://maven.apache.org/xsd/assembly-2.2.0.xsd">
 4    <!-- 唯一ID -->
 5    <id>app-assembly</id>
 6
 7    <!-- 打包文件格式,有zip、tar、tar.gz、tar.bz2、jar、war。可以同时定义多个format -->
 8    <formats>
 9        <format>zip</format>
10    </formats>
11
12    <!-- 压缩包下是否生成和项目名相同的根目录 -->
13    <includeBaseDirectory>false</includeBaseDirectory>
14
15    <fileSets>
16        <!-- 可执行脚本的文件配置 -->
17        <fileSet>
18            <!-- 脚本所在的文件夹,以及打包后将脚本输出到哪个文件夹中 -->
19            <directory>src/main/bin</directory>
20            <outputDirectory>rocketmq-demo</outputDirectory>
21            <!-- 哪些文件会被提取;所有文件可以用*.* -->
22            <includes>
23                <include>*.sh</include>
24            </includes>
25            <!-- 文件权限及编码 -->
26            <fileMode>0755</fileMode>
27            <lineEnding>unix</lineEnding>
28        </fileSet>
29
30        <!-- 项目自身构建的artifact文件配置 -->
31        <fileSet>
32            <!-- 这里的target是maven-compiler-plugin生成该项目的jar包的位置;也可用${project.build.directory}代替 -->
33            <directory>target</directory>
34            <outputDirectory>rocketmq-demo</outputDirectory>
35            <includes>
36                <include>*.jar</include>
37            </includes>
38            <fileMode>0644</fileMode>
39        </fileSet>
40    </fileSets>
41
42    <dependencySets>
43        <dependencySet>
44            <!-- 这里是将该项目依赖的所有jar包存入lib文件夹中 -->
45            <outputDirectory>rocketmq-demo/lib</outputDirectory>
46            <!-- 是否将项目自身的 artifact(如生成的 jar 包)包含到输出文件中 -->
47            <useProjectArtifact>false</useProjectArtifact>
48            <!-- 是否将项目附件(如源代码、配置文件等)包含到输出文件中 -->
49            <useProjectAttachments>true</useProjectAttachments>
50            <!-- 将scope为runtime的依赖包打包-->
51            <scope>runtime</scope>
52        </dependencySet>
53
54        <dependencySet>
55            <outputDirectory>rocketmq-demo/lib</outputDirectory>
56            <!-- 将scope为system的依赖包打包-->
57            <scope>system</scope>
58        </dependencySet>
59    </dependencySets>
60</assembly>

启动脚本 start.sh

 1#!/bin/sh
 2BASE_PATH=$(cd `dirname $0`; pwd)
 3echo 'Base Path ====>> '$BASE_PATH
 4
 5
 6JAR_FILE='rocketmq-demo-1.0.jar'
 7echo 'Jar File ====>> '$JAR_FILE
 8
 9nohup java -cp "$JAR_FILE;lib/*" org.example.App >/dev/null 2>&1 &
10echo '应用已启动...'

注意:使用该方式启动应用,启动命令和方式一不一样。
方式一是这样运行的(不需要指定启动类和依赖的jar): java -jar rocketmq-demo-1.0.jar
但方式二是这样运行的(必须指定启动类和依赖的jar): java -cp "rocketmq-demo-1.0.jar;lib/*" org.example.App

使用 spring-boot-maven-plugin 插件打包 java 应用

目录结构

 1$ tree /d/dev/workspaces/rocketmq-demo
 2/d/dev/workspaces/rocketmq-demo
 3├── libs
 4│   └── ms-sdk-1.0.2.jar
 5├── pom.xml
 6└── src
 7    └── main
 8        ├── java
 9        │   └── org
10        │       └── example
11        │           └── App.java
12        └── resources
13            ├── application.yaml
14            ├── data.json
15            └── logback.xml
  • libs 是引用的第三方的sdk jar包

重要文件内容和详述

pom.xml 文件内容

 1<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3    <modelVersion>4.0.0</modelVersion>
 4
 5    <groupId>org.example</groupId>
 6    <artifactId>rocketmq-demo</artifactId>
 7    <version>1.0</version>
 8    <packaging>jar</packaging>
 9
10    <name>rocketmq-demo</name>
11    <url>http://maven.apache.org</url>
12
13    <properties>
14        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15        <java.version>1.8</java.version>
16    </properties>
17
18    <dependencies>
19        <dependency>
20            <groupId>com.cpsdc.cpdsp</groupId>
21            <artifactId>ms-sdk</artifactId>
22            <version>1.0.2</version>
23            <scope>system</scope>
24            <systemPath>${basedir}/libs/ms-sdk-1.0.2.jar</systemPath>
25        </dependency>
26
27        <dependency>
28            <groupId>ch.qos.logback</groupId>
29            <artifactId>logback-classic</artifactId>
30            <version>1.2.12</version>
31        </dependency>
32
33        <dependency>
34            <groupId>org.slf4j</groupId>
35            <artifactId>slf4j-api</artifactId>
36            <version>1.7.32</version>
37        </dependency>
38    </dependencies>
39
40    <build>
41        <plugins>
42            <!-- 指定JDK编译版本 -->
43            <plugin>
44                <groupId>org.apache.maven.plugins</groupId>
45                <artifactId>maven-compiler-plugin</artifactId>
46                <version>3.2</version>
47                <configuration>
48                    <source>1.8</source>
49                    <target>1.8</target>
50                    <encoding>UTF-8</encoding>
51                </configuration>
52            </plugin>
53
54            <!-- 打包跳过测试 -->
55            <plugin>
56                <groupId>org.apache.maven.plugins</groupId>
57                <artifactId>maven-surefire-plugin</artifactId>
58                <version>2.22.2</version>
59                <configuration>
60                    <skipTests>true</skipTests>
61                </configuration>
62            </plugin>
63
64            <plugin>
65                <groupId>org.springframework.boot</groupId>
66                <artifactId>spring-boot-maven-plugin</artifactId>
67                <version>2.7.18</version>
68                <executions>
69                    <execution>
70                        <id>repackage</id>
71                        <goals>
72                            <goal>repackage</goal>
73                        </goals>
74                    </execution>
75                </executions>
76                <configuration>
77                    <mainClass>org.example.App</mainClass>
78                    <!-- 确保本地jar包(scope 等于 system 时)能够正确打包 -->
79                    <includeSystemScope>true</includeSystemScope>
80                </configuration>
81            </plugin>
82        </plugins>
83    </build>
84</project>

使用总结

spring-boot-maven-plugin 插件结合 maven-compiler-plugin 、 maven-surefire-plugin 插件后, 功能也比较完善。
只是个人认为,在处理脚本、资源文件等,以及打包后的结构,不如 maven-assembly-plugin 插件。
但是,spring-boot-maven-plugin 能正常打包本地jar包(scope 等于 system 时),这是十分方便的。

终极方式:使用 spring-boot-maven-plugin + maven-assembly-plugin 插件打包 java 应用

目录结构

参考方法一的 maven-assembly-plugin 的目录结构

重要文件内容和详述

pom.xml 文件内容

  1<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3    <modelVersion>4.0.0</modelVersion>
  4
  5    <groupId>org.example</groupId>
  6    <artifactId>assembly-demo</artifactId>
  7    <version>1.0.0</version>
  8    <packaging>jar</packaging>
  9
 10    <name>assembly-demo</name>
 11    <url>http://maven.apache.org</url>
 12
 13    <properties>
 14        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 15        <java.version>1.8</java.version>
 16    </properties>
 17
 18    <dependencies>
 19        <dependency>
 20            <groupId>com.alibaba.fastjson2</groupId>
 21            <artifactId>fastjson2</artifactId>
 22            <version>2.0.58</version>
 23        </dependency>
 24
 25        <dependency>
 26            <groupId>commons-io</groupId>
 27            <artifactId>commons-io</artifactId>
 28            <version>2.20.0</version>
 29        </dependency>
 30
 31        <dependency>
 32            <groupId>ch.qos.logback</groupId>
 33            <artifactId>logback-classic</artifactId>
 34            <version>1.2.12</version>
 35        </dependency>
 36
 37        <dependency>
 38            <groupId>org.slf4j</groupId>
 39            <artifactId>slf4j-api</artifactId>
 40            <version>1.7.32</version>
 41        </dependency>
 42    </dependencies>
 43
 44    <build>
 45        <finalName>${project.artifactId}-${project.version}</finalName>
 46
 47        <plugins>
 48            <!-- 指定JDK编译版本 -->
 49            <plugin>
 50                <groupId>org.apache.maven.plugins</groupId>
 51                <artifactId>maven-compiler-plugin</artifactId>
 52                <version>3.2</version>
 53                <configuration>
 54                    <source>1.8</source>
 55                    <target>1.8</target>
 56                    <encoding>UTF-8</encoding>
 57                </configuration>
 58            </plugin>
 59
 60            <!-- 打包跳过测试 -->
 61            <plugin>
 62                <groupId>org.apache.maven.plugins</groupId>
 63                <artifactId>maven-surefire-plugin</artifactId>
 64                <version>2.22.2</version>
 65                <configuration>
 66                    <skipTests>true</skipTests>
 67                </configuration>
 68            </plugin>
 69
 70            <plugin>
 71                <groupId>org.springframework.boot</groupId>
 72                <artifactId>spring-boot-maven-plugin</artifactId>
 73                <version>2.7.18</version>
 74                <executions>
 75                    <execution>
 76                        <id>repackage</id>
 77                        <goals>
 78                            <goal>repackage</goal>
 79                        </goals>
 80                    </execution>
 81                </executions>
 82                <configuration>
 83                    <mainClass>org.example.App</mainClass>
 84                    <!-- 确保本地jar包(scope 等于 system 时)能够正确打包 -->
 85                    <includeSystemScope>true</includeSystemScope>
 86                </configuration>
 87            </plugin>
 88
 89            <plugin>
 90                <groupId>org.apache.maven.plugins</groupId>
 91                <artifactId>maven-assembly-plugin</artifactId>
 92                <version>3.7.1</version>
 93                <executions>
 94                    <!-- 配置执行器 -->
 95                    <execution>
 96                        <id>make-assembly</id>
 97                        <!-- 绑定到package生命周期阶段上 -->
 98                        <phase>package</phase>
 99                        <goals>
100                            <!-- 只运行一次 -->
101                            <goal>single</goal>
102                        </goals>
103                        <configuration>
104                            <!-- 生成包的末尾添加assembly id,一般关闭 -->
105                            <appendAssemblyId>false</appendAssemblyId>
106                            <!-- 加载指定的配置文件 -->
107                            <descriptors>
108                                <descriptor>src/main/assembly/assembly.xml</descriptor>
109                            </descriptors>
110                        </configuration>
111                    </execution>
112                </executions>
113            </plugin>
114        </plugins>
115    </build>
116</project>

此处务必注意:spring-boot-maven-plugin 插件必须放在 maven-assembly-plugin 之前
因为必须先让 spring-boot-maven-plugin 插件打成 jar 包后,再由 maven-assembly-plugin 插件打包资源文件。

assembly.xml 文件内容

 1<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.2.0"
 2          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.2.0 http://maven.apache.org/xsd/assembly-2.2.0.xsd">
 4    <!-- 唯一ID -->
 5    <id>app-assembly</id>
 6
 7    <!-- 打包文件格式,有zip、tar、tar.gz、tar.bz2、jar、war。可以同时定义多个format -->
 8    <formats>
 9        <format>zip</format>
10    </formats>
11
12    <!-- 压缩包下是否生成和项目名相同的根目录 -->
13    <includeBaseDirectory>false</includeBaseDirectory>
14
15    <fileSets>
16        <!-- 可执行脚本的文件配置 -->
17        <fileSet>
18            <!-- 脚本所在的文件夹,以及打包后将脚本输出到哪个文件夹中 -->
19            <directory>src/main/bin</directory>
20            <outputDirectory>${project.artifactId}</outputDirectory>
21            <!-- 哪些文件会被提取;所有文件可以用*.* -->
22            <includes>
23                <include>*.sh</include>
24            </includes>
25            <!-- 文件权限及编码 -->
26            <fileMode>0755</fileMode>
27            <lineEnding>unix</lineEnding>
28        </fileSet>
29
30        <!-- 项目自身构建的artifact文件配置 -->
31        <fileSet>
32            <!-- 这里的target是maven-compiler-plugin生成该项目的jar包的位置;也可用${project.build.directory}代替 -->
33            <directory>target</directory>
34            <outputDirectory>${project.artifactId}</outputDirectory>
35            <includes>
36                <include>*.jar</include>
37            </includes>
38            <fileMode>0644</fileMode>
39        </fileSet>
40    </fileSets>
41</assembly>