Maven是一个项目构建管理工具,它能自动完成任何java项目的构建阶段。无论何时构建项目,它都会创建jar或war格式的单个模块。
因此它能将你的项目包括配置文件和其他XML文件构建成不同的自定义输出模块类型,如zipping、jar、war和其他tar.gz格式。
大家都知道maven-war-plugin只生成war文件,但在项目中,你需要以不同的格式归档,比如压缩格式,在这种情况下,我们将使用这个插件。
通常,任何项目都会生成war或jar文件。因此,如果项目(工件名称为CloudHadoop),安装命令的结果就是CloudHadoop-1.0.0-SNAPSHOT.war。此外,它生成了一个单一的战争文件。
但使用这个插件,我们可以通过一次执行来生成多个压缩文件,如jar、zip等。
如果你想把你的项目源代码及其依赖项和配置文件生成一个压缩文件(CloudHadoop.zip),在我们的项目中使用maven-assembly-plugin有三个任务:
- 配置maven-assembly-plugin插件
- 创建自定义汇编文件
- 使用maven命令运行汇编目标
如何配置maven-assembly-plugin插件
Maven是基于插件的架构,所以如果我们想支持任何额外的功能,你需要配置插件。
在你的pom.xml中添加以下插件代码
<project>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<version>2.2.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>project</descriptorRef>
</descriptorRefs>
<descriptors>
<descriptor>src/assembly/zipCode.xml</descriptor>
</descriptors>
<finalName>${pom.artifactId}</finalName>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</project>
maven-assembly-plugin 是用 plugin 标签定义的插件,该标签有groupId和版本 descriptorRef代表压缩捆绑文件中的文件夹结构类型。
descriptorRef 的可能元素是jar-with-dependencies(像 MANIFEST 一样的 jar 格式),project(在项目目录结构中压缩),src (只有 src 文件夹和子文件夹)
descriptor 指明包含代码的自定义汇编XML
execution 阶段代表执行该汇编代码的目标
如何创建自定义汇编描述符文件?
我们可以定义自己的自定义汇编文件来创建我们的模块。文件名是zipCode.xml。你可以通过在插件声明中配置这个文件来定义多个汇编文件。
<assembly>
<id>src</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>src/main</directory>
<outputDirectory>zipDirectory</outputDirectory>
<includes>
<include>\*.\*</include>
</includes>
</fileSet>
</fileSets>
</assembly>
格式指定了你的压缩文件的格式的结果。
FileSets定义了你项目源目录下的文件。在你的打包目标运行后,目标目录是zipDirectory。
用汇编程序构建你的项目
assemble插件可以用两种方式运行。
一种是独立的执行方式。
mvn assembly:assembly
```.once the above goal is executed, it creates a zipped file which contains files in the form of same project directory structure.
and other ways is attach the plugin execution with any predefined goals.
```markup
maven package
如何用maven创建多个jar文件
列出在一次执行中生成多个jar文件的高级步骤
- 在pom.xml中定义
maven-assembler-plugin配置 - 在
maven-assembler-plugin插件配置中定义多个执行元素,在自定义汇编器xml文件中配置多个规则。 - 对于每个jar,定义自己的自定义汇编文件,包括格式类型、源和目标目录信息,还包括包括和排除元素。
- 将目标的执行定义为单一的或与任何预定义的目标相连的。
- 最后,在命令行中使用maven命令构建你的项目。
希望你能通过一个基本的例子了解maven汇编程序插件的基本概念。