maven-assembly可以通过dependencySets将依赖的jar包打到特定目录。

1. 简介

简单的说,maven-assembly-plugin 就是用来帮助打包用的,比如说打出一个什么类型的包,包里包括哪些内容等等。

2. 常见的maven插件

maven插件是在生命周期中某些阶段执行的任务。一个插件完成一项功能。以下介绍几种常见的插件。
如对于打包来说,有多种插件选择。最常见的有以下3个:

plugin function
maven-jar-plugin maven 默认打包插件,用来创建 project jar
maven-shade-plugin 用来打可执行包,executable(fat) jar
maven-assembly-plugin 支持定制化打包方式,例如 apache 项目的打包方式

以下选取几个常见插件介绍。

maven-compiler-plugin

编译Java源码,一般只需设置编译的jdk版本

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

或者在properties设置jdk版本

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

maven-jar-plugin

打成jar时,设定manifest的参数,比如指定运行的Main class,还有依赖的jar包,加入classpath中

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>/data/lib</classpathPrefix>
<mainClass>com.zhang.spring.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

tomcat7-maven-plugin

用于远程部署Java Web项目

<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://59.110.162.178:8080/manager/text</url>
<username>linjinbin</username>
<password>linjinbin</password>
</configuration>
</plugin>

maven-shade-plugin

用于把多个jar包,打成1个jar包
一般Java项目都会依赖其他第三方jar包,最终打包时,希望把其他jar包包含在一个jar包里。
与assembly类似,使用assembly即可。以下详解assembly。

3. maven-assembly-plugin

1.在pom中引入插件

1.首先我们需要在pom.xml中配置maven的assembly插件

 <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions> <execution><!-- 配置执行器 -->
<id>make-assembly</id>
<phase>package</phase><!-- 绑定到package生命周期阶段上 -->
(1)
<goals>
<goal>single</goal><!-- 只运行一次 -->
</goals> (2)
<configuration>
<finalName>${project.name}</finalName>
<!--主类入口等-->
... <descriptor>src/main/assembly/assembly.xml</descriptor><!--配置描述文件路径-->
</configuration> </execution>
</executions>
</plugin>
</plugins>
</build>

2.两个主要的参数设置(上述中的(1)(2))

I) Assembly插件的goals

  • single
  • help

可以执行如下命令完成动作:

mvn assembly:single

或者是,绑定到package生命周期阶段上(见上配置)触发。后续可以直接执行:

mvn package

这也是最常见的Assembly插件配置方式。

II)Assembly descriptor

Assembly Descriptor可以使用内置的,或者定制的。

(1) 使用内置的Assembly Descriptor

要使用maven-assembly-plugin,需要指定至少一个要使用的assembly descriptor 文件。默认情况下,maven-assembly-plugin内置了几个可以用的assembly descriptor:

  • bin : 类似于默认打包,会将bin目录下的文件打到包中;
  • jar-with-dependencies : 会将所有依赖都解压打包到生成物中;
  • src :只将源码目录下的文件打包;
  • project : 将整个project资源打包。

使用 descriptorRefs来引用(官方提供的定制化打包方式)【不建议使用】

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration> <descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>

上述直接配置jar-with-dependencies打包方式。不需要引入额外文件。实际上,上述4中预定义的assembly descriptor有对应的xml。要查看它们的详细定义,可以到maven-assembly-plugin.jar里去看,例如对应 bin 的assembly descriptor 原始文件如下:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>bin</id>
<formats>
<format>tar.gz</format>
<format>tar.bz2</format>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.basedir}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>README*</include>
<include>LICENSE*</include>
<include>NOTICE*</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}/site</directory>
<outputDirectory>docs</outputDirectory>
</fileSet>
</fileSets>
</assembly>

(2) 自定义Assembly Descriptor

一般来说,内置的assembly descriptor都不满足需求,这个时候就需要写自己的assembly descriptor的实现了。
使用 descriptors,指定打包文件 src/assembly/assembly.xml,即在配置文件内指定打包操作要使用这个自定义assembly descriptor(自定义的xml中配置),需要如下配置,即要引入描述文件:

<configuration>
<finalName>demo</finalName>
<descriptors>
<!--描述文件路径-->
<descriptor>src/assembly/assembly.xml</descriptor>
</descriptors>
<outputDirectory>output</outputDirectory>
</configuration>

示例:
src/assembly/assembly.xml:

<?xml version='1.0' encoding='UTF-8'?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0
http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>demo</id> <formats>
<format>jar</format>
</formats> <includeBaseDirectory>false</includeBaseDirectory> <fileSets>
<fileSet> <directory>${project.build.directory}/classes</directory> <outputDirectory>/</outputDirectory> </fileSet>
</fileSets>
</assembly>

这个定义很简单:

  • format:指定打包类型;
  • includeBaseDirectory:指定是否包含打包层目录(比如finalName是output,当值为true,所有文件被放在output目录下,否则直接放在包的根目录下);
  • fileSets:指定要包含的文件集,可以定义多个fileSet;
  • directory:指定要包含的目录;
  • outputDirectory:指定当前要包含的目录的目的地。

回到pom的配置中,自定义的configuration配置后,将会生成一个demo-demo.jar 文件在目录 output 下,其中前一个demo来自finalName,后一个demo来自assembly descriptor中的id,其中的内容和默认的打包出来的jar类似。

如果只想有finalName,则增加配置:

<appendAssemblyId>false</appendAssemblyId>

对于描述文件的元素,即assembly.xml中的配置节点的详细配置,在此稍作总结,见下。

assembly.xml节点配置

在配置assembly.xml之前,我们先看一下pom中引入插件的结构。

<project>
[...]
<build>
[...]
<plugins>
<plugin>
------------(1)坐标-----------
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
------------(2)入口-----------
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.***.startup.BootStrap</mainClass> <!-- 你的主类名 -->
</manifest>
</archive>
------------(3)描述-----------
<descriptors>
<descriptor>src/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
[...]
</project>

archive说明
上面的mainClass标签中的内容替换成自己的main函数所在的类,前面要包含package名字,也就是package_name.MainClassName

assembly.xml文件的主要结构如下。

id

<id>distribution</id>

id 标识符,添加到生成文件名称的后缀符。如果指定 id 的话,目标文件则是 ${artifactId}-${id}.tar.gz

formats

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

<formats>
<format>dir</format>
</formats>

dependencySets

用来定制工程依赖 jar 包的打包方式,核心元素如下表所示。

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

fileSets

管理一组文件的存放位置,核心元素如下表所示。

元素 类型 作用
outputDirectory String 指定文件集合的输出目录,该目录是相对于根目录
includes List<String> 包含文件
excludes List<String> 排除文件
fileMode String 指定文件属性,使用八进制表达,分别为(User)(Group)(Other)所属属性,默认为 0644
    <fileSets>
<fileSet>
<directory>shell</directory>
<outputDirectory>/shell</outputDirectory>
</fileSet>
<fileSet>
<directory>cluster_config</directory>
<outputDirectory>/cluster_config</outputDirectory>
</fileSet>
</fileSets>

其他

其他的,如files节点基本类似fileSets。不常用的暂不介绍。

至此,即可按打包成功。下边给出具体示例。

