愿文地址:https://www.jianshu.com/p/e8585a991e8e

当你使用 Maven 对项目打包时,你需要了解以下 3 个打包 plugin,它们分别是

plugin function
maven-jar-plugin maven 默认打包插件,用来创建 project jar
maven-shade-plugin 用来打可执行包,executable(fat) jar
maven-assembly-plugin 支持定制化打包方式,例如 apache 项目的打包方式

下面我们就简单介绍一下 maven-assembly-plugin。

使用方法

  • 使用 descriptorRefs(官方提供的定制化打包方式),官方提供的 descriptorRef 有 bin, jar-with-dependencies, src, project。【不建议使用】
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<!-- NOTE: We don't need a groupId specification because the group is
org.apache.maven.plugins ...which is assumed by default.
-->
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
[...]
</project>
  • 使用 descriptors,指定打包文件 src/assembly/src.xml,在该配置文件内指定打包操作。
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<descriptors>
<descriptor>src/assembly/src.xml</descriptor>
</descriptors>
</configuration>
[...]
</project>

描述符文件元素

id

<id>release</id>

id 标识符,添加到生成文件名称的后缀符。如果指定 id 的话,目标文件则是 ${artifactId}-${id}.tar.gz

formats

maven-assembly-plugin 支持的打包格式有zip、tar、tar.gz (or tgz)、tar.bz2 (or tbz2)、jar、dir、war,可以同时指定多个打包格式

  <formats>
<format>tar.gz</format>
<format>dir</format>
</formats>

dependencySets

用来定制工程依赖 jar 包的打包方式,核心元素如下表所示。

元素 类型 作用
outputDirectory String 指定包依赖目录,该目录是相对于根目录
includes/include* List<String> 包含依赖
excludes/exclude* List<String> 排除依赖
<dependencySets>
<dependencySet>
<outputDirectory>/lib</outputDirectory>
</dependencySet>
</dependencySets>

fileSets

管理一组文件的存放位置,核心元素如下表所示。

元素 类型 作用
outputDirectory String 指定文件集合的输出目录,该目录是相对于根目录
includes/include* List<String> 包含文件
excludes/exclude* List<String> 排除文件
fileMode String 指定文件属性,使用八进制表达,分别为(User)(Group)(Other)所属属性,默认为 0644
  <fileSets>
<fileSet>
<includes>
<include>bin/**</include>
</includes>
<fileMode>0755</fileMode>
</fileSet> <fileSet>
<includes>
<include>/conf/**</include>
<include>logs</include>
</includes>
</fileSet> </fileSets>

files

可以指定目的文件名到指定目录,其他和 fileSets 相同,核心元素如下表所示。

元素 类型 作用
source String 源文件,相对路径或绝对路径
outputDirectory String 输出目录
destName String 目标文件名
fileMode String 设置文件 UNIX 属性
 <files>
<file>
<source>README.txt</source>
<outputDirectory>/</outputDirectory>
</file>
</files>

样例

工程目录结构

 
scribe-log4j2-test

pom.xml

<build>
<finalName>scribe-log4j2-test</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/main/assembly/release.xml</descriptor>
</descriptors>
</configuration> <executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

release.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"> <formats>
<format>tar.gz</format>
<format>dir</format>
</formats> <dependencySets>
<dependencySet>
<outputDirectory>/lib</outputDirectory>
</dependencySet>
</dependencySets> <fileSets>
<fileSet>
<includes>
<include>bin/**</include>
</includes>
<fileMode>0755</fileMode>
</fileSet> <fileSet>
<includes>
<include>/conf/**</include>
<include>logs</include>
</includes>
</fileSet> </fileSets> <files>
<file>
<source>README.txt</source>
<outputDirectory>/</outputDirectory>
</file>
</files> </assembly>

最终创建生成可执行二进制工程

参考

maven 入门指南
maven 生命周期
Maven 默认插件以及功能
maven 依赖管理
maven-shade-plugin 入门指南
maven-assembly-plugin

最新文章

  1. 终于开始用github了
  2. css 实现未知图片垂直居中
  3. angular js 自定义指令
  4. PowerDesigner中name和code取消自动关联
  5. DedeCMS时间格式
  6. 转:iOS 7人机界面准则
  7. 17款提高编程效率的css工具
  8. 这辆车已覆盖免费wifi
  9. tf–idf算法解释及其python代码实现(下)
  10. javascript full screen 全屏显示 页面元素
  11. Codeforces758C
  12. js数组、内置对象、自定义对象
  13. 接口自动化测试遭遇问题,excel中取出来的json串,无法使用requests去请求解决办法
  14. Windows服务器修改网站上传文件的大小限制
  15. CF809C Find a car
  16. Process 开启子进程 的两种方式、join控制子进程、守护进程
  17. 【ASP.NET MVC系列】浅谈ASP.NET 程序发布过程
  18. 驱动开发--【字符设备、块设备简介】【sky原创】
  19. Navicat的外键设置
  20. 协作工具 discord 和 slack

热门文章

  1. Oracle 和pl/sql以及pl/sql developer
  2. JS求数组最大值常用方法
  3. Scala中 下划线的用处
  4. NuGet 如何设置图标
  5. tcp短连接和长连接
  6. 聚类分析 一、k-means
  7. Flink State Backends (状态后端)
  8. 基于ambassador实现K8S灰度发布
  9. $Luogu2512/CH122/AcWing122$糖果传递 模拟
  10. StrategyPattern(策略模式)-----Java/.Net