|
3.2. Getting Input from the UserAt 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.
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.
|
|