---
title: "初学maven(5)-使用assembly plugin实现自定义打包"
author: "skydream"
date: "2009-01-16T18:22:00Z"
tags: ["Ant", "Apache", "Eclipse", "XML", "maven"]
slug: "maven-5-assembly-plugin"
draft: false
lastmod: "2009-08-02T11:13:31Z"
id: 437937
url: "https://skydream.iteye.com/blog/437937"
---

在上一篇文章中，讨论到在对maven的机制不熟悉的情况下，为了实现自己需要的打包格式而使用maven ant task以maven + ant的方式来实现非标准打包，而现在要介绍的是maven中针对打包任务而提供的标准插件：assembly plugin。

    依然以上文（初学maven(4)-使用maven ant task实现非标准打包）的项目为例，要打包的程序如下：

    demo1  
    |\_\_\_\_lib  
    |\_\_\_\_\_demo1.jar  
    |\_\_\_\_\_\*\*\*\*\*.jar  
    |\_\_\_\_\_\*\*\*\*\*.jar  
    |\_\_\_\_config  
    |\_\_\_\_\_\*\*\*\*\*.properties  
    |\_\_\_\_\_\*\*\*\*\*.xml  
    |\_\_\_\_log  
    |\_\_\_\_\_\*\*\*\*\*.log  
    |\_\_\_\_run.bat  
    |\_\_\_\_run.sh

    类似的建立java项目,文件结构如下：

    demo1  
    |\_\_\_\_src/main/java  
    |\_\_\_\_src/main/config  
    |\_\_\_\_src/main/bin  
    |\_\_\_\_src/main/resources  
    |\_\_\_\_src/main/assemble  
    |\_\_\_\_src/test/java  
    |\_\_\_\_src/test/resources  
    |\_\_\_\_target  
    |\_\_\_\_pom.xml

    除开增加了src/main/assemble目录和没有ant的build文件外，其他内容完全一样：其中src/main/java下放java代码；src/main/resources下放一个\*.properties文件，这个资源文件是打包到 jar中，内容打包之后不需要改变的。src/main/config下放一个标准的log4j.xml，这个是有在安装运行前临时修改的需要的。src /main/bin下放置可执行文件。

    assembly plugin的使用方式比较简单，主要有：

1\. 修改pom.xml

    pom.xml中设置如下：

&lt;!--&lt;br /&gt; &lt;br /&gt; Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt; http://www.CodeHighlighter.com/&lt;br /&gt; &lt;br /&gt; --&gt;&lt; build &gt;  
         &lt; plugins &gt;  
             &lt; plugin &gt;  
                 &lt; artifactId &gt; maven-assembly-plugin &lt;/ artifactId &gt;  
                 &lt; configuration &gt;  
                     &lt;!--  not append assembly id in release file name  --&gt;  
                     &lt; appendAssemblyId &gt; false &lt;/ appendAssemblyId &gt;  
                     &lt; descriptors &gt;  
                         &lt; descriptor &gt; src/main/assemble/package.xml &lt;/ descriptor &gt;  
                     &lt;/ descriptors &gt;  
                 &lt;/ configuration &gt;  
                 &lt; executions &gt;  
                     &lt; execution &gt;  
                         &lt; id &gt; make-assembly &lt;/ id &gt;  
                         &lt; phase &gt; package &lt;/ phase &gt;  
                         &lt; goals &gt;  
                            &lt; goal &gt; single &lt;/ goal &gt;  
                         &lt;/ goals &gt;  
                     &lt;/ execution &gt;  
                 &lt;/ executions &gt;  
             &lt;/ plugin &gt;  
         &lt;/ plugins &gt;  
     &lt;/ build &gt;

    其中&lt;artifactId&gt;maven-assembly-plugin&lt;/artifactId&gt;的maven-assembly-plugin是这个插件的标准命名，在maven2.0.\*中带的默认版本是

    appendAssemblyId属性控制是否在生成的打包文件的文件名中包含assembly id。  
     
    descriptor属性指定maven-assembly-plugin的配置文件，当然我设置的是src/main/assemble/package.xml.容许使用多个，功能强大当然用法也复杂，对于简单情况一个足矣。

    execution的设置是为了将maven-assembly-plugin继承到标准的maven打包过程中，这样在运行maven-package时就会执行maven-assembly-plugin的操作，从而实现我们需要的自定义打包。  
