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="config.name" defaultvalue="${config.name.default}" />

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="config.name" defaultvalue="${config.name.default}" />
</target>

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

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

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 config.name property on the ant command
line.

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

Calling ant with the following will not prompt the user for the config.name:

ant compile -Dconfig.name=foo

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 CoffeaElectronica.com content is copyright © 2016 Christopher J. Stehno and available under a Creative Commons Attribution-ShareAlike 4.0 International License.