maven plugin之maven-assembly-plugin

一、使用场景
如果项目是微服务架构,可能用到这个插件的概率比较高,平时普通的项目不需要这样的实现方式。
如果项目内的一部分通用功能,不需要挨个引用,则需要将通用功能部分达成jar包。

二、Maven-assembly-plugin作用
1、作用:要想将写的程序和它本身所依赖的jar包一起build到一个包里,是maven中针对打包任务而提供的标准插件。
2、其他作用:
1)提供一个把工程依赖元素、模块、网站文档等其他文件存放到单个归档文件里。
2)打包成指定格式分发包,支持各种主流的格式如zip、tar.gz、jar和war等,具体打包哪些文件是高度可控的。
3)能够自定义包含/排除指定的目录或文件。

三、总体来说,实现插件maven-assembly-plugin需要两个步骤:
第1步骤:pom.xml文件里配置maven-assembly-plugin,指定描述文件
第2步骤:描述文件配置具体参数

对应步骤1 ------> 项目中pom.xml的配置如下:

<?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.study</groupId>
<artifactId>center</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>make-assembly</id>
					<phase>package</phase>
					<goals>
						<goal>single</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>
</project>

单行解释如下:

<plugin>
   <artifactId>maven-assembly-plugin</artifactId> <!--插件引用-->
   <configuration>
      <appendAssemblyId>false</appendAssemblyId>
      <descriptors>  <!--描述文件路径-->
         <descriptor>src/main/assembly/package.xml</descriptor> 
      </descriptors>
   </configuration>
   <executions>
      <execution>
         <id>make-assembly</id>  <!--名字任意 -->
         <phase>package</phase>  <!-- 绑定到package生命周期阶段上 -->
         <goals>
            <goal>single</goal>  <!-- 只运行一次 --> 
         </goals>
      </execution>
   </executions>
</plugin>

对应步骤2 ------> 这样配置后,在项目的对应路径下,找到package.xml文件(描述文件)
在这里插入图片描述

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>package</id>
    <formats>
        <format>dir</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>src/main/resources</directory>
            <outputDirectory>/config</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>src/main/bin</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
            <scope>runtime</scope>
            <excludes>
                <exclude>${groupId}:${artifactId}</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
</assembly>

描述符文件说明:
1、ID
id 标识符,添加到生成文件名称的后缀符。如果指定 id 的话,目标文件则是

${artifactId}-${id}.tar.gz
<id>package</id>

2、formats
maven-assembly-plugin 支持的打包格式有zip、tar、tar.gz (or tgz)、tar.bz2 (or tbz2)、jar、dir、war,可以同时指定多个打包格式

<formats>
    <format>dir</format>
    ...可配置多个
</formats>

3、dependencySets
用来定制工程依赖 jar 包的打包方式,核心元素如下:

元素类型作用
outputDirectoryString指定包依赖目录,该目录是相对于根目录
includes/include*List包含依赖
excludes/exclude*List排除依赖
<dependencySets>
    <dependencySet>
        <outputDirectory>lib</outputDirectory>
        <scope>runtime</scope>
        <excludes>
            <exclude>${groupId}:${artifactId}</exclude>
        </excludes>
    </dependencySet>
</dependencySets>

4、fileSets
管理一组文件的存放位置,核心元素如下:

元素类型作用
outputDirectoryString指定包依赖目录,该目录是相对于根目录
includes/include*List包含依赖
excludes/exclude*List排除依赖
<fileSets>
    <fileSet>
        <directory>src/main/resources</directory>
        <outputDirectory>/config</outputDirectory>	<!--resource文件输出路径-->
    </fileSet>
    <fileSet>
        <directory>src/main/bin</directory>
        <outputDirectory>/</outputDirectory>  <!--bin目录文件输出路径-->
    </fileSet>
    <fileSet>
        <directory>${project.build.directory}</directory> <!--jar文件输出路径-->
        <outputDirectory>/</outputDirectory>
        <includes>
            <include>*.jar</include>
        </includes>
    </fileSet>
</fileSets>

三、找到生成的包
实现了上述两步之后,在项目路径/target目录下,有jar包如下:

打开center-0.0.1-SNAPSHOT文件,输出格式符合描述文件内的配置。如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值