Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a property has value in Ant

Tags:

ant

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.

like image 848
KK99 Avatar asked Feb 09 '12 08:02

KK99


People also ask

How do I override property value in ant?

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.

Where are ant properties set?

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.


1 Answers

You can use conditions using the <fail> task:

<fail message="Property &quot;foo&quot; 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 &quot;foo&quot; 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 &quot;foo&quot; 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> 
like image 163
David W. Avatar answered Oct 03 '22 23:10

David W.