Wednesday, August 4, 2010

Develop a simple Maven plugin

While this is a really old topic, the process to develop a maven plugin still need some time to sort our especially for people who are not familiar with Maven, like me. So here is a very short introduction on how to develop a maven plugin and how to integrate it with your existing application.

Prerequisite: of course, you need Maven, and all other things will totally depend on your wish. For me, I use Eclipse with m2eclipse plugin which saves me some time to create the archetype of Maven plugin project. However, this is a rather simple process and everyone can do it in command line with only a few typing.

1. Create a new Maven plugin project in Eclipse. Here, we name the project groupid: Featheast, artifactid: maven-test-plugin. Please pay attention to the naming convention of artifactid which we will use a little bit later.

2. Under the src directory, create a new Class naming MyMojo which extends AbstractMojo. AbstractMojo is the class you must inherit for plugin to work, and the only method you have to implement is execute(), where the real business will happens.

3. In order for other project to recognize your plugin function, you have to specify what the Mojo does. In Maven, this is accomplished by add an annotation @goal for the class in the comment of class. This goal name will be used later for other projects to reference this function.

4. You can create any number of variables in the class which acts like a parameter for later process. Consider the whole class as a function, then this variables will be the arguments you passed in. For each variable, another annotation @parameter will be used to specify how to like the variable to external usage.

5. Set the real business logic in the execute() function. You can use the Maven log to print out or debug for your convenience. An internal method getLog() is always there for you to do so and the usage of it is quite similar to the log4j.

/**

*

* @author yudong

*

* @goal realmojo

*/

public class MyRealMojo extends AbstractMojo{



/**

* @parameter expression="${mymojo.username}"

*/

private String username;



/**

* @parameter expression="${mymojo.password}"

*/

private String password;







public void execute() throws MojoExecutionException, MojoFailureException {

if(password.length()<10){

getLog().info("Hey" + username +", your password is too short!");

}else{

getLog().info("Congratulations " + username + ", your password is all right!");

}

Set set = getPluginContext().keySet();

getLog().info("The context include " + set.size() + " entries");

Iterator iterator = set.iterator();



while(iterator.hasNext()){

Object key = iterator.next();

getLog().info(key.toString() + " : " + getPluginContext().get(key));

}

}

}

6. In the pom.xml, add any dependency you need, then add the maven-plugin-plugin  to build the plugin. Specify any goals that you want to be included in the build output.

<build>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-plugin-plugin</artifactId>

<version>2.5.1</version>

<configuration>

<goalPrefix>Plugin.Test</goalPrefix>

<username>Featheast</username>

</configuration>

<goals>

<goal>

realmojo

</goal>

</goals>

</plugin>

</plugins>

</build>

7. Now your Maven plugin is created, build it with standard command: mvn install.

8. Create another project to use this plugin. Add the plugin configuration in the pom.xml, and specify the goal.

<build>

<plugins>

<plugin>

<groupId>Featheast</groupId>

<artifactId>maven-test-plugin</artifactId>

<version>0.0.1-SNAPSHOT</version>  

<executions>

<execution>

<phase>install</phase>

<goals>

<goal>realmojo</goal>

</goals>

<configuration>

<username>This is the usernmae</username>

</configuration>

</execution>

</executions>

</plugin>

</plugins>

</build>

9. You could specify the parameters in the configuration tag, or you can add -Dusername = XXX in the command line to pass in the parameters.

10. Finally what you did will be print on the console, once you build the new project.

No comments:

Post a Comment