Maven Assembly Plugin示例

本文介绍如何在Maven项目中配置Assembly插件以自定义打包过程,包括生成tar.gz包的具体步骤及配置细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

工程结构如下:

在Maven配置文件pom.xml中添加Assembly插件,并将该插件绑定至package生命周期阶段

<project xmlns="http://maven.apache.org/POM/4.0.0" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
  		http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.sean</groupId>
	<artifactId>test</artifactId>
	<version>1.0.0</version>
	<packaging>jar</packaging>  
	
	<!--  这里指定的参数在之后可以通过${}进行调用  -->
	<properties>
    	<base.dir>/</base.dir>
  	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<resources>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.xsl</include>
					<include>**/*.xml</include>
					<include>**/*.xsd</include>
					<include>**/*.properties</include>
					<include>**/*.txt</include>
				</includes>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<includes>
					<include>META-INF/**</include>
				</includes>
			</resource>
		</resources>
		<plugins>
			<plugin>
				<!--  gtoupId的值属于默认值,可以省略  -->
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>2.2.1</version>
				<configuration>
					<descriptors>
						<descriptor>assembly.xml</descriptor>
					</descriptors>
				</configuration>
				<executions>
					<execution>
						<id>make-package</id>
						<!--  将Assembly的single目标绑定至Maven的package阶段,
								当调用mvn package时,Maven在调用maven-jar-plugin:jar生成jar包之后,
								将会调用maven-assembly-plugin:single生成tar.gz包  -->
						<phase>package</phase>
						<goals>
							<!--  Assembly的主要目标,其它目标将在未来版本移除,不推荐使用  
									single : your assemblies are created  
									as part of your normal build process.  -->
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

之后需要添加Assembly插件自己的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<assembly>
	<!-- 最终生成的包名称为artifactId-version-id.format,
			id主要用来区分包是由哪个assembly文件生成的,本例中
			artifactId:test
			version:1.0.0
			id:bin
			format:tar.gz
			最终将会在target文件夹下生成一个名为test-1.0.0-bin.tar.gz的包 -->									   
	<id>bin</id>
	<!-- zip, tar, tar.gz, tar.bz2, jar, dir, war 也是支持的  -->
	<formats>
	<!-- 可以同时打多种格式的包  -->
		<format>tar.gz</format>
		<!-- <format>zip</format> -->
	</formats>
	<!--  默认值是true,此时将创建一个名为artifactId-version的根文件夹,
			所有outputDirectory标签指定的路径将变为相对于该文件夹的路径。
			例如:outputDirectory的值为/test,则最终径为/artifactId-version/test
			如果includeBaseDirectory的值是false,则最终路径为/test  -->
	<includeBaseDirectory>false</includeBaseDirectory>
	<fileSets>
		<!-- 如果存在fileSet标签,并且在标签中没有使用includes标签指定打入包中的文件,
				默认情况下,工程中的所有文件(源文件、编译后产生的.class文件、配置文件等)
	      		都会被打入包中  -->
    	<fileSet>
	      	<outputDirectory>${base.dir}</outputDirectory>
	      	<includes>
	      		<include>readme.txt</include>
	      	</includes>
	    	<excludes>
	      		<exclude>*.xml</exclude>
	      		<exclude>script/*</exclude>
	      	</excludes> 
		</fileSet>
	</fileSets> 
 	<files>
	    <file>
	    	<!--  source不能使用模糊匹配,必须是特定的文件  -->
	      	<source>config.properties</source>
	      	<outputDirectory>${base.dir}</outputDirectory>
		</file>
	</files> 
 	<dependencySets>
 		<dependencySet>
 			<!--  true是默认值,本次构建出来的jar包属于当前这个dependencySet,
 					此时jar包将会被添加至新的tar.gz包中  -->
 			<useProjectArtifact>true</useProjectArtifact>
			<outputDirectory>${base.dir}</outputDirectory>
		</dependencySet> 
		<dependencySet>
			<!-- 本次构建出来的jar包不属于当前这个dependencySet  -->
			<useProjectArtifact>false</useProjectArtifact>
			<outputDirectory>${base.dir}test</outputDirectory>
			<!--  scope为test的依赖包属于当前dependencySet  -->
			<scope>test</scope>
		</dependencySet>
		<dependencySet>
			<useProjectArtifact>false</useProjectArtifact>
			<outputDirectory>${base.dir}provided</outputDirectory>
			<scope>provided</scope>
		</dependencySet>
	</dependencySets>
</assembly>  

Assembly插件配置参数详细说明参见:

http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值