Assembly插件主要是聚合项目的输出,比如依赖,模块以及其他文件。通俗的来说,就是将项目内容按照一定规则及指定格式重新组合并输出。它支持的输出格式有多种:
  - jar jar包
- war war包
- dir 文件目录
- zip zip压缩包
- tar tar压缩包
- tar.gz
- tar.bz2
- tar.xz
为什么需要Assembly插件呢?因为对于服务类型的后台程序,线上运行时一般通过脚本的方式启动和停止,而项目中的目录结构复杂且不够直观,且项目文件也需要合并和过滤,Assembly插件就可以帮助我们完成。
 Assembly使用
 使用Assembly插件需要在pom.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> 
 Assembly插件一般绑定的阶段为package,主要的插件目标就是assembly:single。在<configuration>的<descriptor>中指定assembly组装规则的XML。
 现在我们的项目目录如下
 + src     + main         + java         + resources             + jdbc.properties         + bin             + start.sh             + stop.sh         + assembly             + assembly.xml 
 最终我们想要生成的目录如下
 + bin     start.sh     stop.sh + conf     jdbc.properties + lib(依赖jar包目录)     xxx.jar 
 同时希望能输出成zip格式,那么assembly.xml的配置如下
 <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">   <id>distribution</id>   <!-- 输出格式 -->   <formats>     <format>jar</format>     <format>zip</format>   </formats>   <!-- 指定文件组的组装方式 -->   <fileSets>   	<!-- 将项目中src/main/bin目录下的脚本文件copy到target目录的bin目录下 -->     <fileSet>       <directory>src/main/bin</directory>       <outputDirectory>bin</outputDirectory>     </fileSet>     <!-- 将项目中src/main/resources目录下的资源文件copy到target目录的conf目录下 -->     <fileSet>       <directory>src/main/resources</directory>       <outputDirectory>conf</outputDirectory>       <!-- 过滤资源文件,对其中的maven变量进行复制 --> 	  <filtered>true</filtered>     </fileSet>   </fileSets>   <!-- 指定依赖jar包输出的目录 -->   <dependencySets>   	<dependencySet>   		<useProjectArtifact>true</useProjectArtifact>   		<outputDirectory>lib</outputDirectory>   		<!-- 只包含runtime作用域的依赖 -->   		<scope>runtime</scope>   	</dependencySet>   </dependencySets> </assembly> 
 命令行执行
 mvn clean package 
 项目的target目录下会生成两个文件
  - xxx-distribution.jar
- xxx-distribution.zip
xxx是项目的finalName-version,而distribution后缀是默认加上assembly.xml中的id,如果不想要这个后缀,可以在pom.xml的插件的<configuration>中设置appendAssemblyId为false
 <configuration> 	<appendAssemblyId>false</appendAssemblyId> 	<descriptors> 		<descriptor>src/main/assembly/assembly.xml</descriptor> 	</descriptors> </configuration> 
 如果有另外的变量属性文件,也可以在<configuration>中配置
 <configuration> 	<appendAssemblyId>false</appendAssemblyId> 	<filters> 		<filter>src/main/filter/filter.properties</filter> 	</filters> 	<descriptors> 		<descriptor>src/main/assembly/assembly.xml</descriptor> 	</descriptors> </configuration> 
 assembly.xml中的更多配置见http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html
 pom文件中插件的更多配置见http://maven.apache.org/plugins/maven-assembly-plugin/single-mojo.html
 includes/excludes
 在<fileSet>中可以使用includes/excludes进行细粒度的控制,此控制对<filtered>也生效
 <fileSet> 	<directory>src/main/resources</directory> 	<outputDirectory>conf</outputDirectory> 	<filtered>true</filtered> 	<excludes> 		<exclude>some/path1</exclude> 	</excludes> </fileSet> <fileSet> 	<directory>src/main/resources</directory> 	<outputDirectory>conf</outputDirectory> 	<filtered>false</filtered> 	<includes> 		<include>some/path2</include> 	</includes> </fileSet> 
 第一个<fileSet>将src/main/resources输出到target/conf,并且过滤变量属性,但是排除some/path1的文件。第二个<fileSet>将src/main/resources下的some/path2输出到target/conf,但不过滤变量属性。
 创建可执行的jar
 默认情况下,通过mvn package生成的jar包中因为没有指定Main-Class属性,因此并不能使用-jar配置直接运行。Assembly插件支持<archive>元素(maven-archive)配置Main-Class。
 <configuration> 	<archive> 		<manifest> 			<mainClass>com.lcifn.maven.Application</mainClass> 		</manifest> 	</archive> </configuration> 
 再执行命令
 mvn clean package 
 新生成的jar包中的META-INF/MANIFEST.MF中就包含了Main-Class属性
 使用containerDescriptorHandlers合并文件
 Assembly插件支持常用的文件合并功能,特别是META-INF下的services文件或spirng文件(spring.handlers和spring.schemas)。
 metaInf-services
 聚合所有的META-INF/services文件合并成一个文件
 <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">   ....   <containerDescriptorHandlers>     <containerDescriptorHandler>       <handlerName>metaInf-services</handlerName>     </containerDescriptorHandler>   </containerDescriptorHandlers> </assembly> 
 metaInf-spring
 聚合所有的**META-INF/spring.**文件合并成一个文件
 <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">   ....   <containerDescriptorHandlers>     <containerDescriptorHandler>       <handlerName>metaInf-spring</handlerName>     </containerDescriptorHandler>   </containerDescriptorHandlers> </assembly> 
 file-aggregator
 也可以给定正则表达式匹配文件合并成一个文件,以下匹配所有file.txt然后合并成一个file.txt
 <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">   ....   <containerDescriptorHandlers>     <containerDescriptorHandler>       <handlerName>file-aggregator</handlerName>       <configuration>         <filePattern>.*/file.txt</filePattern>         <outputPath>file.txt</outputPath>       </configuration>     </containerDescriptorHandler>   </containerDescriptorHandlers> </assembly> 
 参考文档:
  - http://maven.apache.org/plugins/maven-assembly-plugin/index.html