maven--插件篇(assembly插件)的更多相关文章

  1. 使用Maven的assembly插件实现自定义打包

    一.背景 最近我们项目越来越多了,然后我就在想如何才能把基础服务的打包方式统一起来,并且可以实现按照我们的要求来生成,通过研究,我们通过使用maven的assembly插件完美的实现了该需求,爽爆了有 ...

  2. Maven的assembly插件实现自定义打包部署(包含依赖jar包)

    微服务必备 优点: 1.可以直接导入依赖jar包 2.可以添加插件启动 .sh 文件 3.插件的配置以及微服务的统一打包方式 1.首先我们需要在pom.xml中配置maven的assembly插件 & ...

  3. Maven Assembly插件介绍

    转自:http://blueram.iteye.com/blog/1684070 已经写得挺好的,就不用重写了. 你是否想要创建一个包含脚本.配置文件以及所有运行时所依赖的元素(jar)Assembl ...

  4. java工程打成jar包 - 使用maven assembly插件打包及手动打包

    在java工程打包的过程中遇到过不少问题,现在总结一下.一种是典型的maven工程打包,依赖的jar包全都在pom.xml中指定,这种方式打包很方便:另一种是依赖了本机jar包(不能通过pom.xml ...

  5. CloudNotes之桌面客户端篇:插件系统的实现

    [CloudNotes版本更新历史与各版本下载地址请点击此处] [CloudNotes中文系列文章汇总列表请点击此处] [查看CloudNotes源代码请点击此处] 有时候,同一个名词,针对不同的人群 ...

  6. 使用mybatis assembly插件打成tar包,在linux系统中运行服务

    使用mybatis assembly插件打成tar包,在linux系统中运行服务 assembly插件插件地址: 链接:https://pan.baidu.com/s/1i6bWPxF 密码:gad5 ...

  7. maven(一)maven自带的插件

    关于org.apache.maven.plugins 前言 maven提供了很多插件给我们使用,解释3个java环境常用的maven插件, maven-jar-plugin, maven-compli ...

  8. 欲善事先利器-IEAD插件篇

    工欲善其事,必先利其器,好鞋踢好球是非常合乎逻辑的事情. --<长江七号> 同样的开场白,不一样的酒,不一样的故事. 上篇<欲善事先利器--系统篇>已经推荐了一些个人常用的效率 ...

  9. Maven 依赖调解源码解析(二):如何调试 Maven 源码和插件源码

    本文是系列文章<Maven 源码解析:依赖调解是如何实现的?>第二篇,主要介绍如何调试 Maven 源码和插件源码.系列文章总目录参见:https://www.cnblogs.com/xi ...

随机推荐

  1. [py]使用字典get方法做数据统计

    s = "aabbccc" d = {} for i in s: if i in d: d[i] += 1 else: d[i] = 0 for i in s: d[i] = d. ...

  2. python将对象名的字符串类型,转化为相应对象的操作方法

    在实际使用Python的过程中,遇到了一个问题,就是定义一个数组,数组内容为对应类名字的字符串. 此时在调用对应的类,生成实例时,需要将字符串转化为相应的类,之后再进行实例化. # coding : ...

  3. puppeteer(一)环境搭建——新Web自动化工具(同selenium)

    一.简介 https://github.com/GoogleChrome/puppeteer Puppeteer是一个Node库,它提供了一个高级API来控制DevTools协议上的 Chrome或C ...

  4. CentOS 7 keepalived+LVS

    LVS架构中 , 不管是NAT模式还是DR模式 , 当后端的RS宕机了 , 调度器还是会把请求转发到宕掉的RS上 , 然而keepalived可以解决该问题 , 它不仅仅有高可用的功能 , 还有负载均 ...

  5. latex 参考文献

    https://blog.csdn.net/garfielder007/article/details/51628565 https://www.cnblogs.com/BUAAdaozhong/p/ ...

  6. 在 python3.x中安装 Crypto 库

    1.安装:直接找过来 whl 安装:链接: https://pan.baidu.com/s/1zXjzchnqc1GgSWT9TjHDaA 提取码: dzbn 复制这段内容后打开百度网盘手机App,操 ...

  7. C#异步编程基础入门总结

    1.前言 *.NET Framework提供了执行异步操作的三种模式: 异步编程模型(APM)模式(也称为IAsyncResult的模式),其中异步操作要求Begin和End方法(例如,BeginWr ...

  8. MVC开发模式的数据运行流程

    对于java中经典的开发模式MVC,有一些感触!现说一下Java中数据的运行流程,由于我技术有限,有错的话欢迎提出,不喜勿喷! 我们知道在MVC开发模式,包括三部分视图层V(view).控制层C(Co ...

  9. Eclipse中XML文件自定义格式化配置

    1,编码格式:UTF-8 2,Line Width:90,Indent using spaces:2 3,默认编辑器 当添加Spket插件后,xml文件默认编辑器将被修改为Spket,要求恢复默认,则 ...

  10. wrap

    import 'package:flutter/material.dart'; void main() { runApp(MaterialApp(home: new MyApp())); } clas ...