Tuesday, February 8, 2011

Ant and the available task - what if something is not available?

When I use the task, the property is only set to TRUE if the resource (say file) is available. If not, the property is undefined.

When I print the value of the property, it gives true if the resource was available, but otherwise just prints the property name.

Is there a way to set the property to some value if the resource is not available? I have tried setting the property explicitly before the available check, but then ant complains:

[available] DEPRECATED -  used to override an existing property.
[available]   Build file should not reuse the same property name for different values.
  • You can use a condition in combination with not:

    http://ant.apache.org/manual/CoreTasks/condition.html

      <condition property="fooDoesNotExist">
        <not>
          <available filepath="path/to/foo"/>
        </not>
      </condition>
    
  • The reason for this behaviour are the if/unless-attributes in targets. The target with such an attribute will be executed if/unless a property with the name is set. If it is set to false or set to true makes no difference. So you can use the available-task to set (or not) a property and based on this execute (or not) a task. Setting the property before the available-task is no solution, as properties in ant are immutable, they cannot be changed once set.

    There are three possible solutions, to set a property to a value if unset before:

    1. You use the available-task in combination with not.
    2. You create a task setting the property, that will be executed only if the property is unset (unless-attribute of task).
    3. You simply set the property after the call to available. As the property will only be changed if unset, this will do what you want.
    From Mnementh
  • <available filepath="/path/to/foo" property="foosThere" value="true"/>
    <property name="foosThere" value="false"/>
    

    The assignment of foosThere will only be successful if it has not already been set to true by your availability check.

    From

0 comments:

Post a Comment