Usage
http://maven.apache.org/plugins/maven-assembly-plugin/usage.html
In order for this discussion to be useful, it’s critical to cover two topics: configuration of the plugin - both inside the POM and, where possible, from the command line - and the different execution styles.
配置分为两类:一是configuration,二是execution
configuration
If you want to use one of the prefabricated assembly descriptors, you configure which descriptor to use with the <``descriptorRefs>``/<``descriptorRef>
parameter.
如果使用预定的**assembly descriptors**
,使用**<**``**descriptorRefs>**``**/<**``**descriptorRef>**
If you want to use a custom assembly descriptor, you configure the path to your descriptor using the <``descriptors>``/<``descriptor>
parameter.
如果使用预定的**assembly descriptors**
,使用**<**``**descriptors>**``**/<**``**descriptor>**
预定descriptor
If we want to create an assembly binary that includes our project’s dependencies, we can take advantage of one of the Assembly Plugin’s prefabricated descriptors.
如果要将项目的依赖打包,可以使用预定的descriptors,如下所示:
<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.3.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
自定义descriptor:(基本使用这种方式)
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<descriptors>
<descriptor>src/assembly/src.xml</descriptor>
</descriptors>
</configuration>
</plugin>
<!-- datax-web admin 示例-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<attach>false</attach>
<descriptors>
<descriptor>${basedir}/src/main/assembly/deploy.xml</descriptor>
</descriptors>
<finalName>${project.artifactId}_${project.version}_1</finalName>
<outputDirectory>${project.parent.basedir}/packages</outputDirectory>
</configuration>
<executions>
<execution>
<id>assemble</id>
<goals>
<goal>single</goal>
</goals>
<!-- install -->
<phase>install</phase>
</execution>
</executions>
</plugin>
execution
In most cases, you’ll want to make sure your assemblies are created as part of your normal build process. This ensures the assembly archives are made available for installation and deployment, and that they are created during the release of your project. This is handled by the assembly:single
goal
大多数时候使用assembly:single就够了
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</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>
Then, to create a project assembly, simple execute the normal package phase from the default lifecycle:
mvn package
When this build completes, you should see a file in the target directory with a name similar to the following:
target/sample-1.0-SNAPSHOT-jar-with-dependencies.jar
Examples
http://maven.apache.org/plugins/maven-assembly-plugin/examples/index.html
排除(过滤)一些文件
http://maven.apache.org/plugins/maven-assembly-plugin/examples/single/filtering-some-distribution-files.html
假设资源根目录下有以下文件:
- README.txt
- NOTICE.txt
- LICENSE.txt
只需保留LICENSE.txt,其他两个过滤掉。使用下面的xml文件
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd">
<id>distribution</id>
<formats>
<format>jar</format>
</formats>
<files>
<file>
<source>README.txt</source>
<outputDirectory></outputDirectory>
<filtered>true</filtered>
</file>
<file>
<source>LICENSE.txt</source>
<outputDirectory></outputDirectory>
</file>
<file>
<source>NOTICE.txt</source>
<outputDirectory></outputDirectory>
<filtered>true</filtered>
</file>
</files>
</assembly>
Alternatively, if there are many .txt files to include inside <files>
, we can setup both<fileSets>
and <files>
like so:
如果有太多文件要打包,可以使用如下的xml:
<fileSets>
<fileSet>
<directory>${basedir}</directory>
<includes>
<include>*.txt</include>
</includes>
<excludes>
<exclude>README.txt</exclude>
<exclude>NOTICE.txt</exclude>
</excludes>
</fileSet>
</fileSets>
<files>
<file>
<source>README.txt</source>
<outputDirectory></outputDirectory>
<filtered>true</filtered>
</file>
<file>
<source>NOTICE.txt</source>
<outputDirectory></outputDirectory>
<filtered>true</filtered>
</file>
</files>
Component Descriptors
http://maven.apache.org/plugins/maven-assembly-plugin/examples/single/using-components.html
Suppose you have a project which will be distributed in two forms: one for use with appserver A and another for appserver B. And as customization for these two servers, you need to exclude some dependencies which are not used by the appserver you will be distributing.
需求描述:将项目打2个包,一个包可能是给测试用的,一个包可能是给发布用的。这两个包的依赖可能有些不同,测试包可能依赖A.jar,发布包可能依赖B.jar,但是他们都共同依赖parent.jar。最终可以使用<componentDescriptors>
来描述:
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd">
<id>appserverB</id>
<formats>
<format>zip</format>
</formats>
<componentDescriptors>
<componentDescriptor>src/assembly/component.xml</componentDescriptor>
</componentDescriptors>
<dependencySets>
<dependencySet>
<outputDirectory>/lib</outputDirectory>
<includes>
<include>application:appserverB</include>
</includes>
</dependencySet>
</dependencySets>
</assembly>
**src/assembly/component.xml**
表示公共依赖。
开源项目用法
datax
https://github.com/alibaba/DataX/blob/master/package.xml
https://github.com/alibaba/DataX/blob/master/core/src/main/assembly/package.xml
datax-web
先说明下模块加载顺序:rpc->core->admin->executor->assembly->transformer
其中**admin,executor,assembly,transformer**
都使用了assembly插件
admin: https://github.com/WeiYe-Jing/datax-web/blob/master/datax-admin/src/main/assembly/deploy.xml
executor: https://github.com/WeiYe-Jing/datax-web/blob/master/datax-executor/src/main/assembly/deploy.xml
assembly:https://github.com/WeiYe-Jing/datax-web/blob/master/datax-assembly/package.xml
assembly负责组装前两个产出
Assembly xml 文件结构
http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html
注意: 最终assembly文件的名称是 pom.finalName+assembly.id+(assembly.format)
�
具体的一些标签解析:
- includeBaseDirectory:
假设最后的压缩包名叫 twx-demo.tar.gz
,如果includeBaseDirectory=true
,那么解压出的一级目录就是twx-demo
- fileSet.fileMode: 设置文件读写权限
- fileSet.lineEnding: 设置文件内部的换行符
- dependencySet.useProjectArtifact: 项目本身生成的jar是否要被包括进来。默认是true。如果是true,那我们的模块datax-admin.jar最终也会被打包进lib目录.否则不会
