maven-assembly-plugin插件的使用方法

一. Assembly 是什么意思?

在这里插入图片描述

二. maven-assembly-plugin是什么?

它是maven中针对打包任务而提供的标准插件。

三. maven-assembly-plugin插件的作用?

摘自官网:http://maven.apache.org/plugins/maven-assembly-plugin/

英文原文:The Assembly Plugin for Maven is primarily intended to allow users to aggregate the project output along with its dependencies, modules, site documentation, and other files into a single distributable archive.

中文翻译:Assembly 插件的主要作用是,允许用户将项目输出与它的依赖项、模块、站点文档、和其他文件一起组装成一个可分发的归档文件。

(翻译不一定准确,以英文为准)

四.maven-assembly-plugin插件在maven项目中如何使用(即使用步骤)?

1. 需要指定一个Assembly描述符文件。该文件指定了打包格式,包含的文件/过滤的文件等信息,可以同时指定多个描述符文件,打包成不同的格式。

2. 在Maven工程的pom.xml文件里配置maven-assembly-plugin插件,引入Assembly描述符文件。

五. maven项目中Assembly描述符文件详解

示例:

<?xml version="1.0" encoding="utf-8"?>
<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    
    <!-- id 标识符,添加到生成文件名称的后缀符。如果指定 id 的话(这里指定的是项目的版本),目标文件则是 ${artifactId}-${id}.jar。【如terminal-dispatch-5.0.0.0.jar】 -->
    <id>${project.version}</id>
    
    <!-- 指定打包格式。maven-assembly-plugin插件支持的打包格式有zip、tar、tar.gz (or tgz)、tar.bz2 (or tbz2)、jar、dir、war,可以同时指定多个打包格式 -->
    <formats>
        <format>jar</format>
    </formats>
    
    <!-- 指定打的包是否包含打包层目录(比如finalName是terminal-dispatch,当值为true,所有文件被放在包内的terminal-dispatch目录下,否则直接放在包的根目录下)-->
    <includeBaseDirectory>true</includeBaseDirectory>
    
    <!-- 指定将工程依赖的包打到包里的指定目录下 -->
    <dependencySets>
        <dependencySet>
            <useProjectArtifact>true</useProjectArtifact> <!-- 指定打包时是否包含工程自身生成的jar包 -->
            <outputDirectory>lib</outputDirectory> <!-- 指定将这些依赖包打到包里lib目录下 -->
            <scope>runtime</scope> <!-- 用于管理依赖的部署,runtime表示只在运行时使用 -->
        </dependencySet>
    </dependencySets>
    
    <!-- 指定要包含的文件集,可以定义多个fileSet -->
    <fileSets>
        <fileSet>
            <directory>src/main/script/linux/bin</directory> <!-- 指定归档文件(要打的jar包)要包含的目录(下的文件及文件夹) -->
            <outputDirectory>bin</outputDirectory> <!-- 指定要将当前目录(<directory>标签中的目录放在归档文件(要打的jar包)bin目录下) -->
            <includes>
                <include>terminal-dispatch</include> <!-- 精确控制要包含的文件,<exclude>用于精确控制要排除的文件  -->
                <include>server</include>
            </includes>
            <fileMode>0755</fileMode> <!-- 设置文件 UNIX 属性,是一种读写权限 -->
        </fileSet>        
        <fileSet>
            <directory>src/main/resources</directory>
            <outputDirectory>conf</outputDirectory>
            <includes>
                <include>config.properties</include>
                <include>logback.xml</include>
            </includes>
            <fileMode>0644</fileMode>
        </fileSet>
        <fileSet>
            <directory>src/main/script/conf</directory>
            <outputDirectory>conf</outputDirectory>
            <includes>
                <include>wrapper.conf</include>
            </includes>
            <fileMode>0644</fileMode>
        </fileSet>
        <fileSet>
            <directory>src/main/script/linux/lib</directory>
            <outputDirectory>lib</outputDirectory>
            <includes>
                <include>libwrapper.so</include>
                <include>wrapper.jar</include>
            </includes>
            <fileMode>0755</fileMode>
        </fileSet>
    </fileSets>
    
</assembly>
 1.  <includeBaseDirectory>true</includeBaseDirectory>标签作用?

指定打的包是否包含打包层目录,比如finalName是terminal-dispatch,当值为true,所有文件被放在包内的terminal-dispatch目录下,否则直接放在包的根目录下,

如下图所示:
在这里插入图片描述

2. <fileMode>0755</fileMode>标签作用?

设置文件的unix属性,好像是一种读写权限的设定,linux的内容,我没有深究,不是特别懂,暂时不多说。

在这里插入图片描述

六. maven中的pom.xml配置(引入assembly描述符文件)

<build>
    <plugins>
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-assembly-plugin</artifactId>
      <version>2.4</version>
        <executions>
          <execution> <!--执行器 mvn assembly:assembly-->
            <id>make-zip</id> <!--名字任意 -->
            <phase>package</phase> <!-- 绑定到package生命周期阶段上 -->
            <goals>
              <goal>single</goal> <!-- 该打包任务只运行一次 -->
            </goals>
            <configuration>
              <descriptors
                <descriptor>src/main/script/assembly.xml</descriptor> 
              </descriptors>
          </configuration>
        </execution>
      </executions>
    </plugin>
    <plugins>
</build>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值