Monday, March 7, 2011

Create Maven Plugin

Your first step in order to play with Maven will be to download Maven and install the same. It’s very simple and you just need to follow instructions available here:


I’m using Maven 3.

You also need to have good understanding of Maven lifecycle and plugins to really benefit from this post. You can refer below link to know more about Maven:
You can also check out Maven site for more details.
A plugin can have several MOJOs, where each MOJO refers to a goal. So when you type
$ mvn jar:jarGoal
That means you’re trying to execute ‘jarGoal’ from ‘jar’ plugin.
Based on my experience, I believe existing plugins offered by Maven will be more than enough for your project needs; however that does not stops you from writing your own plugin J and we’re here for the same.


Your first step will be to create a plug in project.

-          Create 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 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.
<?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>

-          Create MOJO(or goal)
Mojo class is nothing but description of your Goal. You could write this class in Java or any of the scripting language like Groovy, BeanShell, Ruby etc.
Each Mojo class is going to implement the org.apache.maven.plugin.Mojo interface, the Mojo implementation shown in our example implements the Mojo interface by extending the org.apache.maven.plugin.AbstractMojo class.

The Mojo interface is concerned with two things: logging the results of goal execution and executing a goal. When you are writing a custom plugin, you'll be extending AbstractMojo. AbstractMojo takes care of handling the setLog() and getLog() implementations and contains an abstract execute() method. When you extend AbstractMojo, all you need to do is implement the execute() method.

package org.sonatype.mavenbook.plugins;

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

/**
 * Echos an object string to the output screen.
 *
 * @goal echo
 * @requiresProject false
 */
public class EchoMojo extends AbstractMojo {
                /**
                 * Any Object to print out.
                 *
                 * @parameter expression="${echo.message}" default-value="Hello World..."
                 */
                private Object message;

                public void execute() throws MojoExecutionException, MojoFailureException {
                                getLog().info(message.toString());
                }
}

Other thing to note is that, we have also provided our goal a name: ‘echo’ (with annotation on top @goal echo), and also mentioned that our Mojo (or goal) accepts a parameter echo.message whose default value is ‘Hello World’.
That means, while executing our plugin’s goal (i.e. echo), we could write:
$mvn org.sonatype.mavenbook.plugins:first-maven-plugin:1.0-SNAPSHOT:echo –Decho.message=Print me
And on console you could see a message : Print me
Specifying the groupId, artifactId, version, and goal on the command-line is cumbersome. What we want is to execute our goal with simple command like
$mvn first:echo
To achieve this, we need to tell Maven that ‘first’ actually means org.sonatype.mavenbook.plugins:first-maven-plugin:1.0-SNAPSHOT”, and this mapping need to be done in your ~/.m2/settings.xml file.

In your ~/.m2/settings.xml, add this line:
<pluginGroups>
<pluginGroup>org.sonatype.mavenbook.plugins</pluginGroup>
</pluginGroups>

That’s it! You’re done!
Now on command line, if you type:
$mvn first:echo
You can see ‘Hello World…’ on console.
This worked because our project adhered to a naming convention for Maven plugins. If your plugin project has an artifactId which follows the pattern maven-first-plugin or first-maven-plugin, maven will automatically assign a plugin goal prefix of first to your plugin. Well, you’re also free to customize any name to your plugin, so instead of first:echo, you could also execute your goal with second:echo or whatever you like. Check maven-plugin-plugin Plugin for more details.

References:

No comments:

Post a Comment