Ant Input Prompting and Private Targets

21 October 2008 ~ blog java ant

I have found the Ant input tag useful lately for setting up runtime parameters of an Ant build.

We have a few different server configuration settings that vary based on which server the artifact is being built for
and the input tag makes this really easy:

<input message="Enter configuration name: " addproperty="" defaultvalue="${}" />

The downside of this is that it will prompt you to enter this every time you run the build, which can become
annoying and really prohibits automated building. This is where the unless attribute of the target tag
comes into play. First create a private target (one whose name starts with "-") that will prompt for the config name:

<target name="-prompt-for-config">
    <input message="Enter configuration name: " addproperty="" defaultvalue="${}" />

Then add the unless attribute to check for the presence of the property:

<target name="-prompt-for-config" unless="">
    <input message="Enter configuration name: " addproperty="" defaultvalue="${}" />

which will cause this task to be run only if the specified property is not set. The you can have other tasks depend
on this private task, which will only run if you have not specified the property on the ant command

<target name="compile" depends="-prompt-for-config" description="Compiles the java sources.">
    <!-- do stuff -->

Calling ant with the following will not prompt the user for the

ant compile

I have used this in a few places now to make the build a bit more flexible, such as for doing server deployments,
artifact installations, etc. It is a handy ant trick to keep in mind.

Creative Commons License content is copyright © 2016 Christopher J. Stehno and available under a Creative Commons Attribution-ShareAlike 4.0 International License.