How do I specify the phase of a lifecycle in a Mojo?
Author: Deron Eriksson
Description: This maven tutorial describes how to use an annotation in a Mojo java file to specify the phase of a maven lifecycle that we'd like to attach the goal to.
Tutorial created using: Windows Vista || JDK 1.6.0_04 || Eclipse Web Tools Platform 2.0.1 (Eclipse 3.3.1)


In another tutorial, we created a mavenSW plugin project called "maven-howdy-plugin" with a goal called "howdy-world", which is implemented by the HowdyMojo class, seen here:

HowdyMojo.java

package 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!!!");
	}
}

In the javadoc annotations, it's possible to specify a "@phase" which names a particular phase of a maven lifecycle to which a goal is bound. To demonstrate this, I created a new class called "HowdyMojo2", specified the "@goal" to be "howdy-world2" and the "@phase" to be "process-resources". I changed the info message slightly from that of HowdyMojo. The HowdyMojo2 class is shown below.

HowdyMojo2.java

package com.maventest;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;

/**
 * This goal will say a message.
 * 
 * @goal howdy-world2
 * @phase process-resources
 */
public class HowdyMojo2 extends AbstractMojo {

	public void execute() throws MojoExecutionException {

		getLog().info("Hi again...");
	}
}

To install the "maven-howdy-plugin" into my local maven repository so that other projects could use it, I performed a "mvn clean install" on the project.

I have a project called "aproject" that uses the "maven-howdy-plugin", as shown in aproject's pom.xml file, which we can see here. The pom.xml entry for maven-howdy-plugin specifies executions for both the "howdy-world" and "howdy-world2" goals. The "howdy-world" goal specifically is bound to the "validate" lifecycle phase, whereas no lifecycle phase is specified for "howdy-world2".

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>aproject</artifactId>
	<packaging>jar</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>aproject</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>com.maventest</groupId>
				<artifactId>maven-howdy-plugin</artifactId>
				<version>1.0-SNAPSHOT</version>
				<executions>
					<execution>
						<id>first</id>
						<goals>
							<goal>howdy-world</goal>
						</goals>
						<phase>validate</phase>
					</execution>
					<execution>
						<id>second</id>
						<goals>
							<goal>howdy-world2</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

Now, I'll perform a "mvn compile" on the "aproject" project. The following phases in the default lifecycle are executed as a result of the "mvn compile": validate, generate-sources, process-sources, generate-resources, process-resources, and compile. We should expect that the "howdy-world" goal will be executed during the "validate" phase of the default lifecycle and the "howdy-world2" goal will be executed during the "process-resources" phase of the default lifecycle. This is what we see in the console output, shown here:

Console output of 'mvn compile' on 'aproject' project

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building aproject
[INFO]    task-segment: [compile]
[INFO] ------------------------------------------------------------------------
[INFO] [howdy:howdy-world {execution: first}]
[INFO] Hi there!!!
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [howdy:howdy-world2 {execution: second}]
[INFO] Hi again...
[INFO] [compiler:compile]
[INFO] Nothing to compile - all classes are up to date
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Tue Feb 19 03:42:30 PST 2008
[INFO] Final Memory: 2M/6M
[INFO] ------------------------------------------------------------------------