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


With mavenSW, a property can be accessed by the standard ${property_name} format. 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 creates a property called "testproperty" with the value "This is a test property". The pom.xml fragment binds the antrun:goal to the validate build lifecycle phase. It uses the "echo" Ant task to display the value of the "testproperty" property.

Fragment of pom.xml to create and display 'testproperty' property

	<properties>
		<testproperty>This is a test property</testproperty>
	</properties>
	<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 'testproperty' property</echo>
								<echo>[testproperty] ${testproperty}</echo>
							</tasks>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

Performing a "mvn validate" on the "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 'testproperty' property
     [echo] [testproperty] This is a test property
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: < 1 second
[INFO] Finished at: Mon Feb 11 14:29:11 PST 2008
[INFO] Final Memory: 2M/5M
[INFO] ------------------------------------------------------------------------

The value of the "testproperty" property 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>

	<properties>
		<testproperty>This is a test property</testproperty>
	</properties>
	<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 'testproperty' property</echo>
								<echo>[testproperty] ${testproperty}</echo>
							</tasks>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

</project>