如何在另一个插件执行前后声明一个 maven 插件的执行前后? [英] How to declare a before and after execution of a maven plugin around another plugin execution?

查看:54
本文介绍了如何在另一个插件执行前后声明一个 maven 插件的执行前后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的插件中创建一个执行顺序,它围绕一个 maven 插件,并在执行另一个 maven 插件之前和之后.所有 3 次执行都是部署阶段的一部分.

I would like to create an execution order in my plugin which surrounds a maven plugin with a before and after execution of another maven plugin. All 3 executions are part of the deploy phase.

这是我想做的一个例子:

Here is an example of what I want to do:

  • 阶段:部署
  • url:get:执行前
  • 依赖:解包
  • url:get:执行后

注意:url:get 是我自己的 custo mojo,只是使用 commons httpClient 执行 http get.

Note: url:get is my own custo mojo and just executes an http get using commons httpClient.

我通常会在下一阶段附加 after 插件执行,但不幸的是部署是 jar 生命周期的最后阶段.

I would usually attach the after plugin execution in the next phase but unfortunately deploy is the last phase of the jar lifecycle.

先谢谢你,

科斯塔斯

注意:我的 pom 文件中的以下插件段创建了以下预期的执行顺序:

Note: The following plugins segment from my pom file creates the following execution order which is not expected:

  • 阶段:部署
  • url:get:执行前
  • url:get:执行后
  • 依赖:解包

插件段:

        <plugin>
            <groupId>com.blabla.stpadmin</groupId>
            <artifactId>maven-url-plugin</artifactId>
            <version>1.0-SNAPSHOT</version>
            <executions>
                <execution>
                    <id>stop-stpadmin-service</id>
                    <phase>deploy</phase>
                    <goals>
                        <goal>get</goal>
                    </goals>
                    <configuration>
        ...
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>unpack</id>
                    <phase>deploy</phase>
                    <goals>
                        <goal>unpack</goal>
                    </goals>
                    <configuration>
        ...
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>com.blabla.stpadmin</groupId>
            <artifactId>maven-url-plugin</artifactId>
            <version>1.0-SNAPSHOT</version>
            <executions>
                <execution>
                    <id>start-stpadmin-service</id>
                    <phase>deploy</phase>
                    <goals>
                        <goal>get</goal>
                    </goals>
                    <configuration>
        ...
                    </configuration>
                </execution>
            </executions>
        </plugin>

推荐答案

您可以将每个插件的执行绑定到同一个阶段,它们将按照您指定的顺序执行.请注意,它们将在部署目标运行后执行,因此您可能希望将它们绑定到前一个阶段(安装)

You can bind the execution of each plugin to the same phase and they will be executed in the order you specify. Note they will be executed after the deploy goal has run, so you might want to bind them to the previous phase (install)

更新:为了确保在依赖插件执行周围执行 execution-beforeexecution-after 目标,您需要确保它们在单独的插件中定义.否则这两个配置将被合并并依次执行.

Update: to ensure the execution-before and execution-after goals are executed around the dependency plugin execution, you'll need to ensure they are defined in separate plugins. Otherwise the two configurations will be merged and executed sequentially.

如果需要在同一个插件中定义两个执行,您可以通过定义自定义生命周期并在通过执行注释执行 Mojo 之前调用该生命周期来实现.在 这个回答 我描述了如何创建自定义生命周期并强制在运行插件之前调用它.如果您将 execute-after 目标配置为调用依赖项插件,您将获得所需的执行顺序(您甚至可以在该生命周期中调用 execute-before 目标也一样).

If the two executions need to be defined in the same plugin, you can do this by defining a custom lifecycle and invoking that lifecycle before your Mojo is executed via the execute annotation. In this answer I described how to create a custom lifecycle and force it to be invoked before a plugin is run. If you configure the execute-after goal to invoke the dependency-plugin you'll get the execution order you want (you could even invoke the execute-before goal in that lifecycle as well).

以下示例将在部署阶段按顺序执行三个插件:

The example below will execute the three plugins in order during the deploy phase:

  <plugin>
    <groupId>custom.url.plugin</groupId>
    <artifactId>maven-url-plugin</artifactId>
    <version>1.0</version>
    <executions>
      <execution>
        <phase>deploy</phase>
        <goals>
          <goal>execution-before</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.1</version>
    <executions>
      <execution>
        <phase>deploy</phase>
        <goals>
          <goal>unpack</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <groupId>custom.url.plugin</groupId>
    <!--specify final execution in a different plugin to 
           avoid the configurations being merged-->
    <artifactId>maven-url-2-plugin</artifactId>
    <version>1.0</version>
    <executions>
      <execution>
        <phase>deploy</phase>
        <goals>
          <goal>execution-after</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

这篇关于如何在另一个插件执行前后声明一个 maven 插件的执行前后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