pom文件中maven-assembly-plugin

1、maven plugin介绍

使用Maven对Web项目进行打包,默认为war包;但有些时候,总是希望打成zip包(亦或其他压缩包),这时就用到了maven-assembly-plugin插件

官方网址:
http://maven.apache.org/plugins/maven-assembly-plugin/

1、作用:要想将写的程序和它本身所依赖的jar包一起build到一个包里,是maven中针对打包任务而提供的标准插件。

2、其他作用:

1)提供一个把工程依赖元素、模块、网站文档等其他文件存放到单个归档文件里。

2)打包成指定格式分发包,支持各种主流的格式如zip、tar.gz、jar和war等,具体打包哪些文件是高度可控的。

3)能够自定义包含/排除指定的目录或文件。

2、实现

1、pom.xml文件里配置maven-assembly-plugin,指定描述文件

<?xml version="1.0" encoding="UTF-8"?>
<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.xiaoxu</groupId>
	<artifactId>test-maven-plugin</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
 
	<build>
		<plugins>
			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<configuration>
					<appendAssemblyId>false</appendAssemblyId>
					<!--描述文件路径-->
					<descriptors>
						<descriptor>src/main/assembly/package.xml</descriptor>
					</descriptors>
				</configuration>
				<executions>
					<execution>
					    <!--名字任意 -->
						<id>test-assembly</id>
						<!-- 绑定到package生命周期阶段上 -->
						<phase>package</phase>
						<goals>
							<!-- 只运行一次 --> 
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

2、描述文件配置具体参数

package.xml文件(描述文件)内容如下

<?xml version="1.0" encoding="UTF-8"?>
<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 标识符,添加到生成文件名称的后缀符。如果指定 id 的话,目标文件则是 ${artifactId}-${id}.tar.gz 
  	*/     
    <id>package</id>
    <formats>
    	/**
    	maven-assembly-plugin 支持的打包格式有zip、tar、tar.gz (or tgz)、tar.bz2 (or tbz2)、jar、dir、war,可以同时指定多个打包格式
    	*/
        <format>dir</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <fileSets>
    	/**
    	fileSet 管理一组文件存放的位置
    	*/
        <fileSet>
            /**
            指定文件的目录
            */
            <directory>src/main/resources</directory>
            /**
            resources 目录输出路径
            */
            <outputDirectory>/config</outputDirectory>
        </fileSet>
        <fileSet>
        	/**
            指定文件的目录
            */
            <directory>src/main/bin</directory>
            /**
            bin 目录输出路径
            */
            <outputDirectory>/</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}</directory>
            /**
            jar文件输出路径
            */
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
    </fileSets>
    <dependencySets>
        /**
        dependencySet 管理依赖存放位置
        */ 
        <dependencySet>
        	/**
        	依赖输出目录
        	*/
            <outputDirectory>lib</outputDirectory>
            <scope>runtime</scope>
            /**
            排除依赖
            */
            <excludes>
                <exclude>${groupId}:${artifactId}</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
</assembly>

3、生成包,然后查看生成的包

在这里插入图片描述
项目打包后,查看生成的包

在这里插入图片描述

可以看到

1、我们将项目打包成dir
2、将resources下的文件打包到了 config下
3、将项目依赖打包到了 lib 下

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

白鸽呀

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

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

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

打赏作者

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

抵扣说明:

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

余额充值