Section 3.2. Getting Input from the User


3.2. Getting Input from the User

At some point in a build, you may need input from the user. For example, you might want to ask before deleting a directory with the delete task.

The delete task is covered in detail in Chapter 4.


To get input from the user, use the input task, which creates a new property based on user input.

Example 3-1 puts input to work. This build file asks whether it's OK to delete the bin directory, and if it's not, the build fails. It queries the user using the input task, and creates a new property, do.delete, based on the user's input.

Example 3-1. Using the input task (ch03/input/build.xml)
<?xml version="1.0" ?> <project default="main">     <property name="message" value="Building the .jar file." />     <property name="src" location="source" />     <property name="output" location="bin" />     <target name="main" depends="init, compile, compress">         <echo>             ${message}         </echo>     </target>        <target name="init">         <input             message="Deleting bin directory OK?"             validargs="y,n"             addproperty="do.delete"         />         <condition property="do.abort">             <equals arg1="n" arg2="${do.delete}"/>         </condition>         <fail if="do.abort">Build aborted.</fail>         <delete dir="${output}" />         <mkdir dir="${output}" />     </target>        <target name="compile">         <javac srcdir="${src}" destdir="${output}" />     </target>      <target name="compress">         <jar destfile="${output}/Project.jar" basedir="${output}" includes="*.class" />   </target> </project>

Here's what you see when you run Ant using this build file:

%ant Buildfile: build.xml init:     [input] Deleting bin directory OK?(y,n) y     [delete] Deleting directory /home/steven/input/bin     [mkdir] Created dir: /home/steven/input/bin compile:     [javac] Compiling 1 source file to /home/steven/input/bin compress:     [jar] Building jar: /home/steven/input/bin/Project.jar main:     [echo]     [echo]             Building the .jar file.     [echo] BUILD SUCCESSFUL Total time: 5 seconds

The attributes of this task appear in Table 3-3.

Table 3-3. The input task's attributes

Attribute

Description

Required

addproperty

Specifies the name of the property to create, using the input from the user.

No

defaultvalue

Specifies the default value to use for the created property; this default will be used if no input is read.

No

message

Specifies the prompt you want to display to prompt the user to enter text.

No

validargs

Specifies the input values you consider valid, separated by commas. If you use this attribute, the task will not allow input that doesn't match one of these values.

No




    Ant. The Definitive Guide
    Ant: The Definitive Guide, 2nd Edition
    ISBN: 0596006098
    EAN: 2147483647
    Year: 2003
    Pages: 115
    Authors: Steve Holzner

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net