前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >maven-assembly-plugin插件的使用方法

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

作者头像
wuweixiang
发布2018-10-12 10:00:55
13.2K0
发布2018-10-12 10:00:55
举报
文章被收录于专栏:吴伟祥吴伟祥吴伟祥

一. Assembly 是什么意思?

二. maven-assembly-plugin是什么?

1

它是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的内容,我没有深究,不是特别懂,暂时不多说。 

序号

取值

意义

1

compile

缺省值,适用于所有阶段,会随着项目一起发布

2

provided

类似compile,期望JDK、容器或使用者会提供这个依赖。如servlet.jar

3

runtime

只在运行时使用,如JDBC驱动,适用运行和测试阶段

4

test

只在测试时使用,用于编译和运行测试代码。不会随项目发布

5

system

类似provided,需要显式提供包含依赖的jar,Maven不会在Repository中查找它

六. 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>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档