maven-assembly-plugin 打包的配置

maven的工程的创建以及生命周期

https://www.jianshu.com/p/fd43b3d0fdb0

当你使用 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。【不建议使用】
 
  1. <project>

  2. [...]

  3. <build>

  4. [...]

  5. <plugins>

  6. <plugin>

  7. <!-- NOTE: We don't need a groupId specification because the group is

  8. org.apache.maven.plugins ...which is assumed by default.

  9. -->

  10. <artifactId>maven-assembly-plugin</artifactId>

  11. <version>3.0.0</version>

  12. <configuration>

  13. <descriptorRefs>

  14. <descriptorRef>jar-with-dependencies</descriptorRef>

  15. </descriptorRefs>

  16. </configuration>

  17. [...]

  18. </project>

  19.  
  • 使用 descriptors,指定打包文件 src/assembly/src.xml,在该配置文件内指定打包操作。
 
  1. <project>

  2. [...]

  3. <build>

  4. [...]

  5. <plugins>

  6. <plugin>

  7. <artifactId>maven-assembly-plugin</artifactId>

  8. <version>3.0.0</version>

  9. <configuration>

  10. <descriptors>

  11. <descriptor>src/assembly/src.xml</descriptor>

  12. </descriptors>

  13. </configuration>

  14. [...]

  15. </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,可以同时指定多个打包格式

 
  1. <formats>

  2. <format>tar.gz</format>

  3. <format>dir</format>

  4. </formats>

  5.  

dependencySets

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

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

  2. <dependencySet>

  3. <outputDirectory>/lib</outputDirectory>

  4. </dependencySet>

  5. </dependencySets>

