How do I create a variable in Ant?
Author: Deron Eriksson
Description: This tutorial shows how to create an Ant variable task to overcome the unchangeable nature of an Ant property
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1


Page: < 1 2

(Continued from page 1)

The teamcakes-ant-util project consists of the Variable class and the build.xml file.

teamcakes-ant-util project

Variable is shown below. It extends the AntSW Task class. It has fields called 'name' and 'value', and there is a getter and setter for each. It features an execute method, which displays the name and the value associated with the name, and it sets these in the project's properties.

Variable.java

package com.cakes.ant;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

public class Variable extends Task {

	private String name = "";
	private String value = "";

	public Variable() {
		super();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public void execute() throws BuildException {
		if ((name == null) || ("".equals(name))) {
			System.out.println("The variable name is null");
			return;
		}
		System.out.println("Setting '" + name + "' variable to '" + value + "'");
		getProject().setProperty(name, value);
	}

}

The build.xml file for teamcakes-ant-util has a taskdef element that defines our 'var' task, specifying that the task class is com.cakes.ant.Variable. Since we are working on the class in the same project as our build file, we can specify that the classpathW is our bin directory (which contains the Variable.class file). The build.xml file features three targets: property-test, variable-test, and variable-test-2.

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="teamcakes-ant-util" default="" basedir=".">

	<taskdef name="var" classname="com.cakes.ant.Variable" classpath="bin" />

	<target name="property-test">
		<property name="myProp" value="value1" />
		<echo>myProp is ${myProp}</echo>
		<property name="myProp" value="value2" />
		<echo>myProp is ${myProp}</echo>
	</target>

	<target name="variable-test">
		<var name="myVar" value="value1" />
		<echo>myVar is ${myVar}</echo>
		<var name="myVar" value="value2" />
		<echo>myVar is ${myVar}</echo>
	</target>

	<target name="variable-test-2" depends="variable-test">
		<echo>myVar is ${myVar}</echo>
	</target>

</project>

To test our targets, we can add the teamcakes-ant-util file to the EclipseSW Ant view.

Eclipse Ant View

The execution of the 'property-test' target is shown below. As you can see, even though we tried calling the property element twice with the same name but different values, the second call didn't change the value.

Buildfile: C:\projects\workspace\teamcakes-ant-util\build.xml
property-test:
     [echo] myProp is value1
     [echo] myProp is value1
BUILD SUCCESSFUL
Total time: 203 milliseconds

The execution of 'variable-test' is shown below. As you can see, unlike an Ant 'property', the value of the 'myVar' variable does indeed change.

Buildfile: C:\projects\workspace\teamcakes-ant-util\build.xml
variable-test:
      [var] Setting 'myVar' variable to 'value1'
     [echo] myVar is value1
      [var] Setting 'myVar' variable to 'value2'
     [echo] myVar is value2
BUILD SUCCESSFUL
Total time: 203 milliseconds

As a final example, the 'variable-test-2' target is shown below. It depends on 'variable-test', and we can see that the value of 'myVar' does indeed change. In addition, we verify that the value of 'myVar' is accessible from 'variable-test-2'.

Buildfile: C:\projects\workspace\teamcakes-ant-util\build.xml
variable-test:
      [var] Setting 'myVar' variable to 'value1'
     [echo] myVar is value1
      [var] Setting 'myVar' variable to 'value2'
     [echo] myVar is value2
variable-test-2:
     [echo] myVar is value2
BUILD SUCCESSFUL
Total time: 203 milliseconds

In another tutorial, I'll package up this Variable task class into a jarW file, and I'll set up my multi-project Ant build file to use the new task.

Page: < 1 2