I have an Ant XML file which I use for build.
I have 3 properties. I want to break the build if these properties does not contain any value. Also I want to break the build if the value is empty.
How can I do this in Ant?
I a using Ant and not Ant-contrib.
You can use conditions using the
<fail>
task:This is equivalent to saying
if (not set ${foo} or ${foo} = "")
is pseudocode. You have to read the XML conditions from the inside out.You could have used the
<unless>
clause on the<fail>
task if you only cared whether or not the variable was set, and not whether it has an actual value.However, this won't fail if the property is set, but has no value.
There's a trick that can make this simpler
Remember that I can't reset a property! If
${foo}
already has a value, the<property>
task above won't do anything. This way, I can eliminate the<isset>
condition. It might be nice since you have three properties:I'm on an older version of Ant, so isset wasn't available. Instead I used the following notation with the double $ in the equals.
With Ant addon Flaka you may use patterns like :
Concrete check for emptyness :
A complete example, also using some equals check with 'eq' (opposite would be 'neq'):
output with ant -f build.xml -Dbuildtype=prod
or
ant -f build.xml -Dbuildtype=prod -Ddummybuild=whatever
output with typo => ant - build.xml -Dbuildtype=testt
output with ant -f build.xml -Ddummybuild=whatever
You could try using conditions... or creating a target with unless
Building on the other answers, this is my preferred form, as a Macro:
To Be used as:
For brevity you can collapse the two fail elements into one by using an
<or>
, but I prefer my error messages to treat me like I cannot think for myself ;)