bigskull
bigskull

Reputation: 809

How to read Mojo name inside the custom maven plugin?

I created a custom maven plugin and want to read the name of the goal within the code.

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

Upvotes: 2

Views: 288

Answers (1)

sunix
sunix

Reputation: 353

you can inject mojoExecution and retrieve the goal name from it:

@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`.
  }

}

Upvotes: 2

Related Questions