Writing a Custom Plugin

When you write a custom plugin, you are going to be writing a series of Mojos (goals). Every Mojo is a single Java class that 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 a Maven project with the appropriate packaging and POM.

Creating a Plugin Project

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 an 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 will create a directory named my-first-plugin, which contains the POM shown in Example 17-2.

Example 17-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> ...

Get Maven: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.