maven-assembly-plugin 的includeBaseDirectory研究

本文详细探讨了maven-assembly-plugin在构建tar分发包时如何避免生成额外的顶级目录。通过在assembly.xml中设置<includeBaseDirectory>false</includeBaseDirectory>,可以实现直接解压出子目录的目标。文章还深入分析了maven-assembly-plugin的源码,解释了includeBaseDirectory参数的工作原理及其在创建归档文件时的影响。
摘要由CSDN通过智能技术生成

 

需求:

这个问题一度困扰了我好几个小时,我一开始使用maven-assembly-plugin构建tar分发包的时候,发现每次最终打包,都会在最外层有个包装层,比如我要构建的tar分发包的artifactId为abc ,那么最终打包完的tar文件总是内含abc目录,然后才是其他子目录sub1,sub2。而我们所希望的是当untar时候,能够直接出来的是子目录(sub1,sub2),而不是abc目录+abc目录里的子目录(/abc/sub1,/abc/sub2)的形式。

解决方案:

其实只要在assembly.xml中加上<includeBaseDirectory>元素,并且让其设为false就可以了,如下:

 
  
  1. <assembly>  
  2.   
  3.   <id>tarball</id>  
  4.   
  5.   <formats>  
  6.   
  7.     <format>tar</format>  
  8.   
  9.   </formats>  
  10.   
  11.     
  12.   
  13.   <!--fixed the wrapper folder issue-->  
  14.   
  15.   <includeBaseDirectory>false</includeBaseDirectory>  
  16.   
  17.    
  18.   
  19.   <fileSets>  
  20.   
  21.  ... 

关于这个参数的含义,可以参见maven-assembly-plugin的官网:

http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html

 

深入分析:

为什么这样可以呢,我们可以对maven-assembly-plugin的源代码进行研究。

首先,当我们在pom.xml中使用maven-assembly-plugin并且在<descriptor>中配置了assembly.xml文件的位置时候:

 
  
  1. <build>  
  2.                  <plugins>  
  3.                           <plugin>  
  4.                                    <groupId>org.apache.maven.plugins</groupId>  
  5.                                    <artifactId>maven-assembly-plugin</artifactId>  
  6.                                    <version>2.2.1</
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值