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.
Ant Properties are set once and then can never be overridden. That's why setting any property on the command line via a -Dproperty=value will always override anything you've set in the file; the property is set and then nothing can override it. This way: Anything set at the command line takes precedence over build.
The <property> task is used to set the Ant properties. The property value is immutable, once the value is set you cannot change it. To set a property to a specific value you use Name/value assignment.
You can use conditions using the <fail>
task:
<fail message="Property "foo" needs to be set to a value"> <condition> <or> <equals arg1="${foo}" arg2=""/> <not> <isset property="foo"/> </not> </or> </condition>
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.
<fail message="Property "foo" needs to be set" unless="foo"/>
However, this won't fail if the property is set, but has no value.
There's a trick that can make this simpler
<!-- Won't change the value of `${foo}` if it's already defined --> <property name="foo" value=""/> <fail message="Property "foo" has no value"> <condition> <equals arg1="${foo}" arg2=""/> </condition> </fail>
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:
<property name="foo" value=""/> <property name="bar" value=""/> <property name="fubar" value=""/> <fail message="You broke the build, you dufus"> <condition> <or> <equals arg1="${foo}" arg2=""/> <equals arg1="${bar}" arg2=""/> <equals arg1="${fubar}" arg2=""/> </or> </condition> </fail>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With