Using Plugin Tools Java Annotations

You can use Java annotations to generate the plugin descriptor file.

NOTE With annotations, your Mojo super class does not have to be in the same project: provided that the superclass also uses annotations, it can now come from reactor projects or external dependencies. By default all dependencies are scanned, but this can be reduced with the mojoDependencies parameter. BUT as javadoc doclets are still useful for goals and parameters description, @since and @deprecated, the sources are still scanned. So if you use an external dependency, you must still provide an artifact with sources (sources classifier) to provide documentation (the tooling will skip error if this artifact sources is missing).

Annotations

Information for plugin descriptor generation is specified using 4 annotations:

  • 2 class level annotations:
    • @Mojo: This annotation will mark your class as a Mojo,
    • @Execute: Used if your Mojo needs to fork a lifecycle,
  • 1 field or method level annotations:
    • @Parameter: Used to configure your Mojo parameters,
  • 1 field level annotations:
    • @Component: Used to configure injection of Plexus components or Maven context components.

For more information on these annotations, see the corresponding documentation.

Notice that Plugin Tools Java Annotations are named after Plugin Tools Javadoc Tags with following little differences:

Plugin Tools Javadoc Tags Plugin Tools Java Annotation
@goal "goal-name" @Mojo( name = "goal-name" )
@phase "<phase-name>" @Mojo( defaultPhase = LifecyclePhase.<phase> )

POM configuration

To use these Java annotations, add a dependency on maven-plugin-annotations to your pom, preferably with provided scope.

<project>
  ...
  <packaging>maven-plugin</packaging>
  ...
  <dependencies>
    <!-- dependencies to annotations -->
    <dependency>
      <groupId>org.apache.maven.plugin-tools</groupId>
      <artifactId>maven-plugin-annotations</artifactId>
      <version>4.0.0-beta-1</version>
      <scope>provided</scope> <!-- annotations are not used at runtime because @Retention(value=CLASS), they are needed only to build the plugin -->
    </dependency>
  </dependencies>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-plugin-plugin</artifactId>
        <version>4.0.0-beta-1</version>
        <executions>
          <!-- if you want to generate help goal -->
          <execution>
            <id>help-goal</id>
            <goals>
              <goal>helpmojo</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  ...
  </build>
  ...
</project>