2008-12-09 199 views
45

我有一個使用<packaging>war</packaging>的Maven pom。但實際上,我不想構建戰爭文件,我只想收集所有依賴的jar文件並創建完整的部署目錄。如何讓Maven運行戰爭:爆炸但不是戰爭:戰爭

於是我運行war:exploded目標產生deploy目錄:

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-war-plugin</artifactId> 
    <executions> 
     <execution> 
      <phase>package</phase> 
      <configuration> 
       <webappDirectory>target/${env}/deploy</webappDirectory> 
       <archiveClasses>true</archiveClasses> 
      </configuration> 
      <goals> 
       <goal>exploded</goal> 
      </goals> 
     </execution> 
    </executions> 
</plugin> 

麻煩的是,戰爭文件仍然被建成。有沒有簡單的方法讓<packaging>war</packaging>執行war:exploded目標而不是戰爭:戰爭目標?

還是有另一種簡單的方法來做到這一點?

+1

戰爭文件仍然被創建,那麼什麼?爲什麼這是一個問題? – 2009-09-19 23:27:33

+4

我認爲這是因爲他們想加快構建 - 至少這就是我使用它的原因。我有一個使用maven的基本spring應用程序,但我正在配置Google App Engine,它被配置爲掃描爆炸的war build目錄,所以爲了看到變化,我需要經常製作爆炸戰爭。任何時候在項目中的任何文件被更改時自動調用此方法? – chrismarx 2012-02-13 00:34:32

回答

40

根據builtin lifecycle bindings在封裝階段戰神包裝:戰爭的魔力被調用。

您可以撥打之前的「準備包」相 - 所有操作將執行該調用魔力戰後:爆炸

mvn prepare-package war:exploded 

結果將是你的一樣,但沒有創造戰爭。

+1

如果你真的喜歡這個,你可以把這場戰爭:爆炸性的調用放入準備包裝階段的pom中。但是如果你需要maven繼續運行其他階段,那麼你會遇到問題,所以切換到<包裝> pom更好。 – bmargulies 2010-08-15 12:35:49

+0

我發現這個失敗了,因爲它試圖在'classes'文件夾上做一個FileInputStream.open。然而,作爲兩個單獨的命令運行`mvn prepare-package`然後`mvn war:爆炸`啊maven你神祕的野獸 – 2015-04-02 10:20:58

5

我能想到的,做你想要的東西的唯一辦法就是設置使用POM的包裝(或創建一個custom packaging),並結合從戰爭中所要求的目標打包到生命週期的相關階段。如果你選擇pom打包,你可以在配置文件中使用定義war:戰爭執行來允許你打包它,但是你需要使用build-helper-maven-plugin attach-artifact goal來將戰爭附加到pom上。

注意這種方法如果你想使用任何其他特定於戰爭的處理,它可能會導致你的問題。

戰爭包裝的生命週期綁定列在Introduction to The Build Lifecycle(請參閱「默認生命週期綁定 - 打包ejb/ejb3/jar/par/rar/war」部分)。

到相關的插件執行綁定到POM包裝,你會做如下:

<build> 
    <plugins> 
    <plugin> 
     <artifactId>maven-resources-plugin</artifactId> 
     <executions> 
     <execution> 
      <id>process-resources</id> 
      <phase>process-resources</phase> 
      <goals> 
      <goal>resources</goal> 
      </goal> 
     </execution> 
     </executions> 
    </plugin> 
    <plugin> 
     <artifactId>maven-compile-plugin</artifactId> 
     <executions> 
     <execution> 
      <id>compile</id> 
      <phase>compile</phase> 
      <goals> 
      <goal>compile</goal> 
      </goal> 
     </execution> 
     </executions> 
    </plugin> 
    <plugin> 
     <artifactId>maven-resources-plugin</artifactId> 
     <executions> 
     <execution> 
      <id>process-test-resources</id> 
      <phase>process-test-resources</phase> 
      <goals> 
      <goal>testResources</goal> 
      </goal> 
     </execution> 
     </executions> 
    </plugin> 
    <plugin> 
     <artifactId>maven-surefire-plugin</artifactId> 
     <executions> 
     <execution> 
      <id>test</id> 
      <phase>test</phase> 
      <goals> 
      <goal>test</goal> 
      </goal> 
     </execution> 
     </executions> 
    </plugin> 
    <!-- package not wanted, install and deploy already defined for pom packaging--> 
    <!--define war:war execution in a profile in case it is needed--> 
+0

這是正確的方法。 – bmargulies 2010-08-15 12:34:24

63

該解決方案非常簡單。你需要重寫戰爭插件的默認執行禁用它,並添加自己的執行(對爆炸):

<pluginManagement> 
    <plugins> 
      <plugin><!-- don't pack the war --> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-war-plugin</artifactId> 
       <executions> 
        <execution> 
         <id>default-war</id> 
         <phase>none</phase> 
        </execution> 
        <execution> 
         <id>war-exploded</id> 
         <phase>package</phase> 
         <goals> 
          <goal>exploded</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 
    </plugins> 
</pluginManagement> 
5

我想升級到@邁克爾Wyraz回答,只是包括安裝跳過設置在如果有人執行mvn clean install構建在多模塊項目的頂層,並且其中一個子模塊是Web應用程序。

這代表戰爭模塊內部:

<profiles> 
    <profile> 
     <id>war_explode</id> 
     <build> 
      <pluginManagement> 
       <plugins> 
        <plugin> 
         <groupId>org.apache.maven.plugins</groupId> 
         <artifactId>maven-war-plugin</artifactId> 
         <version>2.6</version> 
         <executions> 
          <execution> 
           <id>default-war</id> 
           <phase>none</phase> 
          </execution> 
          <execution> 
           <id>war-exploded</id> 
           <phase>package</phase> 
           <goals> 
            <goal>exploded</goal> 
           </goals> 
          </execution> 
         </executions> 
        </plugin> 
        <plugin> 
         <groupId>org.apache.maven.plugins</groupId> 
         <artifactId>maven-install-plugin</artifactId> 
         <executions> 
          <execution> 
           <id>default-install</id> 
           <phase>none</phase> 
          </execution> 
         </executions> 
        </plugin> 
       </plugins> 
      </pluginManagement> 
     </build> 
    </profile> 
</profiles> 

沒有安裝跳過,因爲它試圖戰爭進入安裝文件夾的.m2構建失敗。錯誤信息是這樣的:

[INFO] --- maven-install-plugin:2.4:install (default-install) @ *** --- 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD FAILURE 
[INFO] ------------------------------------------------------------------------ 
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-install-plugin:2.4:install (default-install) on project ***: The packaging for this project did not assign a file to the build artifact -> [Help 1] 

執行mvn clean install -P war_explode與此設置(括在一個名爲war_explode Maven的配置文件)它完成建立一個沒有錯誤。