如何在 Maven 插件运行之前执行一组目标?

发布于 2024-08-05 00:07:38 字数 106 浏览 3 评论 0原文

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南城追梦 2024-08-12 00:07:38

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

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

/**
 * 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/resources/META-INF/maven/lifecycle.xml 中定义自定义生命周期。

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

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

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

<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>

使用这种方法,依赖插件将在分叉生命周期的进程资源阶段中使用每个配置调用一次(所有这些都发生在 Mojo 中定义的执行中)。

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

您可以在 中找到有关生命周期的更多信息Maven 书籍的创建自定义生命周期 部分。但它并没有给出允许的详尽列表。我知道的唯一其他参考来自 Maven 2 alpha,所以可能不是那个最新

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

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 {
...

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

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

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>

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).

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文