You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by Hilco Wijbenga <hi...@gmail.com> on 2011/04/12 01:50:43 UTC

How to get my plugin to execute in a particular phase?

Hi all,

I'm following the online documentation to write a plugin
(http://maven.apache.org/guides/mini/guide-configuring-plugins.html).
I have

package sample.plugin;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;

/**
 * Says "Hi" to the user.
 *
 * @goal sayhi
 * @phase generate-sources
 */
public class GreetingMojo extends AbstractMojo {
  /**
   * The greeting to display.
   *
   * @parameter expression="${sayhi.greeting}" default-value="Hello World!"
   */
  private String greeting;

  /**
   * @execute phase="generate-sources"
   */
  @Override
  public void execute() throws MojoExecutionException {
    getLog().info(greeting);
  }
}

This works if I invoke the plugin directly (i.e. mvn greeting:sayhi).
I have it configured as

<build><plugins><plugin>
  <groupId>org.example.maven.plugins</groupId>
  <artifactId>greeting-maven-plugin</artifactId>
  <version>0.1-SNAPSHOT</version>
  <configuration>
    <greeting>Howdy!</greeting>
  </configuration>
</plugin></plugins></build>

so it prints "Howdy!". Excellent, so far.

But despite the "@phase generate-sources" and "@execute
generate-sources" I cannot get it to run in the generate-sources phase
by simply invoking something like "mvn compile". (I'm using Maven
3.0.3 and maven-plugin-api 3.0.)

How do I get the greeting plugin to run (by default) in a particular
phase? (I know I can use <executions> to configure it explicitly but
the documentation seems to indicate I can configure a default phase.)

Cheers,
Hilco

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: How to get my plugin to execute in a particular phase?

Posted by Tim Kettler <ti...@udo.edu>.

Am 12.04.2011 01:50, schrieb Hilco Wijbenga:
> Hi all,

Hi,

> I'm following the online documentation to write a plugin
> (http://maven.apache.org/guides/mini/guide-configuring-plugins.html).
> I have
>
> package sample.plugin;
>
> import org.apache.maven.plugin.AbstractMojo;
> import org.apache.maven.plugin.MojoExecutionException;
>
> /**
>   * Says "Hi" to the user.
>   *
>   * @goal sayhi
>   * @phase generate-sources
>   */
> public class GreetingMojo extends AbstractMojo {
>    /**
>     * The greeting to display.
>     *
>     * @parameter expression="${sayhi.greeting}" default-value="Hello World!"
>     */
>    private String greeting;
>
>    /**
>     * @execute phase="generate-sources"
>     */

The @execute invokes a parallel build of the project, up to the 
specified phase. Is that really what you want?

>    @Override
>    public void execute() throws MojoExecutionException {
>      getLog().info(greeting);
>    }
> }
>
> This works if I invoke the plugin directly (i.e. mvn greeting:sayhi).
> I have it configured as
>
> <build><plugins><plugin>
>    <groupId>org.example.maven.plugins</groupId>
>    <artifactId>greeting-maven-plugin</artifactId>
>    <version>0.1-SNAPSHOT</version>
>    <configuration>
>      <greeting>Howdy!</greeting>
>    </configuration>
> </plugin></plugins></build>
>
> so it prints "Howdy!". Excellent, so far.
>
> But despite the "@phase generate-sources" and "@execute
> generate-sources" I cannot get it to run in the generate-sources phase
> by simply invoking something like "mvn compile". (I'm using Maven
> 3.0.3 and maven-plugin-api 3.0.)
>
> How do I get the greeting plugin to run (by default) in a particular
> phase? (I know I can use<executions>  to configure it explicitly but
> the documentation seems to indicate I can configure a default phase.)

 From the Mojo API Specification [1]:

"
@phase:
Defines a default phase to bind a mojo execution to if the user does not 
explicitly set a phase in the POM. Note: This annotation will not 
automagically make a mojo run when the plugin declaration is added to 
the POM. It merely enables the user to omit the <phase> element from the 
surrounding <execution> element.
"

To execute a goal you must declare an execution somewhere, either in the 
projects pom or in a parent. The only way around this is to create a 
custom packaging [2] and bind the goal to a lifecycle phase for the 
packaging

> Cheers,
> Hilco

-Tim

[1] http://maven.apache.org/developers/mojo-api-specification.html
[2] 
http://www.sonatype.com/books/mvnref-book/reference/writing-plugins-sect-plugins-lifecycle.html

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org