Maven Assembly Plugin示例

工程结构如下:

在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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Maven-assembly-plugin是一个Maven插件,用于创建可部署的归档文件(assembly)。 在Maven项目中,可以使用maven-assembly-plugin来定义和配置自定义的归档文件。这些归档文件可以是可执行的JAR文件、WAR文件或其他自定义格式的文件。 对于mapper的问题,可能是指在使用maven-assembly-plugin时,如何配置mapper。在maven-assembly-plugin中,mapper是用来指定如何映射项目中的文件到归档文件中的位置。 你可以使用以下示例配置来定义mapper: ```xml <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>3.3.0</version> <configuration> <descriptors> <descriptor>assembly.xml</descriptor> </descriptors> </configuration> </plugin> ``` 然后,在`src/main/assembly`目录下创建一个`assembly.xml`文件,定义你的mapper配置。下面是一个简单的示例: ```xml <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd"> <id>my-assembly</id> <formats> <format>zip</format> </formats> <fileSets> <fileSet> <directory>${project.build.outputDirectory}</directory> <outputDirectory>/</outputDirectory> <includes> <include>*.jar</include> </includes> </fileSet> </fileSets> </assembly> ``` 在这个示例中,我们定义了一个名为`my-assembly`的归档文件,并将其格式设置为ZIP。然后,我们使用`fileSet`元素指定要包含在归档文件中的文件集。在这个例子中,我们将`${project.build.outputDirectory}`目录下的所有JAR文件包含在归档文件中。 这只是一个简单的示例,你可以根据你的需求进行更复杂的配置。希望这个回答对你有帮助!如果你还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值