maven-assembly-plugin 的includeBaseDirectory研究

來源:互聯網
上載者:User

需求:

這個問題一度困擾了我好幾個小時,我一開始使用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</version>                                     <configuration>                                              <appendAssemblyId>false</appendAssemblyId>                                              <descriptors>                                                      <descriptor>src/main/assembly/assembly.xml</descriptor>                                              </descriptors>                                     </configuration>                                     <executions>                                              <execution>                                                      <id>make-assembly</id>                                                      <phase>package</phase>                                                      <goals>                                                               <goal>single</goal>                                                      </goals>                                              </execution>                                     </executions>                            </plugin>                           .....                  </plugins>           </build>

外掛程式會去調用DefaultAssemblyReader的readAssemblies()方法,然後調用如下代碼進行遍曆<descriptors>元素:

for ( int i = 0; i < descriptors.length; i++ )           {               getLogger().info( "Reading assembly descriptor: " + descriptors[i] );               addAssemblyFromDescriptor( descriptors[i], locator, configSource, assemblies );           }       }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.