在应用开发中你是否想要创建一个包含脚本、配置文件以及所有运行时所依赖的元素(jar)的发布jar包。Assembly插件能帮你构建一个完整的发布包。
Assembly插件会生成 “assemblies”, 此特性等同于的Maven 1 distribution plug-in.。该插件不仅支持创建二进制归档文件,也支持创建源码归档文件。这些assemblies定义在一个assembly描述符文件里。你可以选择自定义assembly描述符或者直接使用插件自带的三个预定义描述符中的任何一个.
目前Assembly插件支持如下格式的归档文件:
zip
tar.gz
tar.bz2
jar
dir
war
and any other format that the ArchiveManager has been configured for
Maven 2上使用assembly的简单步骤:
从预定义描述符里选择一个或者自己编写一个assembly描述符号。
工程的pom.xml里配置Assembly插件。
在工程根目录下运行”mvn assembly:assembly”命令 。
http://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html
2、什么是Assembly?
“assembly”是把一组文件、目录、依赖元素组装成一个归档文件.Assembly Plugin的描述符可以定义任何一个文件或者目录归档方式。举个例子,如果的你的Maven 2工程包含”src/main/bin”这个目录,你可以指示Assembly插件复制“src/main/bin”目录下所有的文件到bin目录里(归档文件里的目录),并且可以修改它们的权限属性(UNIX mode)
Maven 2.0的Assembly插件目的是提供一个把工程依赖元素、模块、网站文档等其他文件存放到单个归档文件里。使用任何一个预定义的描述符你可以轻松的构建一个发布包。这些描述符能处理一些常用的操作,如:把依赖的元素的归档到一个jar文件. 当然, 你可以自定义描述符来更灵活的控制依赖,模块,文件的归档方式。
3、配置说明
3.1、pom.xml配置说明
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions> <!--执行器 mvn assembly:assembly-->
<execution>
<id>make-zip</id><!--名字任意 -->
<phase>package</phase><!-- 绑定到package生命周期阶段上 -->
<goals>
<goal>single</goal><!-- 只运行一次 -->
</goals>
<configuration>
<descriptors> <!--描述文件路径-->
<descriptor>src/main/resources/zip.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
3.2 assembly.xml配置说明:
<assembly>
<id>bin</id>
<!-- Generates a zip package containing the needed files -->
<formats>
<format>tar.gz</format>
</formats>
<!-- Adds dependencies to zip package under lib directory -->
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
<fileSets>
<!-- Adds startup scripts to the root directory of zip package -->
<fileSet>
<directory>bin</directory>
<outputDirectory>bin</outputDirectory>
<includes>
<include>*.*</include>
</includes>
</fileSet>
<fileSet>
<directory>META-INF</directory>
<outputDirectory>META-INF</outputDirectory>
<includes>
<include>*.*</include>
</includes>
</fileSet>
</fileSets>
</assembly>
3.3、assembly.xml 格式属性说明
打包的文件格式
可以有:tar.zip war zip
<formats>
<format>zip</format>
</formats>
需要打包的路径
<directory>${project.basedir}</directory>
打包后输出的路径
<outputDirectory>/</outputDirectory>
打包需要包含的文件
<excludes>
<exclude>junit:junit</exclude>
<exclude>commons-lang:commons-lang</exclude>
<exclude>commons-logging:commons-logging</exclude>
</excludes>
当前项目构件是否包含在这个依赖集合里。
<useProjectArtifact>true</useProjectArtifact>
依赖包打包到目录下
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory><!-- 将scope为runtime的依赖包打包到lib目录下。 -->
<useProjectArtifact>true</useProjectArtifact>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
关注微信公众号和今日头条,精彩文章持续更新中。。。。。