assembly plugin实现自定义打包

http://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中设置如下:

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


    其中<artifactId>maven-assembly-plugin</artifactId>的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内容如下:

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


    
    详细的语法不介绍了,请参考官方指南,有非常详尽的说明:Assembly Descriptor Format reference 

    简单解释一下:

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

    2)  fileset

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

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

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


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

    3) dependencySets

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->      <  dependencySet  > 
              <  outputDirectory  > lib  </  outputDirectory  > 
              <  scope  > runtime  </  scope  > 
     </  dependencySet  >


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


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

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




===========http://blog.csdn.net/onlyqi/article/details/8194357

在IDE中安装了maven插件之后,就可以直接运行maven package来打包了。

如果没有在IDE中安装插件而是单独安装了maven程序,则可以在命令行中直接执行mvn package来打包。

二者没有本质区别,本文以在IDE中直接为例。


要将项目打包,有两种选择:生成一个jar包;或一个包含jar包,配置文件,脚本文件等等的一个zip文件(assembly)。

如果没有在pom中显示的指定,则会使用默认的插件(通过查看effective pom可以看到)并按默认方式打包。


我们常用maven-jar-plugin来生成jar包。如果希望生成assembly,则除了maven-jar-plugin外,再使用插件maven-assembly-plugin来生成zip包。

[html]  view plain copy
  1. <plugin>  
  2.                 <artifactId>maven-jar-plugin</artifactId>  
  3.                 <version>2.3.1</version>  
  4.                 <executions>  
  5.                     <execution>  
  6.                         <id>default-jar</id>  
  7.                         <phase>package</phase>  
  8.                         <goals>  
  9.                             <goal>jar</goal>  
  10.                         </goals>  
  11.                         <configuration>  
  12.                             <archive>  
  13.                                 <manifest>  
  14.                                     <addClasspath>true</addClasspath>  
  15.                                     <mainClass>com.thomsonreuters.PALFullExtractor.ExtractorMain</mainClass>  
  16.                                 </manifest>  
  17.                             </archive>  
  18.                         </configuration>  
  19.                     </execution>  
  20.                 </executions>  
  21.             </plugin>  
在上例中,指定mainClass将在manifest文件中加入mainclass,这样jar就可以直接运行了。

[html]  view plain copy
  1. <plugin>  
  2.                 <artifactId>maven-assembly-plugin</artifactId>  
  3.                 <version>2.2</version>  
  4.                 <executions>  
  5.                     <execution>  
  6.                         <id>make-assembly</id>  
  7.                         <phase>package</phase>  
  8.                         <goals>  
  9.                             <goal>single</goal>  
  10.                         </goals>  
  11.                         <configuration>  
  12.                             <archive>  
  13.                                 <manifest>  
  14.                                     <mainClass>com.thomsonreuters.PALFullExtractor.ExtractorMain</mainClass>  
  15.                                 </manifest>  
  16.                             </archive>  
  17.                             <descriptorRefs>  
  18.                                 <descriptorRef>jar-with-dependencies</descriptorRef>  
  19.                             </descriptorRefs>  
  20.                             <descriptors>  
  21.                                 <descriptor>D:\projectname\src\main\assembly/assembly.xml</descriptor>  
  22.                             </descriptors>  
  23.                         </configuration>  
  24.                     </execution>  
  25.                 </executions>  
  26.             </plugin>  

注意其中2点:

1,descriptorRef -- jar-with-dependencies 就是将程序依赖的所有第三方类库都打入jar包,这样就不需要在运行时指定class-path了。

2,descriptor -- 指定assembly文件所在的位置。我们还需要创建一个assembly.xml来详细说明package的目录结构和内容。

assembly.xml文件的示例:

[html]  view plain copy
  1. <assembly>  
  2.     <id>package</id>  
  3.     <formats>  
  4.         <format>zip</format>  
  5.     </formats>  
  6.     <includeBaseDirectory>false</includeBaseDirectory>  
  7.     <fileSets>  
  8.         <fileSet>  
  9.             <directory>src/main/resources/config</directory>  
  10.             <outputDirectory>config</outputDirectory>   
  11. <!--将项目中src/main/resources/config下的内容放入packaeg的第一级config目录中-->  
  12.             <includes>  
  13.                 <include>*.xsd</include>  
  14.                 <include>*.dtd</include>  
  15.                 <include>*.xml</include>  
  16.                 <include>*.properties</include>  
  17.                 <include>*.key</include>  
  18.             </includes>  
  19.             <lineEnding>lf</lineEnding>  
  20.         </fileSet>  
  21.         <fileSet>  
  22.             <directory>src/main/resources/script</directory>  
  23.             <outputDirectory></outputDirectory>  
  24.   
  25. <!--将项目中src/main/resources/script下的内容放入packaeg中-->  
  26.   
  27.             <includes>   
  28.                 <include>*.ksh</include>   
  29.                 <include>*.cmd</include>   
  30.             </includes>   
  31.             <lineEnding>lf</lineEnding>   
  32.         </fileSet>   
  33.     </fileSets>  
  34.     <dependencySets>   
  35.         <dependencySet>  
  36.             <outputDirectory>lib</outputDirectory>  
  37.         </dependencySet>   
  38.     </dependencySets>  
  39. </assembly>  

这样打好的package名叫例如:project-1_0.zip中解压后就会得到脚本文件以及另外两个目录config(包含配置文件),和lib目录(包含所有依赖的第三方类库和projectName.jar)

我们还可以在assembly按需要构建更复杂的package结构。

从maven-jar-plugin和maven-assembly-plugin的pom文件可以看到它们都指定了:<phase>package</phase>

这会导致运行mvn package命令时运行这两个插件。

直接搜索这两个插件的名字可以找到maven官网中队插件更详细的介绍。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值