Maven工程插件(maven-assembly-plugin)

说明

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.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
如果你在使用 Maven 时遇到找不到插件 maven-assembly-plugin 的问题,可以尝试以下解决方法: 1. 检查 Maven 的配置文件 settings.xml 中是否配置了正确的镜像源。可以在该文件中添加如下配置: ``` <mirrors> <mirror> <id>aliyunmaven</id> <name>aliyun maven</name> <url>https://maven.aliyun.com/repository/public</url> <mirrorOf>*</mirrorOf> </mirror> </mirrors> ``` 这个配置会将 Maven 的默认镜像源替换为阿里云的镜像源,由于阿里云的镜像源是国内访问速度较快的镜像源之一,可以尝试使用这个配置来解决找不到插件的问题。 2. 检查 Maven 的本地仓库中是否已经下载了 maven-assembly-plugin 插件。可以在本地仓库的目录中查找是否有以下路径存在: ``` ~/.m2/repository/org/apache/maven/plugins/maven-assembly-plugin/ ``` 如果该路径不存在,说明 Maven 还没有下载该插件,可以尝试执行以下命令来下载该插件: ``` mvn dependency:get -Dartifact=org.apache.maven.plugins:maven-assembly-plugin:3.3.0 ``` 这个命令会下载 maven-assembly-plugin 插件的最新版本到本地仓库中。 3. 检查 Maven 的 pom.xml 文件中是否正确引入了 maven-assembly-plugin 插件。可以在 pom.xml 文件中添加以下配置: ``` <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.3.0</version> </plugin> </plugins> </build> ``` 这个配置会让 Maven 在构建时自动引入 maven-assembly-plugin 插件,如果该插件已经下载到本地仓库中,就可以正常使用了。 如果以上方法都无法解决问题,可以尝试在 Maven 的命令中加入 -U 参数来强制更新本地仓库中的插件版本。命令如下: ``` mvn clean package -U ``` 这个命令会清除 Maven 的缓存,并从远程仓库下载最新的插件版本到本地仓库中。如果该插件版本存在问题,可以尝试使用其他版本来解决问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

P("Struggler") ?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值