A Technology Blog About Code Development, Architecture, Operating System, Hardware, Tips and Tutorials for Developers.

Wednesday, October 5, 2016

Custom Maven Plugin

7:30:00 PM Posted by Satish Kumar , No comments
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. 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.tat.api and the artifactId of webapp-optimizer:

pom.xml
The most important element in a plugin project’s POM is the packaging element which has a value of maven-plugin. 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.

The Mojo implementation shown in the following example implements the Mojo interface by extending the org.apache.maven.plugin.AbstractMojo class. Before we dive into the code for this Mojo, let’s take some time to explore the methods on the Mojo interface. Mojo provides the following methods:

void setLog( org.apache.maven.monitor.logging.Log log )
When Maven loads and executes a Mojo, it is going to call the setLog() method and supply the Mojo instance with a suitable logging destination to be used in your custom plugin.

protected Log getLog()
Maven is going to call setLog() before your Mojo is executed, and your Mojo can retrieve the logging object by calling getLog().

void execute() throws org.apache.maven.plugin.MojoExecutionException
This method is called by Maven when it is time to execute your goal.


You can run the plug-in using the following command. The commands is as per the groupId:artifactId:version:goal format.

 When you run this command-line you should see output that contains the output of the echo goal with the default message: "Hello Maven World…". If you want to customize the message, you can pass the value of the message parameter with the following command-line:

0 comments:

Post a Comment