Summary
In this post I’ll demonstrate how quickly a simple Maven plugin can be created.
Reason
Normally in Maven project you do not need to create own plugins as there likely exists a plugin which covers common scenarios. However, there are edge cases or project specific needs where having own plugin would make sense.
What do we need
- Installed Maven (I used 3.6.3)
- Java 11+ (for lower Java version sample
POM
needs to be adjusted) - Your favorite text editor
Naming
Every project starts from its name. The plugin naming convention is
. In sample I’ll use struggzard-maven-plugin.
Project POM
Create project directory struggzard-maven-plugin and add pom.xml
file with content bellow:
4.0.0
dev.stuggzard.tutorial
struggzard-maven-plugin
1.0-SNAPSHOT
maven-plugin
A Simple Maven Plugin
11
${java.version}
${java.version}
org.apache.maven
maven-plugin-api
3.0
org.apache.maven.plugin-tools
maven-plugin-annotations
3.4
provided
At this point maven build will likely fail as there no Mojo defined.
Add Mojo
The “Mojo” in Maven terminology is a simple class which represents one plugin goal (e.g. print message).
package dev.struggzard.tutorial;
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;
import org.apache.maven.plugins.annotations.Parameter;
/**
* A Mojo which accepts parameter and prints it.
*/
@Mojo(name = "print")
public class MessagePrinterMojo extends AbstractMojo {
@Parameter( property = "print.text", defaultValue = "Default text" )
private String textParameter;
public void execute() throws MojoExecutionException, MojoFailureException {
getLog().info(textParameter);
}
}
Running from CLI
Maven plugin can be invoked directly from CLI using following pattern:
mvn groupId:artifactId:version:goal
Run plugin:
mvn dev.stuggzard.tutorial:struggzard-maven-plugin:1.0-SNAPSHOT:print
Output:
[INFO] Scanning for projects...
[INFO]
[INFO] ---< dev.stuggzard.tutorial:struggzard-maven-plugin >---
[INFO] Building A Simple Maven Plugin 1.0-SNAPSHOT
[INFO] ---[ maven-plugin ]---
[INFO]
[INFO] --- struggzard-maven-plugin:1.0-SNAPSHOT:print (default-cli) @ struggzard-maven-plugin ---
[INFO] Default text
[INFO] -----------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] -----------------------------------------------------------------------
[INFO] Total time: 0.390 s
[INFO] Finished at: 2020-06-21T17:29:28+03:00
[INFO] ------------------------------------------------------------------------
As no parameter was passed [INFO] Default text
was printed.
Now try passing text parameter:
mvn dev.stuggzard.tutorial:struggzard-maven-plugin:1.0-SNAPSHOT:print -"Dprint.text=Sample Text"
Output:
[INFO] Scanning for projects...
[INFO]
[INFO] ---< dev.stuggzard.tutorial:struggzard-maven-plugin >---
[INFO] Building A Simple Maven Plugin 1.0-SNAPSHOT
[INFO] ---[ maven-plugin ]---
[INFO]
[INFO] --- struggzard-maven-plugin:1.0-SNAPSHOT:print (default-cli) @ struggzard-maven-plugin ---
[INFO] Sample Text
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.346 s
[INFO] Finished at: 2020-06-21T17:33:08+03:00
[INFO] ------------------------------------------------------------------------
With passed parameter text was printed accordingly [INFO] Sample Text
.
Adding plugin in other Maven project
In most cases plugins are created for integration into another maven project. Here some sample Maven project POM
:
4.0.0
dev.struggzard.tutorial
sample-module
1
11
${java.version}
${java.version}
dev.stuggzard.tutorial
struggzard-maven-plugin
1.0-SNAPSHOT
validate
print
Sample text
By invoking mvn clean install
on maven project Maven plugin goal print
will be triggered with configured text parameter.
Resources
- Plugin source code – https://github.com/struggzard/struggzard-maven-plugin
- Maven plugin guide – http://maven.apache.org/guides/plugin/guide-java-plugin-development.html