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 | Assemble an application bundle or distribution from an assembly descriptor. This goal is suitable either for binding to the lifecycle or calling directly from the command line (provided all required files are available before the build starts, or are produced by another goal specified before this one on the command line). |
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等参数可以对间接依赖、传递依赖进行控制。
相关推荐
maven-assembly-plugin-2.2-beta-5.jar
Maven-assembly-plugin是maven中针对打包任务而提供的标准插件,可以实现自定义打包。主要提供如下功能: ● 提供一个把工程依赖元素、模块、网站文档等其他文件存放到单个归档文件里。 ● 打包成指定格式分发包,...
Spring Boot项目使用maven-assembly-plugin根据不同环境打包成tar.gz或者zip
java运行依赖jar包
java运行依赖jar包
maven-license-plugin-2.4.1.jar
maven打包dubbo服务接口(maven-assembly-plugin)
主要介绍了Maven3种打包方式中maven-assembly-plugin的使用,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
Maven的组装插件的例子 该项目包含maven-assembly-plugin用法的示例阅读 yeoman generator(java)生成的项目
maven打包,指定入口类的jar包,具体的pom配置为:<plugin> <groupId>org.apache.maven.plugins <artifactId>maven-shade-plugin <version>3.1.0 <phase>package <goal>shade implementation=...
maven安装与配置 确保已安装JDK。Maven 3.9+版本需要JDK 8或更高版本。可以通过输入java -version来检查JDK是否安装以及其版本。 下载Maven。访问Maven官网下载最新版本,例如3.9.1。对于Windows用户,通常下载...
maven-assembly-plugin maven-jar-plugin spring-boot-maven-plugin maven-dependency-plugin maven-resources-plugin CHANGELOG 1.maven-assembly-plugin 配置assembly.xml文件路径 <plugin> <artifactId>maven-...
maven-assembly-plugin-2.2-beta-5.jar maven-bundle-plugin-1.0.0.jar maven-clean-plugin-2.4.1.jar maven-clean-plugin-2.5.jar maven-common-artifact-filters-1.3.jar maven-compat-3.2.1-sources.jar maven-...
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
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...
这样,您可以确保您不会浪费时间在Apache Maven范围之外的东西上。 假设您的问题不存在,请提交该问题的票证。 清楚地描述问题,包括在出现错误时重现的步骤。 确保填写您知道存在问题的最早版本。 在GitHub上...
java运行依赖jar包
java运行依赖jar包
java运行依赖jar包