需求:
这个问题一度困扰了我好几个小时,我一开始使用maven-assembly-plugin构建tar分发包的时候,发现每次最终打包,都会在最外层有个包装层,比如我要构建的tar分发包的artifactId为abc ,那么最终打包完的tar文件总是内含abc目录,然后才是其他子目录sub1,sub2。而我们所希望的是当untar时候,能够直接出来的是子目录(sub1,sub2),而不是abc目录+abc目录里的子目录(/abc/sub1,/abc/sub2)的形式。
解决方案:
其实只要在assembly.xml中加上<includeBaseDirectory>元素,并且让其设为false就可以了,如下:
- <assembly>
- <id>tarball</id>
- <formats>
- <format>tar</format>
- </formats>
- <!--fixed the wrapper folder issue-->
- <includeBaseDirectory>false</includeBaseDirectory>
- <fileSets>
- ...
关于这个参数的含义,可以参见maven-assembly-plugin的官网:
http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html
深入分析:
为什么这样可以呢,我们可以对maven-assembly-plugin的源代码进行研究。
首先,当我们在pom.xml中使用maven-assembly-plugin并且在<descriptor>中配置了assembly.xml文件的位置时候:
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-assembly-plugin</artifactId>
- <version>2.2.1</