Tag : custom-maven-plugins-using-java

Developing custom maven plugin using Java5 annotations

Maven provides lots of built-in plugins for developers but at some point you may find need of custom maven plugin. Developing custom maven plugin using java5 annotations is very simple and straightforward.




You just need to follow below steps to develop custom maven plugin using Java 5 annotations:




1. Create a new project with pom packaging set to “maven-pom”


2. Add below dependencies to your plugin pom:


i.Maven-plugin-api dependency helps for developing mojos required by custom maven plugin.



ii. Since Maven 3.0 version we can use java 5 annotations to develop custom plugin.with annotations it is not necessasry that mojo super class should be in the same project if your super class also uses annotations. To use annotations in mojos add below dependency to your plugin pom file.



iii.Below dependency is used to not only read Maven project object model files, but to assemble inheritence and to retrieve remote models as required.



iv. If you want to add any test cases or any other 3rd party dependencies add them.


3.Maven plugin tools looks classes with @Mojo annotation any class annotated with @Mojo will be added to plugin configuration file.




import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
public class CustomMojo extends AbstractMojo{
            public void execute() throws MojoExecutionException, MojoFailureException {
            getLog().info("Successfully created custom maven plugin");
                        * your businees logic goes here


The “name” parameter of mojo annotation is your plugins name,your plugin will be recognised with this name.You mojo class extends AbstractMojo class.AbstractMojo class implements mojo interface and set logging for your plugin.AbstractMojo sets log4j based logging getLog() method provides info,error,debug,warn levels of logging.


Mojo interface is having execute method which will contain business logic of plugin.execute method throws 2 kinds of execptions:


i. MojoFailureException : If expected probelm occurs throwing this exception causes Build Failure message to be displayed.Throwing this exception causes build failure.


ii. MojoExecutionException : If unexcepted probelm occurs throwing this exception causes Build error message to be displayed.


4.You can execute your plugin from command line by providing following command:


mvn pluginGroupId:artifactID:version:mojoName

to shorten the command to be executed for plugin add below lines to maven’s settings.xml file in pluginGroups section. This will tell maven to search repository for this groupID:

<pluginGroups><pluginGroup>plugin group id</pluginGroup></pluginGroups>

After this you can run your plugin simply by providing goal prefix and mojo name command to run plugin will be like this:

mvn goalPrefix:mojoName


5. configuring goalPrefix:

To Create goalPrefix add plugin maven-plugin-plugin to maven plugin pom.It is used to create plugin descriptor for any mojo’s found in source tree to include in jar.it can be used for generating report files for mojo’s updating plugin registry.


                                                            <goalPrefix>your goalPrefix</goalPrefix>
                                                            <parameter1>custom param1</parameter1>
                                                            <parameter2>custom param2</parameter2>


6. You can pass external parameters to your plugin from command line and also you can set default values for you parameters if they are not send from command line.



@Parameter(property = "param1", defaultValue = "abc")
            private String type;


command to run plugin by passing parameter is :

mvn goalPrefix:mojoName -Dparam1='acd';


if you set required parameter of property to false then there is no compulsion of passing parameter from command line.

As we all know that maven has default structure of scanning source files in src/main/java structure and test files in src/test folded,similarly if you want your plugin to scan files in particular folder in your project structure then you can do this by adding org.apache.maven.project.MavenProject property

to your project.


            @Parameter(defaultValue = "${project}", readonly = true, required = true)
            private MavenProject project;


you can also set default values to your parameters by setting parameter property name tag in maven-plugin-plugin plugin’s configuration section.if you don’t want to set default property then keep these custom property fields blank.these property fields are the property values you mentioned in mojo.


9. Using your plugin in main project: for using your plugin in another project add plugins dependency in build section of your project.



           <groupId>plugin's group id</groupId>
           <artifactId>>plugin's artifact id</artifactId>
           <version>versin of plugin</version>


10. To release plugin copy plugin’s jar and other dependent jars from your m2 repository and release it to client or qa.


Here we are done with developing custom maven plugin with java 5 annotations.please let me know if you have any doubts or suggestions.The sample project is present on github you can download it from below link:




facebooktwittergoogle_plusredditpinterestlinkedinmailby feather