maven-assembly-plugin插件使用详解

2024-06-02 19:38

本文主要是介绍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插件来定制化打包。

首先需要添加插件声明:

复制代码

 1 <plugin>  2     <groupId>org.apache.maven.plugins</groupId>  3     <artifactId>maven-assembly-plugin</artifactId>  4     <version>2.4</version>  5     <executions>  6         <execution>  7             <phase>package</phase>  8             <goals>  9                 <goal>single</goal>  
10             </goals>  
11         </execution>  
12     </executions>  
13 </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 如下:

复制代码

 1 <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"   2   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  3   xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">  4   <id>bin</id>  5   <formats>  6     <format>tar.gz</format>  7     <format>tar.bz2</format>  8     <format>zip</format>  9   </formats>  
10   <fileSets>  
11     <fileSet>  
12       <directory>${project.basedir}</directory>  
13       <outputDirectory>/</outputDirectory>  
14       <includes>  
15         <include>README*</include>  
16         <include>LICENSE*</include>  
17         <include>NOTICE*</include>  
18       </includes>  
19     </fileSet>  
20     <fileSet>  
21       <directory>${project.build.directory}</directory>  
22       <outputDirectory>/</outputDirectory>  
23       <includes>  
24         <include>*.jar</include>  
25       </includes>  
26     </fileSet>  
27     <fileSet>  
28       <directory>${project.build.directory}/site</directory>  
29       <outputDirectory>docs</outputDirectory>  
30     </fileSet>  
31   </fileSets>  
32 </assembly>

复制代码

 

自定义Assembly Descriptor

一般来说,内置的assembly descriptor都不满足需求,这个时候就需要写自己的assembly descriptor的实现了。先从一个最简单的定义开始:

复制代码

 1 <?xml version='1.0' encoding='UTF-8'?>  2 <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"  3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  4 xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0  5                     http://maven.apache.org/xsd/assembly-1.1.0.xsd">  6     <id>demo</id>  7     <formats>  8         <format>jar</format>  9     </formats>  
10     <includeBaseDirectory>false</includeBaseDirectory>  
11     <fileSets>  
12         <fileSet>  
13             <directory>${project.build.directory}/classes</directory>  
14             <outputDirectory>/</outputDirectory>  
15         </fileSet>  
16     </fileSets>  
17 </assembly>

复制代码

 

这个定义很简单:

  • format:指定打包类型
  • includeBaseDirectory:指定是否包含打包层目录(比如finalName是output,当值为true,所有文件被放在output目录下,否则直接放在包的根目录下)
  • fileSets:指定要包含的文件集,可以定义多个fileSet
  • directory:指定要包含的目录
  • outputDirectory:指定当前要包含的目录的目的地
    要使用这个assembly descriptor,需要如下配置:

复制代码

1 <configuration>  
2     <finalName>demo</finalName>  
3     <descriptors>  
4         <descriptor>assemblies/demo.xml</descriptor>  
5     </descriptors>  
6     <outputDirectory>output</outputDirectory>  
7 </configuration> 

复制代码

 

最后会生成一个demo-demo.jar 文件在目录 output 下,其中前一个demo来自finalName,后一个demo来自assembly descriptor中的id,其中的内容和默认的打包出来的jar类似。

如果只想有finalName,则增加配置:

1 <appendAssemblyId>false</appendAssemblyId>  

 

添加文件

上面演示了添加所有编译后的资源,同样的可以增加其他资源,例如想添加当前工程目录下的某个文件 b.txt ,在assembly descriptor的assembly结点下增加

复制代码

1 <files>  
2     <file>  
3         <source>b.txt</source>  
4         <outputDirectory>/</outputDirectory>  
5     </file>  
6 </files>

复制代码

 

这里用到了 files 元素类型,可以想象 fileSets 下的结点都是针对文件夹的;files 下的结点都是针对文件的。

也可以改变打包后的文件名,例如上面的 b.txt ,希望打包后的名字为 b.txt.bak, 只需要在file 里添加以下配置 :

1 <destName>b.txt.bak</destName>  

 

排除文件

在fileSet里可以使用includes 和 excludes来更精确的控制哪些文件要添加,哪些文件要排除。
例如要排除某个目录下所有的txt文件:

复制代码

1 <fileSet>  
2     <directory>${project.build.directory}/classes</directory>  
3     <outputDirectory>/</outputDirectory>  
4     <excludes>  
5         <exclude>**/*.txt</exclude>  
6     </excludes>  
7 </fileSet>

复制代码

 

或者某个目录下只想 .class 文件:

复制代码

1 <fileSet>  
2     <directory>${project.build.directory}/classes</directory>  
3     <outputDirectory>/</outputDirectory>  
4     <includes>  
5         <include>**/*.class</include>  
6     </includes>  
7 </fileSet>

复制代码

 

添加依赖

如果想把一些依赖库打到包里,可以用 dependencySets 元素,例如最简单的,把当前工程的所有依赖都添加到包里:

1 <dependencySets>  
2     <dependencySet>  
3         <outputDirectory>/</outputDirectory>  
4     </dependencySet>  
5 </dependencySets>

 

在assembly下添加以上配置,则当前工程的依赖和工程本身生成的jar都会被打包进来。

如果要排除工程自身生成的jar,则可以添加

1 <useProjectArtifact>false</useProjectArtifact>

 

unpack参数可以控制依赖包是否在打包进来时是否解开,例如解开所有包,添加以下配置:

1 <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,可以如下配置:

1 <archive>  
2     <manifest>  
3         <mainClass>demo.DemoMain</mainClass>  
4     </manifest>  
5 </archive>

 

添加MANIFEST项

除了可以指定Main-Class外,还可以添加任意项。比如在OSGI bundle的MANIFEST.MF定义里就有很多用来定义bundle的属性的项,如Import-Package,Export-Package等等。要添加项,可以使用如下配置:

1 <archive>  
2     <manifestEntries>  
3         <Import-Package>javax.xml.ws.*</Import-Package>  
4     </manifestEntries>  
5 </archive> 

 

指定MANIFEST.MF文件

还可以直接指定MANIFEST.MF文件。如下:

1 <archive>  
2     <manifestFile>META-INF/MANIFEST.MF</manifestFile>  
3 </archive>
 

 


                                                                                          -END-

这篇关于maven-assembly-plugin插件使用详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1024906

相关文章

PHP轻松处理千万行数据的方法详解

《PHP轻松处理千万行数据的方法详解》说到处理大数据集,PHP通常不是第一个想到的语言,但如果你曾经需要处理数百万行数据而不让服务器崩溃或内存耗尽,你就会知道PHP用对了工具有多强大,下面小编就... 目录问题的本质php 中的数据流处理:为什么必不可少生成器:内存高效的迭代方式流量控制:避免系统过载一次性

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

springboot中使用okhttp3的小结

《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异... 在 Spring Boot 项目中使用 OkHttp3 进行 HTTP 请求是一个高效且流行的方式。

MySQL的JDBC编程详解

《MySQL的JDBC编程详解》:本文主要介绍MySQL的JDBC编程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、前置知识1. 引入依赖2. 认识 url二、JDBC 操作流程1. JDBC 的写操作2. JDBC 的读操作总结前言本文介绍了mysq

Redis 的 SUBSCRIBE命令详解

《Redis的SUBSCRIBE命令详解》Redis的SUBSCRIBE命令用于订阅一个或多个频道,以便接收发送到这些频道的消息,本文给大家介绍Redis的SUBSCRIBE命令,感兴趣的朋友跟随... 目录基本语法工作原理示例消息格式相关命令python 示例Redis 的 SUBSCRIBE 命令用于订

Java使用Javassist动态生成HelloWorld类

《Java使用Javassist动态生成HelloWorld类》Javassist是一个非常强大的字节码操作和定义库,它允许开发者在运行时创建新的类或者修改现有的类,本文将简单介绍如何使用Javass... 目录1. Javassist简介2. 环境准备3. 动态生成HelloWorld类3.1 创建CtC

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Python中 try / except / else / finally 异常处理方法详解

《Python中try/except/else/finally异常处理方法详解》:本文主要介绍Python中try/except/else/finally异常处理方法的相关资料,涵... 目录1. 基本结构2. 各部分的作用tryexceptelsefinally3. 执行流程总结4. 常见用法(1)多个e

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java