maven-assembly-plugin 进行打包
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 Descriptor 和 Assembly 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>
该插件的更多属性可见:http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html
该文献转自:https://blog.csdn.net/moonpure/article/details/73480768
maven-assembly-plugin 进行打包的更多相关文章
- 记录一次maven打包时将test目录下的类打包到jar中,Maven Assembly Plugin的使用
今天有人问我打包后找不到主类,运行的类写在test中.按照常规,test目录下的文件不会打包到jar包中.(但是我测试一个springboot工程就可以,这里之后再研究) 具体解决如下 第一步:在po ...
- maven assembly plugin使用
使用场景 在使用maven来管理项目时,项目除了web项目,还有可能为控制台程序,一般用于开发一些后台服务的程序.最近在工作中也遇到了这种场景,使用quartz开发一个任务调度程序.程序中依赖很多ja ...
- 使用Maven Assembly plugin将依赖打包进jar
一个Eclipse的工程,在pom中配置了若干依赖,需要将pom中所有的依赖全部打包进一个jar包中,可以选择的方案有maven-assembly-plugin和fatjar.以前采用fatjar进行 ...
- Maven之多模块打包成一个jar包及assembly
一.多模块打包 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...
- Maven Assembly插件介绍
转自:http://blueram.iteye.com/blog/1684070 已经写得挺好的,就不用重写了. 你是否想要创建一个包含脚本.配置文件以及所有运行时所依赖的元素(jar)Assembl ...
- maven assembly 配置详解
Maven Assembly插件介绍 博客分类: 项目构建 你是否想要创建一个包含脚本.配置文件以及所有运行时所依赖的元素(jar)Assembly插件能帮你构建一个完整的发布包. Assembl ...
- java工程打成jar包 - 使用maven assembly插件打包及手动打包
在java工程打包的过程中遇到过不少问题,现在总结一下.一种是典型的maven工程打包,依赖的jar包全都在pom.xml中指定,这种方式打包很方便:另一种是依赖了本机jar包(不能通过pom.xml ...
- Maven Assembly打包提示[WARNING] transitive dependencies if any will not be available
maven assembly打包出现错误 [WARNING] The POM for com.flink.xxr:0.0.1-SNAPSHOT is invalid, transitive depen ...
- [Apache Maven Shade Plugin] [example] [001] 官方例子:includes-excludes
链接地址:[Selecting Contents for Uber JAR](http://maven.apache.org/plugins/maven-shade-plugin/examples/i ...
- 施用 maven shade plugin 解决 jar 或类的多版本冲突
施用 maven shade plugin 解决 jar 或类的多版本冲突 使用 maven shade plugin 解决 jar 或类的多版本冲突java 应用经常会碰到的依赖的三方库出现版本 ...
随机推荐
- python logging模块使用总结
目录 logging模块 日志级别 logging.basicConfig()函数中的具体参数含义 format参数用到的格式化信息 使用logging打印日志到标准输出 使用logging.base ...
- Codeforces 758C:Unfair Poll(思维+模拟)
http://codeforces.com/problemset/problem/758/C 题意:教室里有n列m排,老师上课点名从第一列第一排开始往后点,直到点到第一列第m排,就从第二列第一排开始点 ...
- echo-nginx-module的安装、配置、使用
一.下载压缩包 [root@www nginx-1.16.0]# wget https://github.com/openresty/echo-nginx-module/archive/v0.61.t ...
- Windows下通过VNC远程访问Linux服务器,并实现可视化
前言 最近因部门需要,老大想让我在公司Linux服务器上弄个Oracle,以用作部门测试环境的数据库服务器,经过一番折腾后,成功完成了任务.因公司Linux服务器是无图形界面的,本人接触Linux不多 ...
- 用Supervisor实现进程守护,在异常退出时自动重启
程序启动后,有些是以daemon的形式运行,但在意外退出后,如果不能及时重新启动,会有比较严重的影响. 比如Zimg在图片处理中由于某些图片处理失败,会导致zimg进程挂掉,影响正常的服务提供,并且只 ...
- U盘被写保护大全解
相信大家的U盘在使用的过程中多或少都有出现过一些问题,写保护,程序写蹦而造成的逻辑错误,或者在使用过程中因电脑而中毒,内部零件损伤等等各种各样倒霉的错误. 简单了解一下是个什么东西吧.U盘写保护其实就 ...
- Bzoj 1997 [Hnoi2010]Planar题解
1997: [Hnoi2010]Planar Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 2224 Solved: 824[Submit][Stat ...
- Cookie起源与发展
上一篇我们在讲优酷弹幕爬虫的时候,引入了一个新的知识点:Cookie,由于篇幅有限当时只是简单的给大家介绍了一下它的作用,今天我们就来全面了解一下Cookie(小饼干)以及相关的知识! 相信很多同学肯 ...
- 使用gulp构建微信小程序工作流
前言 刚入门微信小程序的时候,一切都基于微信web开发者工具,没有使用其他框架,也没有工程化的概念.当时做的项目都比较简单,单单用微信web开发者工具倒也得心应手.学了些东西后,就按捺不住地想跳出原生 ...
- HDU5521 Meeting(dijkstra+巧妙建图)
HDU5521 Meeting 题意: 给你n个点,它们组成了m个团,第i个团内有si个点,且每个团内的点互相之间距离为ti,问如果同时从点1和点n出发,最短耗时多少相遇 很明显题目给出的是个无负环的 ...