Tuesday, January 19, 2010

To abort an ant build if a property is not set

The easiest way to abort an Ant - http://ant.apache.org/ - build if a user-defined property (e.g. password, filename, etc) is not set is to use the task with a condition element as follows

<fail message="@{propertyName} is not set">
<condition>
<not>
<isset property="@{propertyValue}" />
</not>
</condition>
</fail>

To test a range of properties, use this code snippet to create a macro:

<macrodef name="testProperty">
<attribute name="propertyName" />
<attribute name="propertyValue" />
<sequential>
<fail message="@{propertyName} is not set">
<condition>
<not>
<isset property="@{propertyValue}" />
</not>
</condition>
</fail>
</sequential>
</macrodef>

then simply call this macro with each value you want to test
<testProperty propertyName="property1" propertyValue="${property1}" />
<testProperty propertyName="property2" propertyValue="${property2}" />
<testProperty propertyName="property3" propertyValue="${property3}" />
<testProperty propertyName="property4" propertyValue="${property4}" />
<testProperty propertyName="property5" propertyValue="${property5}" />

No comments: