Create a Simple Maven Plugin


In this post we are going to create a simple maven plugin that prints out the message “Hello World” during the build process of a project.

Create a Maven Project

create a Maven project for our plugin development.  The details are as follows:

  • Group Id: com.skills421.maven.plugin
  • Artifact Id: HelloWorld-maven-plugin
  • Version: 1.0-SNAPSHOT
  • Project name: HelloWorld
  • Project location: /Development/Skills421/Training/MavenPlugins/HelloWorld

Edit the pom as follows


4.0.0

com.skills421.maven.plugin
HelloWorld-maven-plugin
1.0-SNAPSHOT
maven-plugin

<!-- Maven Compiler plugin -->
1.8
1.8

org.apache.maven
maven-plugin-api
3.0

<!-- dependencies to annotations -->

org.apache.maven.plugin-tools
maven-plugin-annotations
3.4
provided

create the package: com.skills421.maven.plugin

create the class: HelloWorld

package com.skills421.maven.plugin;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;

/**
* Says "Hi" to the user.
*
*/
@Mojo( name = "sayhi")
public class HelloWorld extends AbstractMojo
{
public void execute() throws MojoExecutionException
{
getLog().info( "Maven-Plugin: Hello World." );
}
}<span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span>

build the plugin: mvn clean install

Create a Project to use the plugin

pom.xml


4.0.0

com.skills421.maven.plugin
TestPlugins
1.0-SNAPSHOT

<!-- Maven Compiler plugin -->
1.8
1.8

com.skills421.maven.plugin
HelloWorld-maven-plugin
1.0-SNAPSHOT

HelloWorld

package com.pronapse.test;

public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}<span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span>

Compile to project

mvn clean install

Note that there is no output from our plugin.

Now repeat using the following:

mvn com.skills421.maven.plugin:HelloWorld-maven-plugin:sayhi

(mvn groupId:artifactId:version:goal)

This generates the following output:

[INFO] --- HelloWorld-maven-plugin:1.0-SNAPSHOT:sayhi (default-cli) @ TestPlugins ---
[INFO] Hello World.

because the plugin Artifact name is of the form ${prefix}-maven-plugin (HelloWorld-maven-plugin)

we can reduce
mvn com.skills421.maven.plugin:HelloWorld-maven-plugin:sayhi
to
mvn HelloWorld:sayhi

to attach our plugin to the build cycle, we need to add it to the build phase of the pom as follows:


com.skills421.maven.plugin
HelloWorld-maven-plugin
1.0-SNAPSHOT

compile

sayhi

now we can invoke our plugin by simply calling mvn compile

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Up ↑

%d bloggers like this: