如何在我的Maven插件运行之前执行一组目标? [英] How do I execute a set of goals before my Maven plugin runs?

查看:151
本文介绍了如何在我的Maven插件运行之前执行一组目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Maven插件(Mojo),它需要在运行之前执行一组标准的其他插件执行。

I'm writing a Maven plugin (Mojo) that needs to execute a standard set of other plugin executions before it is run.

是否有机制可以声明我的插件中的所有目标,所以我不必依赖用户在他们的POM中定义所有目标?

Is there a mechanism to declare all the goals within my plugin so I don't have to rely on the user defining them all in their POM?

推荐答案

你可以通过定义自定义生命周期并在通过执行注释执行Mojo之前调用该生命周期来实现此目的。

You can do this by defining a custom lifecycle and invoking that lifecycle before your Mojo is executed via the execute annotation.

在你的Mojo中,在Javadoc中声明要执行的生命周期:

In your Mojo, declare in the Javadoc the lifecycle to be executed:

/**
 * Invoke the custom lifecycle before executing this goal.
 * 
 * @goal my-goal
 * @execute lifecycle="my-custom-lifecycle" phase="process-resources"
 */
public class MyMojo extends AbstractMojo {
...

然后在src / main /中定义自定义生命周期资源/ META-INF /行家/ lifecycle.xml。

Then define a custom lifecycle in src/main/resources/META-INF/maven/lifecycle.xml.

生命周期有点像plexus的components.xml,但允许您为这些目标指定配置。

The lifecycle is a bit like plexus' components.xml, but allows you to specify configuration for those goals.

注意语法与pom中的插件配置略有不同。您可以使用以下方法定义目标:作为分隔符而不是指定单独的groupId,artifactId和version元素,否则它与pom中插件配置的执行元素大致相同。你甚至可以在lifecycle.xml中使用一些属性(尽管可能不支持所有属性,我需要检查它)。

Note the syntax is slightly different to plugin configurations in the pom. You define a goal using : as a separator rather than specifying separate groupId, artifactId and version elements, otherwise it is largely the same notation as the execution element of a plugin configuration in the pom. You can even use some properties in the lifecycle.xml (though possibly not all properties are supported, I'll need to check that).

以下示例调用依赖项在流程资源阶段使用不同配置插件两次:

The following example invokes the dependency plugin twice with different configurations in the process-resources phase:

<lifecycles>
  <lifecycle>
    <id>download-dependencies</id>
    <phases>
      <phase>
        <id>process-resources</id>
        <executions>
          <execution>
            <goals>
              <goal>
                org.apache.maven.plugins:maven-dependency-plugin:copy-dependencies
              </goal>
            </goals>
            <configuration>
              <includeScope>compile</includeScope>
              <includeTypes>war</includeTypes>
              <overWrite>true</overWrite>
              <outputDirectory>
                ${project.build.outputDirectory}/wars
              </outputDirectory>
            </configuration>
          </execution>
          <execution>
            <goals>
              <goal>
                org.apache.maven.plugins:maven-dependency-plugin:copy-dependencies
              </goal>
            </goals>
            <configuration>
              <includeScope>compile</includeScope>
              <includeTypes>jar</includeTypes>
              <overWrite>true</overWrite>
              <outputDirectory>
                ${project.build.outputDirectory}/jars
              </outputDirectory>
            </configuration>
          </execution>
        </executions>
      </phase>
    </phases>
  </lifecycle>
</lifecycles>

使用这种方法,依赖插件将在 process-resources 分叉生命周期的阶段(所有发生在Mojo中定义的执行中)。

With this approach, the dependency plugin will be invoked once with each configuration in the process-resources phase of the forked lifecycle (all happening within the execution defined in the Mojo).

在lifecycle.xml中,你可以定义多个阶段,并在生命周期的每个阶段执行多个执行。可用阶段在 Maven生命周期中定义。

In the lifecycle.xml, you can define multiple phases, and multiple executions per phase of the lifecycle. The available phases are defined in the Maven lifecycle.

您可以在创建自定义生命周期部分。它没有详尽列出允许的内容。我知道的唯一其他参考是来自 Maven 2 alpha ,所以可能不是那样的最新

You can find out more about lifecycles in the Creating a Custom Lifecycle section of the Maven book. It doesn't give an exhaustive list of what is allowed though. The only other reference I know if is from the Maven 2 alpha, so is possibly not that up-to-date

这篇关于如何在我的Maven插件运行之前执行一组目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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