转自http://liugang594.iteye.com/blog/2093607

maven-assembly-plugin使用描述(拷自 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.

目前它只有一个有意义的goal, 详细的请看(http://maven.apache.org/plugins/maven-assembly-plugin/plugin-info.html):

assembly:single

single操作有很多可配置的参数,详细的请看(http://maven.apache.org/plugins/maven-assembly-plugin/single-mojo.html)。

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

目前至少支持以下打包类型:

  • zip
  • tar
  • tar.gz
  • tar.bz2
  • jar
  • dir
  • war

默认情况下,打jar包时,只有在类路径上的文件资源会被打包到jar中,并且文件名是${artifactId}-${version}.jar,下面看看怎么用maven-assembly-plugin插件来定制化打包。

首先需要添加插件声明:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

使用内置的Assembly Descriptor

要使用maven-assembly-plugin,需要指定至少一个要使用的assembly descriptor 文件,对于当前使用的版本(2.4)对应的assembly descriptor的schema定义为:Assembly Schema ,其中assembly descriptor中又可以包括 component 的定义 (component 可以很方便的用于多个assembly descriptor之间共享),component 的schema 定义在:Component Schema 。 关于assembly descriptor的component descriptor的更详细的说明,请见:Component DescriptorAssembly Descriptor

默认情况下,maven-assembly-plugin内置了几个可以用的assembly descriptor:

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

要查看它们的详细定义,可以到maven-assembly-plugin-2.4.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>

自定义Assembly Descriptor

一般来说,内置的assembly descriptor都不满足需求,这个时候就需要写自己的assembly descriptor的实现了。先从一个最简单的定义开始:

<?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:指定当前要包含的目录的目的地

    要使用这个assembly descriptor,需要如下配置:
<configuration>
<finalName>demo</finalName>
<descriptors>
<descriptor>assemblies/demo.xml</descriptor>
</descriptors>
<outputDirectory>output</outputDirectory>
</configuration>

最后会生成一个demo-demo.jar 文件在目录 output 下,其中前一个demo来自finalName,后一个demo来自assembly descriptor中的id,其中的内容和默认的打包出来的jar类似。

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

<appendAssemblyId>false</appendAssemblyId>

添加文件

上面演示了添加所有编译后的资源,同样的可以增加其他资源,例如想添加当前工程目录下的某个文件 b.txt ,在assembly descriptor的assembly结点下增加

<files>
<file>
<source>b.txt</source>
<outputDirectory>/</outputDirectory>
</file>
</files>

这里用到了 files 元素类型,可以想象 fileSets 下的结点都是针对文件夹的;files 下的结点都是针对文件的。

也可以改变打包后的文件名,例如上面的 b.txt ,希望打包后的名字为 b.txt.bak, 只需要在file 里添加以下配置 :

<destName>b.txt.bak</destName>

排除文件

在fileSet里可以使用includes 和 excludes来更精确的控制哪些文件要添加,哪些文件要排除。

例如要排除某个目录下所有的txt文件:

<fileSet>
<directory>${project.build.directory}/classes</directory>
<outputDirectory>/</outputDirectory>
<excludes>
<exclude>**/*.txt</exclude>
</excludes>
</fileSet>

或者某个目录下只想 .class 文件:

<fileSet>
<directory>${project.build.directory}/classes</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>**/*.class</include>
</includes>
</fileSet>

添加依赖

如果想把一些依赖库打到包里,可以用 dependencySets 元素,例如最简单的,把当前工程的所有依赖都添加到包里:

<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
</dependencySet>
</dependencySets>

在assembly下添加以上配置,则当前工程的依赖和工程本身生成的jar都会被打包进来。

如果要排除工程自身生成的jar,则可以添加

<useProjectArtifact>false</useProjectArtifact>

unpack参数可以控制依赖包是否在打包进来时是否解开,例如解开所有包,添加以下配置:

<unpack>true</unpack>

和 fileSet 一样,可以使用 excludes 和 includes 来更详细的控制哪些依赖需要打包进来;另外 useProjectAttachments,useTransitiveDependencies,useTransitiveFiltering等参数可以对间接依赖、传递依赖进行控制。

其他选项

  • moduleSets:当有子模块时候用
  • repositories:想包含库的时候用
  • containerDescriptorHandlers:可以进行一些合并,定义ArtifactHandler之类的时候可以用,(可以参考:说明
  • componentDescriptors:如上所述,可以包含一些componentDescriptor定义,这些定义可以被多个assembly共享

Assembly Plugin更多配置

上面已经看到了一些Assembly Plugin本身的配置,例如 finalName, outputDirectory, appendAssemblyId和descriptors等,除了这些还有其他的一些可配置参数,参见:single,其中某些参数会覆盖在assembly descriptor中的参数。有一个比较有用的参数是: archive,它的详细配置在:archive

下面介绍一些archive的用法。

指定Main-Class

archive的一个重要用处就是配置生成的MANIFEST.MF文件。默认会生成一个MANIFEST.MF文件,不过这个文件默认值没什么意义。如果想指定生成jar的Main-Class,可以如下配置:

<archive>
<manifest>
<mainClass>demo.DemoMain</mainClass>
</manifest>
</archive>

添加MANIFEST项

除了可以指定Main-Class外,还可以添加任意项。比如在OSGI bundle的MANIFEST.MF定义里就有很多用来定义bundle的属性的项,如Import-Package,Export-Package等等。要添加项,可以使用如下配置:

<archive>
<manifestEntries>
<Import-Package>javax.xml.ws.*</Import-Package>
</manifestEntries>
</archive>

指定MANIFEST.MF文件

还可以直接指定MANIFEST.MF文件。如下:

<archive>
<manifestFile>META-INF/MANIFEST.MF</manifestFile>
</archive>

【Maven学习】maven-assembly-plugin的使用的更多相关文章

  1. maven 学习---Maven安装配置

    想要安装 Apache Maven 在Windows 系统上, 只需要下载 Maven 的 zip 文件,并将其解压到你想安装的目录,并配置 Windows 环境变量. 所需工具 : JDK 1.8 ...

  2. maven 学习---Maven添加远程仓库

    默认情况下,Maven从Maven中央仓库下载所有依赖关系.但是,有些库丢失在中央存储库,只有在Java.net或JBoss的储存库远程仓库中能找到. 1. Java.net资源库 添加Java.ne ...

  3. maven 学习---Maven中央存储库

    当你建立一个 Maven 的项目,Maven 会检查你的 pom.xml 文件,以确定哪些依赖下载. 首先,Maven 将从本地资源库获得 Maven 的本地资源库依赖资源, 如果没有找到,然后把它会 ...

  4. maven 学习---Maven教程

    Apache Maven是一个软件项目管理和综合工具.基于项目对象模型(POM)的概念,Maven可以从一个中心资料片管理项目构建,报告和文件. 本教程将介绍如何使用Maven在Java开发,或任何其 ...

  5. Maven学习之 插件plugin

    Maven本质上是一个执行插件的框架.插件共分两类:build插件和reporting插件. build插件,会在build阶段被执行,应该配置在POM的<build/>元素中. repo ...

  6. maven 学习---Maven Web应用

    本教程将教你如何管理使用Maven版本控制系统管理一个基于Web项目.在这里,将学习如何创建/构建/部署和运行Web应用程序: 创建Web应用程序 要创建一个简单的java web应用程序,我们将使用 ...

  7. maven 学习---Maven 编译打包时如何忽略测试用例

    本文地址:http://blog.csdn.net/wirelessqa/article/details/14057305 跳过测试阶段: mvn package -DskipTests 临时性跳过测 ...

  8. maven 学习---Maven 构建配置文件

    什么是构建配置文件? 构建配置文件是一组配置的集合,用来设置或者覆盖 Maven 构建的默认配置.使用构建配置文件,可以为不同的环境定制构建过程,例如 Producation 和 Developmen ...

  9. maven 学习---Maven 插件

    什么是 Maven 插件? Maven 实际上是一个依赖插件执行的框架,每个任务实际上是由插件完成.Maven 插件通常被用来: 创建 jar 文件 创建 war 文件 编译代码文件 代码单元测试 创 ...

  10. maven 学习---Maven构建生命周期

    构建生命周期是一组阶段的序列(sequence of phases),每个阶段定义了目标被执行的顺序.这里的阶段是生命周期的一部分. 举例说明,一个典型的 Maven 构建生命周期是由以下几个阶段的序 ...

随机推荐

  1. 移动lob类型索引到指定表空间

    WWWNEWSAD 为表名 USERS 为原表空间 CONTENT 为lob类型的字段 DATA_INDEX_WXZJ 指定的表空间 ALTER TABLE WWWNEWSAD MOVE TABLES ...

  2. redis cluster 使用中出现的问题

    问题一 redis.clients.jedis.exceptions.JedisClusterMaxRedirectionsException: Too many Cluster redirectio ...

  3. B-spline Curves 学习之B样条基函数计算实例(3)

    B-spline Basis Functions: Computation Examples 本博客转自前人的博客的翻译版本,前几章节是原来博主的翻译内容,但是后续章节博主不在提供翻译,后续章节我在完 ...

  4. 测试嵌入GeoGebra网页

    使用 http://ggbstudy.top/tools/ggb2html/ 将GGB文件免费托管,然后在博客内容中点击“HTML”按钮插入GGB网页地址: <iframe src=" ...

  5. StringFormate使用

    1常规类型的格式化 1.1显示不同转换符实现不同数据类型到字符串的转换 转换符   说明 示例 %s     字符串类型 “mingrisof” %c     字符类型 'm' %b 布尔类型 tru ...

  6. LR中的迭代次数设置

    在参数化时,对于一次压力测试中均只能用一次的资源应该怎么参数化呢?就是说这些资源用了一次就不能在用了的. --参数化时,在select  next row选择unique,update value o ...

  7. 一起学习《C#高级编程》2--比较对象的相等性

    今后争取每两天能更新一次.平日的诱惑太多,双休只顾玩了,进度有点慢. 接上一讲的,类型的安全性,留下了点小尾巴——比较对象的相等性. C#有四种比较相等的方式:除了“==”运算符外,System.Ob ...

  8. [uwp]自定义Behavior之随意拖动

    由于最近有需求,所以自定义了一个随意拖动元素的Behavior. 当然在使用这个自定义的Behavior时,有个小假设:拖动元素必须是Canvas容器的子元素. 实现原理比较简单低效: 监听被拖动元素 ...

  9. BASE64编码乱码问题的浅层分析与解释

    本文由作者朱臻授权网易云社区发布. 1问题案例 曾在开发过程中,我们遇到了BASE64编码乱码的问题,该问题的场景如下: 当web前端,将带有中文字符的字符串base64编码后,传到后端.当后端将数据 ...

  10. django系列3.4-- request对象和response对象(未完待续)

    一.request对象 详细信息可以查阅django官方文档 共有五种请求相关的常用值 request.path_info 返回用户访问的url不包括域名 request.method 请求中使用的H ...