说明
maven-assembly-plugin 是 Maven 的一个插件,主要用于将 Maven 项目打包成可执行的程序或分发包。
该插件可以将项目的依赖和资源文件打包成一个可执行的 JAR、WAR 或者 ZIP 文件,也可以将所有文件打包成一个单独的分发包。
使用该插件的好处是可以简化项目的部署和分发过程,让项目更加易于管理。
该插件的使用需要在项目的 pom.xml 文件中进行配置,具体可以参考官方文档:
http://maven.apache.org/plugins/maven-assembly-plugin/
配置说明
配置 maven-assembly-plugin
在项目的 pom.xml 文件中添加以下配置:
<!--主要用于将 Maven 项目打包成可执行的程序或分发包 -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>assembly/assembly.xml</descriptor>
</descriptors>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
定义 assembly 描述文件
新建一个 assembly.xml 文件,用于定义打包规则。例如:
<assembly>
<id>assembly</id>
<formats>
<format>zip</format>
<format>dir</format>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<!-- -->
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>conf</outputDirectory>
<includes>
<include>*.xml</include>
<include>*.properties</include>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<fileMode>0644</fileMode>
</fileSet>
<fileSet>
<directory>assembly/bin</directory>
<outputDirectory>bin</outputDirectory>
<fileMode>0755</fileMode>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<includes>
<include>com.gee.maven:mavenEnforcerExample</include>
</includes>
<outputDirectory>lib</outputDirectory>
</dependencySet>
</dependencySets>
</assembly>
其中,id 指定了 assembly 描述文件的标识;formats 指定了打包的格式;includeBaseDirectory 表示是否包含基本目录;dependencySets 指定了依赖项的输出路径;fileSets 指定 JAR 包的输出路径和文件范围。
打包时间戳插件
maven 默认时间戳插件是UTC
<maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
<!--GMT+8-->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.4</version>
<configuration>
<timestampFormat>yyyyMMddHHmmss</timestampFormat>
</configuration>
<executions>
<execution>
<goals>
<goal>create-timestamp</goal>
</goals>
</execution>
</executions>
<inherited>false</inherited>
</plugin>
Simply put
The Maven Assembly Plugin is a plugin for Apache Maven that provides the ability to create an executable distribution of a project. It allows you to package your project along with its dependencies and resources into a single, executable JAR or ZIP file.
The plugin is configured using an XML file called an assembly descriptor. The descriptor specifies the contents of the distribution, including the files to include, the directory structure, and any additional configuration files that are required.
To use the Maven Assembly Plugin, you need to add it to your project’s pom.xml file and configure it with the desired assembly descriptor. You can then run the plugin using the “mvn assembly:assembly” command.
The plugin provides several pre-defined assembly descriptors that you can use out-of-the-box, such as the “jar-with-dependencies” descriptor, which packages your project along with its dependencies into a single, executable JAR file.
In addition to the pre-defined descriptors, you can also create your own custom assembly descriptors to package your project in a way that meets your specific requirements.
Overall, the Maven Assembly Plugin is a powerful tool for creating executable distributions of your Maven projects. It simplifies the process of packaging your project and its dependencies into a single, deployable artifact, making it easier to distribute and run your application.