How do I create a hello world goal for a maven plugin?
Author: Deron Eriksson
Description: This tutorial describes how to create a hello world goal for a maven plugin.
Tutorial created using:
Windows Vista || JDK 1.6.0_04 || Eclipse Web Tools Platform 2.0.1 (Eclipse 3.3.1)
In mavenSW, a "goal" is a unit of work that performs a particular task. A plugin is a maven artifact that is essentially a jarW file that contains a set of goals, and these goals are implemented by JavaSW "Mojo" classes. A maven plugin has a packaging of "maven-plugin". In another tutorial, we used the maven archetype plugin to create a basic maven plugin project, and we hooked this project into EclipseSW. In this tutorial, I'll create a Mojo class that implements a particular goal. We'll then install the plugin into the local maven repository so that we can use it. The "maven-howdy-plugin" project is shown below. Like all maven projects, it contains a pom.xml file at its root. It is a maven plugin project. It also contains a "HowdyMojo.java" file. This is the class that implements our goal in our plugin, as we shall see. The pom.xml file is shown here. Notice in particular the packaging type, which is "maven-plugin". Also, notice the "maven-plugin-api" dependency. This dependency contains the various maven plugin classes that we need to have access to (such as AbstractMojo) in order to write our plugin. pom.xml<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.maventest</groupId> <artifactId>maven-howdy-plugin</artifactId> <packaging>maven-plugin</packaging> <version>1.0-SNAPSHOT</version> <name>Howdy Plugin</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>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project> The HowdyMojo class is shown here. Notice the @goal javadoc annotation. This is where we specify the name of the goal that we are implementing. Since we specify "@goal howdy-world", the goal that we are implementing with the HowdyMojo class is called "howdy-world". As we just mentioned, the HowdyMojo class is the Java class that implements this "howdy-world" goal that we just specified. Our "HowdyMojo" is a maven Mojo since it extends "AbstractMojo", which we have a reference to due to the "maven-plugin-api" dependency. The actual goal implementation code is placed in the "execute" method of the Mojo. The execute method gets a Log via getLog() and then calls the info method on it to output a "Hi there!!!" message to the console via the Log at "INFO" level. HowdyMojo.javapackage com.maventest; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; /** * This goal will say a message. * * @goal howdy-world */ public class HowdyMojo extends AbstractMojo { public void execute() throws MojoExecutionException { getLog().info("Hi there!!!"); } } (Continued on page 2) Related Tutorials:
|