Chapter 9. Variable Task

The Variable task provides a mutable property to Ant and works much like variable assignment in Java. This task is similar to the standard Ant Property task, except that THESE PROPERTIES ARE MUTABLE. While this goes against the standard Ant use of properties, occasionally it is useful to be able to change a property value within the build. In general, use of this task is DISCOURAGED, and the standard Ant Property should be used if possible. Having said that, in real life I use this a lot.

To use this task in your build files, include a task definition like this:


    <taskdef name="var" classname="ise.antelope.tasks.Variable"/>
   

Variables can be set individually or loaded from a standard properties file. A 'feature' of variables is that they can override properties, but properties cannot override variables. So if an already established property exists, its value can be reassigned by use of this task.

Table 9.1. Variable Task Attributes

AttributeDescriptionDefaultRequired
nameThe name of the property to set.NoneYes, unless 'file' is used.
valueThe value of the property.""No
fileThe name of a standard properties file to load variables from.NoneNo

In the following example, the property x is first set to "6", then evaluated by the if, and reassigned the value "12". The echo task will print out 12.


    <var name="x" value="6"/>
    <if name="x" value="6">
        <var name="x" value="12"/>
    </if>
    <echo>${x}</echo>   <!-- will print 12 -->

The following shows some more uses of the Variable task. It is especially handy for property appending. Notice a couple of things: the property task can't override a var value, however, if the var value is set to "", then it can as in the case of the format example.


    <var name="x" value="6"/>
    <echo>x = ${x}</echo>   <!-- print: 6 -->

    <var name="x" value="12"/>
    <echo>x = ${x}</echo>   <!-- print: 12 -->

    <var name="x" value="6 + ${x}"/>
    <echo>x = ${x}</echo>   <!-- print: 6 + 12 -->

    <var name="str" value="I "/>
    <var name="str" value="${str} am "/>
    <var name="str" value="${str} a "/>
    <var name="str" value="${str} string."/>
    <echo>${str}</echo>     <!-- print: I am a string. -->

    <var name="x" value="6"/>
    <echo>x = ${x}</echo>   <!-- print: 6 -->

    <property name="x" value="12"/>
    <echo>x = ${x}</echo>   <!-- print: 6 (property can't override) -->

    <var name="x" value="blue"/>
    <tstamp>
        <format property="x" pattern="EEEE"/>
    </tstamp>
    <echo>Today is ${x}.</echo> <!-- print: Today is blue. -->

    <var name="x" value=""/>
    <tstamp>
        <format property="x" pattern="EEEE"/>
    </tstamp>
    <echo>Today is ${x}.</echo> <!-- print: Today is Friday. -->


The next example shows Variable, If, Assert, and Try working together to make sure e-mail is sent from the right address and that if the mail fails to be sent for any reason, the build will not fail.

    
   <var name="valid_email" value="false"/>
   <if name="email_from" value="buildteam@mycompany.com">
      <var name="valid_email" value="true"/>
   </if>
   <if name="email_from" value="buildsite@mycompany.com">
      <var name="valid_email" value="true"/>
   </if>
   <assert name="valid_email" value="true" failonerror="false">
      <try>
         <mail from="${email_from}" tolist="${email_to}"
            message="New release available"/>
      </try>
   </assert>