How do I accept text input using Ant?
Author: Deron Eriksson
Description: This Ant tutorial describes how to accept text input from a user using the input task.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1


Ant's input task makes it very easy to accept text input, as we can demonstrate with the following build.xml file.

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="ant-demo" default="howdy" basedir=".">
	<target name="howdy">
		<input message="What's your name?" addproperty="your-name" validargs="Bob,Fred" />
		<echo>What's up, ${your-name}?</echo>
	</target>
</project>

This simple build.xml file has one target called 'howdy', which contains the input task. If we run the 'howdy' target in EclipseSW, we are greeted by the following popup window.

Ant Input Request

Notice that the input task has a validargs attribute that lets us restrict text input. In this example, the input needs to be 'Bob' or 'Fred', or the OK button is deactivated. If we type 'Jane', we can't click OK.

Ant Input Request

If we type 'Bob', the OK button is activated.

Ant Input Request

If we click OK, we see that "What's up, Bob?" in echoed to the console window.

Buildfile: C:\projects\workspace\ant-demo\build.xml
howdy:
     [echo] What's up, Bob?
BUILD SUCCESSFUL
Total time: 11 seconds

The input task is a very useful, easy way to add user interactivity to the AntSW targets that you create.