2011-11-23 1516 views
11

嗨我對maven很新穎。我想改變Maven插件執行的順序。 在我的pom.xml中,我有maven-assembly插件和maven ant插件。我使用maven assembly插件創建zip文件和maven ant插件,用於將zip文件從目標文件複製到其他目錄。當我運行pom.xml時,maven ant插件被觸發並最終找到zip文件,我得到了錯誤,說沒有找到zip文件。請告訴我如何在maven ant插件運行之後首先運行maven assembly插件的方式,以便它將zip文件複製到相應的目錄中。更改maven插件執行的順序

+0

其值得注意的是,如果你有一個步驟是從文件讀取屬性你不能在pom的後期階段使用該屬性...查看[this post](http://stackoverflow.com/questions/8541332/maven-read-version-number-from-property-file/27782825# 27782825) –

回答

25

既然你說你對Maven很新穎...... Maven構建是一個有序的階段系列的執行。這些階段由適合您的項目based on its packaginglifecycle確定。

因此,控制插件的目標是通過binding it to a particular phase執行。

希望有所幫助。

編輯:此外,由於Maven的3.0.3,對於綁定到同相兩個插件,執行的順序是一樣的,你定義它們的順序。例如:

<plugin> 
    <artifactId>maven-plugin-1</artifactId> 
    <version>1.0</version> 
    <executions> 
    <execution> 
     <phase>process-resources</phase> 
     ... 
    </execution> 
    </executions> 
</plugin> 
<plugin> 
    <artifactId>maven-plugin-2</artifactId> 
    <version>1.0</version> 
    <executions> 
    <execution> 
     <phase>process-resources</phase> 
     ... 
    </execution> 
    </executions> 
</plugin> 
<plugin> 
    <artifactId>maven-plugin-3</artifactId> 
    <version>1.0</version> 
    <executions> 
    <execution> 
     <phase>generate-resources</phase> 
     ... 
    </execution> 
    </executions> 
</plugin> 

在上述情況下,執行順序是:

  1. 行家-插件-3(產生資源)
  2. 行家-插件-1(處理資源)
  3. 行家-插件-2(處理資源)以相同的相位
+0

感謝您的回覆。 – user1062115

6

插件在聲明的順序執行。

在POM層次結構的情況下,你必須從父POM(只是它的groupId和artifactId的)的插件重新申報到孩子POM指定執行順序:

家長的pom.xml

<plugins> 
    <plugin> 
     <groupId>groupid.maven.1</groupId> 
     <artifactId>maven-plugin-1</artifactId> 
     <version>1.0</version> 
     <executions> 
      <execution> 
       <phase>package</phase> 
      </execution> 
     </executions> 
    </plugin> 
</plugins> 

兒童的pom.xml

<plugins> 
    <plugin> 
     <groupId>groupid.maven.2</groupId> 
     <artifactId>maven-plugin-2</artifactId> 
     <version>1.0</version> 
     <executions> 
      <execution> 
       <phase>package</phase> 
      </execution> 
     </executions> 
    </plugin> 
    <plugin> 
     <groupId>groupid.maven.1</groupId> 
     <artifactId>maven-plugin-1</artifactId> 
    </plugin> 
</plugins> 

然後執行的是:

  1. maven.plugin.2
  2. maven.plugin.1
0

在Maven中3.0.3和以後,有兩個規則

  1. 插件執行按照他們的階段進行排序。請參閱 https://maven.apache.org/ref/current/maven-core/lifecycles.html for 階段的順序。

例如,這裏mavin-插件-1之前行家-插件-2 因爲過程資源相被定義爲 編譯階段之前發生執行。

<plugin> 
    <artifactId>maven-plugin-2</artifactId> 
    <version>1.0</version> 
    <executions> 
    <execution> 
     <phase>compile</phase> 
     ... 
    </execution> 
    </executions> 
</plugin> 
<plugin> 
    <artifactId>maven-plugin-1</artifactId> 
    <version>1.0</version> 
    <executions> 
    <execution> 
     <phase>process-resources</phase> 
     ... 
    </execution> 
    </executions> 
</plugin> 
  • 如果多次執行具有相同的相位,則第一個是 執行將內置的一個(例如行家編譯-插件),其ID 是默認-東西,那麼其他執行將發生在 命令他們出現在你的pom文件。
  • 舉例來說,如果你有這樣的地方在你的POM

     <plugin> 
          <artifactId>maven-plugin-1</artifactId> 
          <version>1.2.3</version> 
          <executions> 
           <execution> 
            <id>my-compile</id> 
            <phase>compile</phase> 
           </execution> 
          </executions> 
         </plugin> 
         <plugin> 
          <artifactId>maven-plugin-2</artifactId> 
          <version>4.5.6</version> 
          <executions> 
           <execution> 
            <id>my-compile-2</id> 
            <phase>compile</phase> 
           </execution> 
          </executions> 
         </plugin> 
    

    ,並在你的有效POM這種隨時隨地

    <plugin> 
        <artifactId>maven-compiler-plugin</artifactId> 
        <version>3.1</version> 
        <executions> 
         <execution> 
         <id>**default-compile**</id> 
         <phase>compile</phase> 
         <goals> 
          <goal>compile</goal> 
         </goals> 
         </execution> 
         ... 
        </executions> 
        </plugin> 
    

    然後Maven的編譯器插件將執行Maven的反編譯插件,然後是maven-plugin-1和maven-plugin-2。

    如果你想Maven的編譯器插件:編譯進球后執行Maven的插件-1,那麼你可以做到這一點

    <plugin> 
        <artifactId>maven-plugin-1</artifactId> 
        <version>1.2.3</version> 
        <executions> 
         <execution> 
          <id>my-compile</id> 
          <phase>compile</phase> 
         </execution> 
        </executions> 
    </plugin> 
    <plugin> 
        <artifactId>maven-compiler-plugin</artifactId> 
        <version>3.1</version> 
        <executions> 
         <execution> 
          <id>something-other-than-**default-compile**</id> 
          <phase>compile</phase> 
         </execution> 
         <execution> 
          <id>**default-compile**</id> 
          <phase>none</phase> 
          <goals> 
           <goal>compile</goal> 
          </goals> 
         </execution> 
        </executions> 
    </plugin>