How do I attach a plugin goal to a particular phase of a maven lifecycle?
Author: Deron Eriksson
Description: This maven tutorial describes how to download a plugin from a remote Archiva repository.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)


In other tutorials, we examined the phases of the clean, default (build), and site lifecycles. It is possible to attach a goal to one of these phases so that it is executed when the phase is executed. In another tutorial, we created a plugin called "maven-howdy-plugin" containing the goal "howdy-world". Here, we will see how to attach this goal to the "validate" phase of the mavenSW default lifecycle.

The following fragment of a pom.xml file shows how to attach the "howdy-world" goal to the "validate" lifecycle phase. To do this, we specify the executions.execution.goals.goal to be "howdy-world", and we specify the executions.execution.phase to be "validate".

Plugin entry from pom.xml

...
	<plugin>
		<groupId>com.maventest</groupId>
		<artifactId>maven-howdy-plugin</artifactId>
		<version>1.0-SNAPSHOT</version>
		<executions>
			<execution>
				<goals>
					<goal>howdy-world</goal>
				</goals>
				<phase>validate</phase>
			</execution>
		</executions>
	</plugin>
...

Here is the pom.xml file for a project called "aproject". It contains the plugin code to attach "howdy-world" to the "validate" phase. The pluginRepositories section was used to download the plugin from an ArchivaS remote snapshots repository.

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>
						<goals>
							<goal>howdy-world</goal>
						</goals>
						<phase>validate</phase>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
	<pluginRepositories>
		<pluginRepository>
			<id>archiva.snapshots</id>
			<url>http://192.168.1.7:8081/archiva/repository/snapshots</url>
		</pluginRepository>
	</pluginRepositories>
</project>

To test, I'll perform a "mvn validate" on the "aproject" project.

Executing 'mvn validate' on 'aproject'

The goal successfully executed for the "validate" phase, as shown here in the console output.

Console output from 'mvn validate' on 'aproject'

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building aproject
[INFO]    task-segment: [validate]
[INFO] ------------------------------------------------------------------------
[INFO] snapshot com.maventest:maven-howdy-plugin:1.0-SNAPSHOT: checking for updates from archiva.snapshots
[INFO] [howdy:howdy-world {execution: default}]
[INFO] Hi there!!!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Tue Feb 19 02:18:43 PST 2008
[INFO] Final Memory: 1M/2M
[INFO] ------------------------------------------------------------------------