How do I display the value of a pom.xml element?
Author: Deron Eriksson
Description: This tutorial describes how to display the value of a pom.xml element.
Tutorial created using: Windows Vista || JDK 1.6.0_04 || Eclipse Web Tools Platform 2.0.1 (Eclipse 3.3.1)


With mavenSW, a pom.xml element can be accessed as a property, where the property name begins with "project.". Thus, a pom.xml element such as "artifactId" can be accessed via ${project.artifactId}.

The maven-antrun-plugin:run goal can be used to run AntSW tasks. We can use the "echo" Ant task to display a value of a property. The following fragment of a pom.xml file binds the antrun:goal to the validate build lifecycle phase. It uses the "echo" Ant task to display the value of the pom.xml artifactId element.

Fragment of pom.xml to display value of pom.xml artifactId element

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-antrun-plugin</artifactId>
				<version>1.1</version>
				<executions>
					<execution>
						<phase>validate</phase>
						<goals>
							<goal>run</goal>
						</goals>
						<configuration>
							<tasks>
								<echo>Displaying value of pom.xml element</echo>
								<echo>[project.artifactId] ${project.artifactId}</echo>
							</tasks>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

Performing a "mvn validate" on my "mytest" project generates the following console output:

Console output of 'mvn validate' on 'mytest' project

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building mytest
[INFO]    task-segment: [validate]
[INFO] ------------------------------------------------------------------------
[INFO] [antrun:run {execution: default}]
[INFO] Executing tasks
     [echo] Displaying value of pom.xml element
     [echo] [project.artifactId] mytest
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: < 1 second
[INFO] Finished at: Mon Feb 11 14:15:47 PST 2008
[INFO] Final Memory: 2M/5M
[INFO] ------------------------------------------------------------------------

As you can see, the value of the pom.xml artifactId element is properly displayed.

The "mytest" project's full pom.xml file is shown here.

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>mytest</artifactId>
	<packaging>jar</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>mytest</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>2.3</version>
			<scope>compile</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-antrun-plugin</artifactId>
				<version>1.1</version>
				<executions>
					<execution>
						<phase>validate</phase>
						<goals>
							<goal>run</goal>
						</goals>
						<configuration>
							<tasks>
								<echo>Displaying value of pom.xml element</echo>
								<echo>[project.artifactId] ${project.artifactId}</echo>
							</tasks>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

</project>