2011-11-23 82 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>