Many ant scripts I write use a default value, and these default values are almost exclusive. I.e. only occasionally will I want to run it without the default values.
A lot of times, these scripts take enough time that it makes sense to go do something else while they run, like getting coffee or using the Little Developer's Room. Of course, if there's a prompt on it, and you forgot it, well, you're SOL.
Is there any way I can put a timeout on the prompt so if it isn't entered in, oh let's say 30 seconds, that it just accepts the default value so that when I get back to my workstation I have my war/jar/whatever ready to go? Something like
<input addproperty="branch.tag"
defaultvalue="dev"
timeout="30000">
Which branch would you like to build?
</input>
Now obviously this timeout feature doesn't exist, but you get the idea of what I'm trying to accomplish.
Rather than timing out the input prompts, you could configure the build to run fully automated by supplying the default input values in a properties file.
Which\ branch\ would\ you\ like\ to\ build?=dev
To switch between interactive and automated builds, the type of input handler to use could be specified when invoking Ant:
$ ant -Dhandler.type=propertyfile
$ ant -Dhandler.type=default
The input handler would need to be specified using the nested <handler>
element.
<input addproperty="branch.tag" defaultvalue="dev"
message="Which branch would you like to build?">
<handler type="${handler.type}" />
</input>
The last step is to specify the properties file for the PropertyFileInputHandler
by defining the ant.input.properties
system property.
export ANT_OPTS=-Dant.input.properties=default.properties
<taskdef name="trycatch" classname="net.sf.antcontrib.logic.TryCatchTask">
<classpath>
<pathelement location="/your/path/to/ant-contrib.jar"/>
</classpath>
</taskdef>
<macrodef name="input-timeout">
<attribute name="addproperty" />
<attribute name="defaultvalue" default="" />
<attribute name="handlertype" default="default" />
<attribute name="message" default="" />
<attribute name="timeout" default="30000" />
<text name="text" default="" />
<sequential>
<trycatch>
<try>
<parallel threadcount="1" timeout="@{timeout}">
<input addproperty="@{addproperty}"
defaultvalue="@{defaultvalue}"
message="@{message}">
<handler type="@{handlertype}" />
@{text}
</input>
</parallel>
</try>
<catch>
<property name="@{addproperty}" value="@{defaultvalue}" />
</catch>
</trycatch>
</sequential>
</macrodef>
<target name="test-timeout">
<input-timeout addproperty="branch.tag" defaultvalue="dev"
message="Which branch would you like to build?"
timeout="5000" />
<echo message="${branch.tag}" />
</target>
Implementation left as an exercise to the reader.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With