Wednesday, 9 May 2012

Java5 annotations support for Maven plugins

So finally or at least (depends on your POV :-)), I have pushed some code to support Java5 annotations support for Maven plugins.
The "specs" are here https://cwiki.apache.org/confluence/display/MAVEN/Java+5+Annotations+for+Plugins.
The code is available in the branch http://svn.apache.org/repos/asf/maven/plugin-tools/branches/MPLUGIN-189/.
Snapshot are deployed to: https://repository.apache.org/content/repositories/snapshot .
You can have a look at it tests to see some samples: http://svn.apache.org/repos/asf/maven/plugin-tools/branches/MPLUGIN-189/maven-plugin-plugin/src/it/annotation-with-inheritance/
Basically you need to setup your pom as it:
    <dependency>
      <groupId>org.apache.maven.plugin-tools</groupId>
      <artifactId>maven-plugin-annotations</artifactId>
      <version>3.0-SNAPSHOT</version>
      <scope>compile</scope>
    </dependency>


  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-plugin-plugin</artifactId>
        <version>3.0-SNAPSHOT</version>
        <configuration>
          <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
        </configuration>
        <executions>
          <execution>
            <id>mojo-descriptor</id>
            <phase>process-classes</phase>
            <goals>
              <goal>descriptor</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  
  <repositories>
    <repository>
      <id>apache.snapshots</id>
      <name>Apache Snapshot Repository</name>
      <url>http://repository.apache.org/snapshots</url>
      <releases>
        <enabled>false</enabled>
      </releases>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <id>apache.snapshots</id>
      <name>Apache Snapshot Repository</name>
      <url>http://repository.apache.org/snapshots</url>
      <releases>
        <enabled>false</enabled>
      </releases>
    </pluginRepository>
  </pluginRepositories>

Annotations to use are:
@Mojo( name = "foo", 
       defaultPhase = LifecyclePhase.COMPILE, 
       threadSafe = true )
@Execute( goal = "compiler", 
          lifecycle = "my-lifecycle", 
          phase = LifecyclePhase.PACKAGE )

public class FooMojo extends AbstractMojo
{
    /**
     * the cool bar to go
     * @since 1.0
     */
    @Parameter( expression = "${thebar}", 
                required = true, 
                defaultValue = "coolbar" )
    protected String bar;

    /**
     * beer for non french folks
     * @deprecated wine is better
     */
    @Parameter( expression = "${thebeer}", 
                defaultValue = "coolbeer" )
    protected String beer;

    /**
     * Plexus compiler manager.
     */
    @Component
    protected CompilerManager compilerManager;

    /**
     *
     */
    @Component( role = "org.apache.maven.artifact.metadata.ArtifactMetadataSource", 
                roleHint = "maven" )
    protected ArtifactMetadataSource artifactMetadataSource;

    public void execute()
        throws MojoExecutionException, MojoFailureException
    {
        // nothing
    }
}
Note: the help generation doesn't work yet !!
A new feature is your parent annotated classes can come from reactor project and from your project dependencies.
And don't complain yet too much in case of issues, it's a work in progress :-)
12 May 2012 UPDATE: Help generation done and code merged in trunk YEAHH :-)
25 May 2012 UPDATE: Few annotations has changed have a look at documentation page: https://cwiki.apache.org/confluence/display/MAVEN/Java+5+Annotations+for+Plugins
31 May 2012 UPDATE: release 3.0 deployed and now available.
Have Fun!