2\. assemble descriptor file

    我的src/main/assemble/package.xml内容如下：

&lt;!--&lt;br /&gt; &lt;br /&gt; Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt; http://www.CodeHighlighter.com/&lt;br /&gt; &lt;br /&gt; --&gt;&lt; assembly  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/assembly-1.0.0.xsd" &gt;  
     &lt; id &gt; package &lt;/ id &gt;  
     &lt; formats &gt;  
         &lt; format &gt; zip &lt;/ format &gt;  
     &lt;/ formats &gt;  
     &lt; includeBaseDirectory &gt; true &lt;/ includeBaseDirectory &gt;  
     &lt; fileSets &gt;  
         &lt; fileSet &gt;  
             &lt; directory &gt; src/main/bin &lt;/ directory &gt;  
             &lt; outputDirectory &gt; / &lt;/ outputDirectory &gt;  
         &lt;/ fileSet &gt;  
         &lt; fileSet &gt;  
             &lt; directory &gt; src/main/config &lt;/ directory &gt;  
             &lt; outputDirectory &gt; config &lt;/ outputDirectory &gt;  
         &lt;/ fileSet &gt;  
     &lt;/ fileSets &gt;  
     &lt; dependencySets &gt;  
         &lt; dependencySet &gt;  
             &lt; outputDirectory &gt; lib &lt;/ outputDirectory &gt;  
             &lt; scope &gt; runtime &lt;/ scope &gt;  
         &lt;/ dependencySet &gt;  
     &lt;/ dependencySets &gt;  
&lt;/ assembly &gt;

     
    详细的语法不介绍了，请参考官方指南，有非常详尽的说明：[Assembly Descriptor Format reference](http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html "Assembly Descriptor Format reference")

    简单解释一下：

    1) format  
    format=zip设置打包的最终文件格式为zip.  
    支持的其他格式还有gz,tar,tar.gz,tar.bz2。

    2)  fileset

&lt;!--&lt;br /&gt; &lt;br /&gt; Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt; http://www.CodeHighlighter.com/&lt;br /&gt; &lt;br /&gt; --&gt;     &lt; fileSet &gt;  
             &lt; directory &gt; src/main/bin &lt;/ directory &gt;  
             &lt; outputDirectory &gt; / &lt;/ outputDirectory &gt;  
     &lt;/ fileSet &gt;  

   
    将src/main/bin目录下的文件打包到根目录(/)下.

&lt;!--&lt;br /&gt; &lt;br /&gt; Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt; http://www.CodeHighlighter.com/&lt;br /&gt; &lt;br /&gt; --&gt;&lt; fileSet &gt;  
             &lt; directory &gt; src/main/config &lt;/ directory &gt;  
             &lt; outputDirectory &gt; config &lt;/ outputDirectory &gt;  
&lt;/ fileSet &gt;

    将src/main/config目录下的文件打包到config下.

    3) dependencySets

&lt;!--&lt;br /&gt; &lt;br /&gt; Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt; http://www.CodeHighlighter.com/&lt;br /&gt; &lt;br /&gt; --&gt;     &lt; dependencySet &gt;  
             &lt; outputDirectory &gt; lib &lt;/ outputDirectory &gt;  
             &lt; scope &gt; runtime &lt;/ scope &gt;  
    &lt;/ dependencySet &gt;

    将scope为runtime的依赖包打包到lib目录下。

    总结一下，pom.xml中引入maven-assembly-plugin，然后assemble descriptor file按需设置，最后在eclipse中执行Run As -&gt; Maven package，在target目录下就会出现\*\*\*.zip文件，里面的格式和要求的完全一致。

    够简单明了吧？感觉比使用maven ant task要轻快不少，看来maven还是很强大的，继续学习......