Maven plugin - smallest example

Posted by Monik, 25 March 2014.
Programming Maven

My notes about how to start developing Maven plugins, based on https://maven.apache.org/guides/plugin/guide-java-plugin-development.html.

Table of contents

Maven Plugins

Below are the minimal sample steps of developing a Maven plugin:

1. Define the plugin
package sample.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 GreetingMojo extends AbstractMojo
{
public void execute() throws MojoExecutionException
{
getLog().info( "Hello, world." );
}
}
<project>
<modelVersion>4.0.0</modelVersion>

<groupId>
sample.plugin</groupId>
<artifactId>
hello-maven-plugin</artifactId>
<version>
1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>

<name>Sample Parameter-less Maven Plugin</name>

<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
</project>
2. Add the plugin to another project
<build>
<plugins>
<plugin>
<groupId>
sample.plugin</groupId>
<artifactId>
hello-maven-plugin</artifactId>
<version>
1.0-SNAPSHOT</version>
</plugin>
</plugins>
</build>

3. Run the plugin
mvn sample.plugin:hello-maven-plugin:1.0-SNAPSHOT:sayhi
mvn hello:sayhi
4. Attach the plugin to a build lifecycle phase
<build>
<plugins>
<plugin>
<groupId>
sample.plugin</groupId>
<artifactId>
hello-maven-plugin</artifactId>
<version>
1.0-SNAPSHOT</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>
sayhi</goal>
</goals>
</execution>
</executions>

</plugin>
</plugins>
</build>

Maven Archetypes

"Maven archetype" is a short name for "maven archetype plugin"!

For example the maven-archetype-quickstart plugin we run like this:

>mvn archetype:generate
-DgroupId=com.companyname.bank
-DartifactId=consumerBanking
-DarchetypeArtifactId=maven-archetype-quickstart
-DinteractiveMode=false</div>


Comments


Comments: