【问题标题】:Changing the order of maven plugins execution更改 Maven 插件执行的顺序
【发布时间】:2012-01-04 20:19:19
【问题描述】:

我是新手。我想改变maven插件的执行顺序。

在我的 pom.xml 中,我有 maven-assembly-plugin 和 maven-ant-plugin 。我使用 maven-assembly-plugin 来创建 zip 文件,使用 maven-ant-plugin 来将 zip 文件从目标复制到某个其他目录。当我运行 pom.xml 时,maven-ant-plugin 被触发并寻找 zip 文件,最后我收到错误消息,提示找不到 zip 文件。请建议我在需要运行 maven-ant-plugin 之后首先运行 maven-assembly-plugin 的方式,以便它将 zip 文件复制到相应的目录。

【问题讨论】:

  • 值得注意的是,如果您有一个从文件中读取属性的步骤,您将无法在 pom 的后期阶段使用该属性...请参阅this post

标签: maven-3


【解决方案1】:

既然你说你对 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. maven-plugin-3(生成资源)
  2. maven-plugin-1(进程资源)
  3. maven-plugin-2(进程资源)

【讨论】:

    【解决方案2】:

    同一阶段的插件按照声明的顺序执行。

    在 pom hierachy 的情况下,你必须将父 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

    【讨论】:

      【解决方案3】:

      在 Maven 3.0.3 及更高版本中,有两条规则

      1. 插件执行按其阶段排序。看 https://maven.apache.org/ref/current/maven-core/lifecycles.html 为 阶段顺序。

      例如这里mavin-plugin-1在maven-plugin-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> 
      
      1. 如果多个执行具有相同的阶段,则第一个是 执行的将是内置的(例如 maven-compiler-plugin),其 id 是 default-something,那么其他执行将在 按顺序显示在您的 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-compiler-plugin 将执行 maven-compiler-plugin 后跟 maven-plugin-1 和 maven-plugin-2。

      如果你想让 maven-compiler-plugin:compile 目标在 maven-plugin-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>
      

      【讨论】:

      • &lt;goals&gt; &lt;goal&gt;compile&lt;/goal&gt; &lt;/goals&gt; 在您指定 &lt;phase&gt;none&lt;/phase&gt; 时不是必需的
      【解决方案4】:

      这可能是一个老问题,提供的答案是正确的,只是在继承的情况下添加了一个额外的点,因为我发现自己处于这种情况并且无法弄清楚。

      在没有提供继承的情况下,插件订单执行似乎有两条规则:

      1. 插件绑定到的阶段按照文档中提供的顺序执行:请参阅Introduction to the Build Lifecycle 上的“默认生命周期” 插件执行按其阶段排序。
      2. “在 Maven 2.0.5 及更高版本中,绑定到一个阶段的多个目标按照它们在 POM 中声明的顺序执行,但是不支持同一插件的多个实例。同一插件的多个实例是分组执行并在 Maven 2.0.11 及更高版本中排序" - source

      在继承的情况下:“父 POM 绑定在子绑定之后执行。”。因此,如果您的插件没有按照您期望的顺序执行(通过遵循此答案中提到的前两点),您可能应该查看它们是否在父 POM 中定义。 - Incorrect execution order of plugins in the same phase.

      我在任何地方都找不到明确的记录,但是有一张document the algorithm calculating the order of plugin executions 的公开票。

      【讨论】:

      • 感谢您的解释!我认为这同样适用于个人资料。如果插件已在主构建部分中定义,则配置文件中的顺序将被忽略 - 至少在我的情况下。如果您有一个用于多个任务的插件,那就有点烦人了。但也许它是构建阶段超载的标志;-)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-04
      相关资源
      最近更新 更多