1

Is there a way to get the current execution id of a goal at runtime from the mojo implementation?

I would like to use the execution id in the mojo code to avoid the need for an extra configuration parameter.

Here is an execution example:

<execution>
  <id>spring</id>
  <phase>generate-sources</phase>

  <configuration>
    <id>spring</id>  <!-- duplicate.. -->
    <apiPath>${project.basedir}/src/api/openapi.yaml</apiPath>
  </configuration>
           
  <goals>
    <goal>process</goal>
  </goals>
</execution>

Depending on the value of <id> the process mojo would run different code. I need the id to select the correct service (loaded with java.util.ServiceLoader).

Not sure if reusing the id is a good idea but I would like to know if it is possible at all? :-)

Martin Hauner
  • 1,593
  • 1
  • 9
  • 15

1 Answers1

2

I found the answer to my question in the AbstractCompilerMojo.java class of the maven-compiler-plugin. It declares the following @Parameter

@Parameter( defaultValue = "${mojoExecution}", readonly = true, required = true )
private MojoExecution mojoExecution;

The MojoExecution contains the execution id.

I do not recommend the original motivation of my question: to use the execution id for plugin internal logic.

It is usually not a good idea to re-use details like this for another purpose and apply a second meaning to an existing information. It is confusing and unexpected for someone not aware of it.

It's still a valuable piece of information, because the defaultValue of the @Parameter annotation allows other expressions that can be used by a plugin to access information about the project & the build.

Martin Hauner
  • 1,593
  • 1
  • 9
  • 15