fileSets

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

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

  2. <fileSet>

  3. <includes>

  4. <include>bin/**</include>

  5. </includes>

  6. <fileMode>0755</fileMode>

  7. </fileSet>

  8.  
  9. <fileSet>

  10. <includes>

  11. <include>/conf/**</include>

  12. <include>logs</include>

  13. </includes>

  14. </fileSet>

  15.  
  16. </fileSets>

files

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

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

  2. <file>

  3. <source>README.txt</source>

  4. <outputDirectory>/</outputDirectory>

  5. </file>

  6. </files>

例子:

pom.xml

 
  1. <build>

  2. <finalName>scribe-log4j2-test</finalName>

  3. <plugins>

  4. <plugin>

  5. <groupId>org.apache.maven.plugins</groupId>

  6. <artifactId>maven-compiler-plugin</artifactId>

  7. </plugin>

  8.  
  9. <plugin>

  10. <groupId>org.apache.maven.plugins</groupId>

  11. <artifactId>maven-assembly-plugin</artifactId>

  12. <configuration>

  13. <descriptors>

  14. <descriptor>src/main/assembly/release.xml</descriptor>

  15. </descriptors>

  16. </configuration>

  17.  
  18. <executions>

  19. <execution>

  20. <id>make-assembly</id> <!-- this is used for inheritance merges -->

  21. <phase>package</phase> <!-- bind to the packaging phase -->

  22. <goals>

  23. <goal>single</goal>

  24. </goals>

  25. </execution>

  26. </executions>

  27. </plugin>

  28. </plugins>

  29. </build>

release.xml

 
  1. <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"

  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  3. xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">

  4.  
  5. <formats>

  6. <format>tar.gz</format>

  7. <format>dir</format>

  8. </formats>

  9.  
  10. <dependencySets>

  11. <dependencySet>

  12. <outputDirectory>/lib</outputDirectory>

  13. </dependencySet>

  14. </dependencySets>

  15.  
  16. <fileSets>

  17. <fileSet>

  18. <includes>

  19. <include>bin/**</include>

  20. </includes>

  21. <fileMode>0755</fileMode>

  22. </fileSet>

  23.  
  24. <fileSet>

  25. <includes>

  26. <include>/conf/**</include>

  27. <include>logs</include>

  28. </includes>

  29. </fileSet>

  30.  
  31. </fileSets>

  32.  
  33. <files>

  34. <file>

  35. <source>README.txt</source>

  36. <outputDirectory>/</outputDirectory>

  37. </file>

  38. </files>

  39.  
  40. </assembly>

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


2、http://blog.csdn.net/WANGYAN9110/article/details/38646677

 

使用场景

在使用maven来管理项目时,项目除了web项目,还有可能为控制台程序,一般用于开发一些后台服务的程序。最近在工作中也遇到了这种场景,使用quartz开发一个任务调度程序。程序中依赖很多jar包,项目的启动时只需要初始化spring容器即可。

使用方法

使用一个简单的基于spring框架的demo来做程序示例,来介绍maven assembly插件的使用方法。
项目中的代码目录如下:

在以上代码目录中,assembly目录下为打包的描述文件,下面会详细介绍该文件,bin目录下为启动脚本以及readme等文件,main下为maven标准中建立的文件,java代码以及配置文件位于该目录下。
打包完成后压缩包目录如下:

打包完成后,我们可以看到bin目录来存放启动脚本等文件,config为配置文件,lib下为运行时依赖的jar包。
使用maven assembly插件需要在pom文件中配置,添加这个插件

 
  1. <plugin>

  2. <artifactId>maven-assembly-plugin</artifactId>

  3. <version>2.4.1</version>

  4. <executions>

  5. <execution>

  6. <id>make-zip</id>

  7. <!-- 绑定到package生命周期阶段上 -->

  8. <phase>package</phase>

  9. <goals>

  10. <!-- 绑定到package生命周期阶段上 -->

  11. <goal>single</goal>

  12. </goals>

  13. <configuration>

  14. <descriptors> <!--描述文件路径-->

  15. <descriptor>src/assembly/assembly.xml</descriptor>

  16. </descriptors>

  17. </configuration>

  18. </execution>

  19. </executions>

  20. </plugin>

其中execution节点,我们配置了执行maven assembly插件的一些配置,descriptor节点配置指向assembly.xml的路径。
在assembly.xml配置了,我们打包的目录以及相应的设置

 
  1. <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"

  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  3. xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">

  4. <id>distribution</id>

  5. <formats>

  6. <format>zip</format>

  7. </formats>

  8. <fileSets>

  9. <fileSet>

  10. <directory>${project.basedir}\src\main\resources</directory>

  11. <outputDirectory>\</outputDirectory>

  12. </fileSet>

  13. <fileSet>

  14. <directory>${project.basedir}\src\bin</directory>

  15. <outputDirectory>\bin</outputDirectory>

  16. </fileSet>

  17. </fileSets>

  18. <dependencySets>

  19. <dependencySet>

  20. <useProjectArtifact>true</useProjectArtifact>

  21. <outputDirectory>lib</outputDirectory>

  22. <!-- 将scope为runtime的依赖包打包到lib目录下。 -->

  23. <scope>runtime</scope>

  24. </dependencySet>

  25. </dependencySets></assembly>

assembly.xml的配置项非常多,可以参考http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html
以上只是用了很少的一部分。
format设置包输出的格式,以上格式设置的为zip格式,目前还支持zip,tar,tar.gz,tar.bz2,jar,dir,war格式
fileSet定义代码目录中与输出目录的映射,在该节点下还有 <includes/>,<excludes/>两个非常有用的节点。
比如:

 
  1. <fileSet>

  2. <directory>${project.basedir}\src\main\resources</directory>

  3. <outputDirectory>\</outputDirectory>

  4. <includes>

  5. <include>some/path</include>

  6. </includes>

  7. <excludes>

  8. <exclude>some/path1</exclude>

  9. </excludes>

  10. </fileSet>

以上代码表示归档时包括some/path,不包括some/path1
dependencySets节点下为依赖设置
在上述配置中,表示所有运行时依赖的jar包归档到lib目录下。在上述截图中lib目录下的文件就是所有依赖的jar包

猜你喜欢

转载自blog.csdn.net/JacksonKing/article/details/89519727