How to attach a maven plugin to a phase by default?

Pablo Fernandez picture Pablo Fernandez · Mar 6, 2011 · Viewed 7.3k times · Source

I have a maven plugin that should run in the compile phase, so in the project that consumes my plugin, I have to do something like this:

<executions>
 <execution>
  <phase>compile</phase>
  <goals>
   <goal>my-goal</goal>
  </goals>
 </execution>
</executions>

What I need is to by default attach my-goal to the compile phase if the user has included my plugin already (ideally the above part wouldn't be necessary, just the plugin declaration).

Is this possible?

Answer

bmargulies picture bmargulies · Mar 6, 2011

Put an @phase annotation in your Mojo classdef annotations.

The doc says:

@phase <phaseName>

This annotation specifies the default phase for this goal. If you add an execution for this goal to a pom.xml and do not specify the phase, Maven will bind the goal to the phase specified in this annotation by default.

If this doesn't work, I guess a JIRA is warranted.