Custom Maven Plugin — Hello World 1

Maven is – at its heart – a plugin execution framework. All work is done by plugins.

Lets try to make one custom plugin. Make one maven project whose packaging should be of maven-plugin.
Below is the pom file :

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.pract.plugin</groupId>
	<artifactId>hello-maven-plugin</artifactId>
	<packaging>maven-plugin</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>hello-maven-plugin Maven Mojo</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>org.apache.maven</groupId>
			<artifactId>maven-plugin-api</artifactId>
			<version>2.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.maven.plugin-tools</groupId>
			<artifactId>maven-plugin-annotations</artifactId>
			<version>3.2</version>
		</dependency>
	</dependencies>
	<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-plugin-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <!-- see http://jira.codehaus.org/browse/MNG-5346 -->
                    <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
                </configuration>

                <executions>
                    <execution>
                        <id>mojo-descriptor</id>
                        <goals>
                            <goal>descriptor</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Now make one java file say GreetingHello which will extend the class AbstractMojo provided by apache.
Below is the class file:

package com.pract.sample.plugin;

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

@Mojo(name = "sayhi",defaultPhase = LifecyclePhase.COMPILE)
public class GreetingHello extends AbstractMojo{

	public void execute() throws MojoExecutionException, MojoFailureException {
		getLog().info("Hello, World!!!");
	}

}

So here the goal name of our plugin is “sayhi”. Thats all our plugin created and ready to be used in other projects.

So how to use in other projects. Simply include this plugin under build tag.
Like there is a ABC project pom, just include it as below:

<plugins>
 <plugin>
  <groupId>com.pract.plugin</groupId>
  <artifactId>hello-maven-plugin</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <executions>
   <execution>
    <goals>
     <goal>sayhi</goal>
    </goals>
   </execution>
  </executions>
 </plugin>
</plugins>

One comment on “Custom Maven Plugin — Hello World

  1. Reply Eyal Golan Apr 30,2014 9:27 pm

    Thanks for this short and clear tutorial.

Leave a Reply