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 plaincopy
  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 plaincopy
  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 plaincopy
  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. <!--将项目中src/main/resources/script下的内容放入packaeg中-->
  25. <includes>
  26. <include>*.ksh</include>
  27. <include>*.cmd</include>
  28. </includes>
  29. <lineEnding>lf</lineEnding>
  30. </fileSet>
  31. </fileSets>
  32. <dependencySets>
  33. <dependencySet>
  34. <outputDirectory>lib</outputDirectory>
  35. </dependencySet>
  36. </dependencySets>
  37. </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官网中队插件更详细的介绍。

assembly plugin实现自定义打包相关推荐

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

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

  2. 使用Maven Assembly plugin将依赖打包进jar

    一个Eclipse的工程,在pom中配置了若干依赖,需要将pom中所有的依赖全部打包进一个jar包中,可以选择的方案有maven-assembly-plugin和fatjar.以前采用fatjar进行 ...

  3. java assembly 打包_使用Maven的assembly插件实现自定义打包

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

  4. Maven自定义打包插件Assembly Plugin简单使用

    使用Assembly Plugin实现自定义打包 1.修改pom.xml <!-- 编译时自动打包,规则见distribution.xml文件--> <build><pl ...

  5. SpringBoot项目利用maven自定义打包结构

    From: https://blog.csdn.net/q15858187033/article/details/80742117 SpringBoot官方提供的demo中,pom.xml文件里引用了 ...

  6. Gradle实践之自定义打包jar+Log开关自动关闭

    2019独角兽企业重金招聘Python工程师标准>>> 上一篇博客介绍了Gradle实践之多渠道自动化打包+版本号管理.由于我在公司里主要是做SDK开发的,这次我想介绍一下如何使用G ...

  7. android 自定义apk名,Android Studio多渠道打包、自定义打包APK名称

    现在为了推广产品,会在多个渠道应用市场发布应用,为了统计不同渠道的数据,需要在应用中表明渠道,如果一个一个去修改打包效率会很低.AS为我们提供了简便的方法,可以多渠道打包,一次打包所有的渠道包. 1. ...

  8. Maven Assembly Plugin - 如何将Maven工程打成一个可执行jar包

    参考自: http://blog.csdn.net/symgdwyh/article/details/6081532 Maven Assembly Plugin http://maven.apache ...

  9. Android studio 自定义打包apk名

    Android Studio打包应用默认生成的apk名称是:app-release.apk .如果我们要让生成的apk名跟我们版本包名有联系的话,那我们就要自定义生成的apk名了 需要在build.g ...

最新文章

  1. VUE 路径拦截, 开放页面, 基于动态路由, 拦截器
  2. B - 数据结构实验之排序二:交换排序(冒泡和快排)
  3. asp点击链接数字加1代码_Asp.Net Core使用TinyMCE富媒体编辑器
  4. 简单的MFC画正弦曲线
  5. PHP WEB程序设计信息表,PHP WEB程序设计
  6. 【机器学习】朴素贝叶斯介绍及实例--对短信进行二分类 使用多项式分布
  7. 马冬晗学习计划表_一年时间提升学习和工作能力,我做对了这3点
  8. 哈维玛德学院 计算机,哈维玛德学院优势多多,令人神往!
  9. ORACLE自增长字段实现(转)
  10. react-native技术调研:react-native是什么?
  11. IntelliJ IDEA 的项目配置和Web部署,终于搞懂了!
  12. 中小型企业网络配置、基于企业网络方案的设计与实施
  13. 油猴脚本对web项目的影响
  14. 「RPA技术」RPA工具的比较,谁能胜出?
  15. MapGIS K9如何裁剪瓦片数据
  16. 戴尔r410服务器raid装系统,Dell R410 Raid磁盘阵列驱动
  17. 【高级操作系统-陈渝】Architecture-计算机架构
  18. 装修后才知道的79件事
  19. Linux—使用doc2unix进行文件格式转换
  20. XML第四讲:DTD元素、属性深度详解

热门文章

  1. [JSP暑假实训] 四.MyEclipse+Servlet+JSP实现火车票网站查询、修改、删除操作
  2. PHP网站使用JavaScript和Iframe简单实现部分刷新效果
  3. 【数据结构与算法】之深入解析“重新安排行程”的求解思路与算法示例
  4. Spark is not running in local mode, therefore the checkpoint directory must not be on the local……
  5. Python回调函数
  6. Scrapy + Redis 分布式爬取58同城北京全站二手房数据
  7. 信息学奥赛一本通(C++)在线评测系统——基础(一)C++语言——1112:最大值和最小值的差
  8. Java中集合(三)Stack
  9. 【色彩管理】YUV色彩模式详解
  10. 【嵌入式】C语言高级编程-地址对齐(07)