编写Maven插件/Mojo:您如何制定目标以强制执行其他目标? [英] Writing a Maven Plugin/Mojo: how do you make a goal force the execution of other goals?

查看:209
本文介绍了编写Maven插件/Mojo:您如何制定目标以强制执行其他目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:此线程是关于编写自定义Mojo的,而不是使用插件的.

我为Maven维护了测试插件.不幸的是,在近一年的时间里,我已经让这个未知的问题徘徊了,我真的很想知道如何处理它,以便其用户可以使用更简单的配置.

I maintain a testing plugin for Maven. Unfortunately, for almost a year now, I've let this particular unknown linger and I'd really like to know how to deal with it so that its users can have a simpler configuration.

假设我们在插件中有两个目标:

Let's say we have two goals in our plugin:

  • prepare(阶段:生成源)
  • do(阶段:编译)
  • prepare (phase: generate-sources)
  • do (phase: compile)

我想将do Mojo配置为要求prepare在构建的早期阶段已执行.但是,描述符文档中没有任何内容建议

I want to configure the do Mojo to require prepare to have been executed in the earlier phase of the build. However, nothing in the descriptor documentation suggests I can.

用户可能不在乎或不理解prepare目标的要点,因此我不想强迫他们在其POM中指定它.当然,我可以直接从do执行Mojo,但是prepare目标的执行时间会晚于预期的时间.

The user probably doesn't care or understand the point of the prepare goal, so I don't want to force them to specify it in their POM. Of course, I could execute the Mojo directly from do, but then the prepare goal will have run at a later phase than is intended.

(我调查了自定义生命周期,但是这似乎使每个已经在其POM中具有prepare目标的人都会在运行do时执行两次该目标.)

(I looked into custom lifecycles, but that makes it appear that everyone who already has the prepare goal in their POMs will have it executed twice upon running do.)

推荐答案

您可能会有类似下面的内容(摘自CompilerMojo):

You could have something like the below (taken from the CompilerMojo):

/**
 * @author <a href="mailto:jason@maven.org">Jason van Zyl </a>
 * @version $Id: CompilerMojo.java 941498 2010-05-05 21:24:11Z krosenvold $
 * @since 2.0
 * @goal compile
 * @phase compile
 * @threadSafe
 * @requiresDependencyResolution compile
 */

通过在您的类上进行设置,它将在编译阶段执行(在此示例中).编译阶段要求所有先前的阶段都已首先执行(validate, generate-sources, process-sources, generate-resources, process-resources ...).

By setting this over your class, it will execute during the compile phase (in this example). The compile phase requires all the previous phases to have executed first (validate, generate-sources, process-sources, generate-resources, process-resources...).

基本上,在您需要的阶段(甚至相同的阶段)之后选择一个阶段,它应该可以工作.

Basically, pick a phase after the one you need (or even the same one) and it should work.

这篇关于编写Maven插件/Mojo:您如何制定目标以强制执行其他目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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