【问题标题】:How to read Mojo name inside the custom maven plugin?如何在自定义 maven 插件中读取 Mojo 名称?
【发布时间】:2020-07-20 16:25:01
【问题描述】:

我创建了一个自定义的 maven 插件,并想在代码中读取目标的名称。

@Mojo(name="wite-to-file")


@Mojo(name="write-to-file")

public class CustomPlugin{

public void execute(){

Annotation[] annotations = this.getClass().getAnnotations();

annotations[0].toString() // this does not return the value of the Mojo.

}

}

When maven plugin's execute method gets called, Is there a way to access the current goal name? or the name of the Mojo?

I tried annotations but could not read the value of Mojo.

【问题讨论】:

  • 我猜你也需要扩展 AbstractMojo 类。
  • 扩展到AbstractMojo 是正确的,但为什么需要获取目标名称?你已经通过@Mojo(name="...")定义了它?

标签: java maven maven-plugin


【解决方案1】:

您可以注入mojoExecution 并从中检索目标名称:

@Mojo(name="write-to-file")
public class CustomPlugin {

  @Parameter(defaultValue = "${mojoExecution}")
  protected MojoExecution mojoExecution;

  public void execute(){
    mojoExecution.getMojoDescriptor().getGoal() // this returns the value of the Mojo name `write-to-file`.
  }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    • 2012-11-14
    • 2016-10-16
    • 2017-09-30
    • 1970-01-01
    相关资源
    最近更新 更多