When you write a custom plugin, you are going to be writing a series of Mojos (goals). Every Mojo is a single Java class which contains a series of annotations that tell Maven how to generate the Plugin descriptor described in the previous section. Before you can start writing Mojo classes, you will need to create Maven project with the appropriate packaging and POM.
To create a plugin project, you should use the Maven
Archetype plugin. The following command-line will create a plugin with a
groupId of
org.sonatype.mavenbook.plugins and the
artifactId of
first-maven-plugin:
$ mvn archetype:create \
-DgroupId=org.sonatype.mavenbook.plugins \
-DartifactId=first-maven-plugin \
-DarchetypeGroupId=org.apache.maven.archetypes \
-DarchetypeArtifactId=maven-archetype-mojo
The Archetype plugin is going to create a directory named my-first-plugin which contains the following POM.
Example 11.2. A Plugin Project's POM
<?xml version="1.0" encoding="UTF-8"?><project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.sonatype.mavenbook.plugins</groupId>
<artifactId>first-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<name>first-maven-plugin Maven Mojo</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
The most import element in a plugin project's
POM is the packaging element which has a value of
maven-plugin. This packaging element customizes the
Maven lifecycle to include the necessary goals to create a plugin
descriptor. The plugin lifecycle was introduced in Section 4.2.3, “Maven Plugin”, it is similar to the Jar
lifecycle with three exceptions: plugin:descriptor is
bound to the generate-resources phase,
plugin:addPluginArtifactMetadata is added to the
package phase, and
plugin:updateRegistry is added to the
install phase.
The other important piece of a plugin project's
POM is the dependency on the Maven Plugin API. This
project depends on version 2.0 of the
maven-plugin-api and it also adds in JUnit as a
test-scoped dependency.
