>maven-assembly-plugin
时间: 2025-06-09 16:25:33 浏览: 22
### maven-assembly-plugin 使用与配置指南
maven-assembly-plugin 是 Maven 构建工具中的一个重要插件,用于将项目及其依赖项打包成特定格式的压缩文件(如 tar.gz 或 zip),并支持高度定制化的打包方式[^1]。以下是对该插件使用与配置的详细说明。
#### 插件功能概述
maven-assembly-plugin 的核心功能是根据用户定义的描述符文件(descriptor)将项目的输出文件、依赖项以及其他资源打包成指定的格式。它能够满足多样化的打包需求,例如生成包含所有依赖的可执行 JAR 文件或创建复杂的部署包结构[^1]。
---
#### 插件配置示例
以下是 maven-assembly-plugin 在 `pom.xml` 文件中的典型配置:
```xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.4.2</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>
```
上述配置中:
- `<descriptors>` 指定了描述符文件的位置,该文件定义了打包的具体规则。
- `<phase>` 绑定了插件执行的目标阶段(此处为 `package` 阶段)。
- `<goal>` 指定了插件的目标(此处为 `single`,表示仅执行一次打包任务)。
---
#### 描述符文件(assembly.xml)
描述符文件是 maven-assembly-plugin 的核心配置文件,用于定义打包的内容和结构。以下是一个简单的 `assembly.xml` 示例:
```xml
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>distribution-package</id>
<formats>
<format>tar.gz</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
<files>
<file>
<source>src/main/resources/start.sh</source>
<outputDirectory>/bin</outputDirectory>
<filtered>true</filtered>
</file>
</files>
</assembly>
```
上述配置中:
- `<id>` 定义了生成的压缩包的后缀名。
- `<formats>` 指定了生成的压缩包格式(如 tar.gz 或 zip)。
- `<fileSets>` 定义了需要包含的文件集合。
- `<files>` 定义了单个文件的处理规则,例如启动脚本 `start.sh` 的位置和目标路径[^1]。
---
#### 插件运行过程
当执行 `mvn package` 命令时,maven-assembly-plugin 会根据配置的描述符文件进行以下操作:
1. 收集项目的主要输出文件(如 JAR 文件)。
2. 根据描述符文件中的规则,将依赖项和其他资源添加到打包内容中。
3. 将所有内容按照指定的格式(如 tar.gz 或 zip)进行压缩并输出最终的打包文件[^1]。
---
#### 注意事项
1. **冲突处理**:如果项目同时使用了 spring-boot-maven-plugin 和 maven-assembly-plugin,则需要注意两者之间的冲突。可以通过禁用 spring-boot-maven-plugin 的默认行为来解决此问题。
2. **多环境支持**:可以结合 Maven 的 profile 功能为不同环境生成不同的打包文件[^1]。
3. **路径匹配规则**:在描述符文件中,路径和文件的匹配规则需要特别注意,否则可能导致打包失败或内容不完整[^1]。
---
#### Spring Boot 项目中的应用示例
以下是一个基于 Spring Boot 项目的 maven-assembly-plugin 配置示例:
```xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.4.2</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>
```
对应的 `assembly.xml` 文件如下:
```xml
<assembly>
<id>distribution-package</id>
<formats>
<format>tar.gz</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
<files>
<file>
<source>src/main/resources/start.sh</source>
<outputDirectory>/bin</outputDirectory>
<filtered>true</filtered>
</file>
</files>
</assembly>
```
---
#### 最终生成的目录结构示例
```
distribution-package/
├── application.jar
├── bin/
│ └── start.sh
└── lib/
└── dependencies.jar
```
---
阅读全文
相关推荐










