Search This Blog

Thursday, May 29, 2014

Fail an ANT build if a property is not set

Any number of properties can be used in ANT build.xml. They can be environment variables or variables set in a properties file.

There is an ability to check if required properties are set or not i.e. included or not in the properties file. Also we can match them to a regular expression to see of they are empty or matching as per the requirement. If they do not match the requirement, the build can be made to fail. There is a fail task provided by the ant library to fail the build. Add this as first task before adding any cutom tasks.

Including Properties File

You can include a properties file or environmental variables in build.xml in the below way
    <property file="build.properties"/>
    <property environment="env"/>



Reading Properties

Particular property can be used as per this format where sf.username is the property and echo is the target.
<echo message="username ---> ${sf.username}" />

Testing property

Method1
<fail message="Properties username and password not set properly">
<condition>
<or>
<isset property="sf.username"/>
<isset property="sf.password"/>
</or>
</condition>
</fail>
This checks if the property 'sf.username' is present in the properties file or not. If it is missing or the trimmed value is blank, the build fails giving the message that is configured.

Method 2
<fail message="Properties username and password not set properly">
<condition>
<or>
<equals arg1="${sf.username}" arg2="" trim="true" />
<equals arg1="${sf.password}" arg2="" trim="true" />
</or>
</condition>
</fail>
This checks if the property 'sf.username' is present in the properties file or not. It fails if the trimmed vaue is empty or not but doesn't check if the value is missing from the properties file. This can be used of the properties need to be of a pattern.

No comments:

Post a Comment