Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make an ant prompt use the default value if not entered within a given time?

Tags:

input

timeout

ant

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.

like image 269
corsiKa Avatar asked Feb 21 '23 00:02

corsiKa


1 Answers

Option 1: Configure build to run automated or interactive

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.

default.properties

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:

Automated build

$ ant -Dhandler.type=propertyfile

Interactive build

$ 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.

Linux

export ANT_OPTS=-Dant.input.properties=default.properties

Option 2: Use AntContrib Trycatch combined with Parallel in a macrodef

<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>

Option 3: Write a custom Ant task

Implementation left as an exercise to the reader.

like image 151
Christopher Peisert Avatar answered Feb 22 '23 12:02

Christopher Peisert