Developing Custom Maven Plugins

Posted on by By Nikhilesh, in Helical Insight, Java, Miscellaneous | 0

Writing Custom Maven Plugins

In this article I would like to point you to a very handy concept i.e. writing custom maven plugins to facilitate in your build process.

If you are a Java developer then you must have been familiar with Maven. Basically maven is a build tool (but its developers say that it is more than that :)). It is a plugin execution engine that helps you write plugins, which can be executed in any maven life cycles.

Basically you will need to be aware of packaging modes, life cycles, build phases and goals of maven. If you are not aware of what all these are, you can find lots of online tutorials that give you good clarity.

Maven community has developed lots of plugins and for most of the use cases you may not need to write your own custom plugin. Sometimes your project build may have specific requiremts that does not get fulfilled by available plugins. In such case, if you have to write one – as I had to write – this tutorial helps you to get your hands dirty with some Java code to write plugins.

Just like POJO in java we have something known as MOJO – Maven plain Old Java Object.

What is a MOJO? (From apache documentation)

What is a Mojo? A mojo is a Maven plain Old Java Object. Each mojo is an executable goal in Maven, and a plugin is a distribution of one or more related mojos.

In short, a mojo is a maven goal, to extend functionality not already found in maven.

So, if you have to write a plugin you are supposed to write one or more mojos.

Here is a sample Mojo(Create a separate maven project):

package com.helicaltech;

 * @author Rajasekhar
 * Developed at Helical IT Solutions
@Mojo(name = "repack",
        defaultPhase = LifecyclePhase.PACKAGE,
        requiresOnline = false, requiresProject = true,
        threadSafe = false)
public class SampleMojo extends AbstractMojo {
    //The parameter basedir is configured in the plugin configuration
    @Parameter(property = "basedir", required = true)
    protected File basedir;

    //Required = true means it is mandatory to configure the xml
    @Parameter(property = "warName", required = true)
    protected String warName;

    public void execute() throws MojoExecutionException {

        //Write your code here that will get executed

Here, name attribut of Mojo is the name of the goal that you would like to run.

The @Mojo annotation defines that the name of the goal is “repack” and that it is not thread safe. It also defines the default phase is the package phase. The last thing is that any member variable can become a parameter. This becomes a parameter for the plugin of a goal.

Example usage of the plugin in POM:


                <!--Parameter for the plugin go here -->

                    <!--You can include the dependencies of your plugins here -->
                    <!--During the package phase the goal repack will be executed -->

The groupId and the artifactId and the version numbers come from the POM file of your plugin.

Once you install the plugin to your maven local repository or your company’s shared central repository so that other developers can use your plugin.


Best Open Source Business Intelligence Software Helical Insight is Here


A Business Intelligence Framework

Hope this article helped you and wish you happy coding!

Helical IT Solutions

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments