3

I know that there is ant-contrib, which provides "if-else" logic for ant. But I need to achieve the same without ant-contrib. Is that possible?

Pseudocode which I need to work:

if(property-"myProp"-is-true){
  do-this;
}else{
  do-that;
}

Thank you!

Justin Johnson
  • 30,978
  • 7
  • 65
  • 89
java.is.for.desktop
  • 10,748
  • 12
  • 69
  • 103

2 Answers2

4

I would strongly recommend using ant-contribs anyway but if you are testing a property that always has a value I would look into using an ant macro parameter as part of the name of a new property that you then test for

<macrodef name="create-myprop-value">
 <attribute name="prop"/>
 <sequential>
      <!-- should create a property called optional.myprop.true or -->
      <!-- optional.myprop.false -->
      <property name="optional.myprop.@{prop}" value="set" />
 </sequential>
</macrodef>

<target name="load-props">
  <create-myprop-value prop="${optional.myprop}" /> 
</target>

<target name="when-myprop-true" if="optional.myprop.true" depends="load-props">
...
</target>

<target name="when-myprop-false" if="optional.myprop.false" depends="load-props">
...
</target>

<target name="do-if-else" depends="when-myprop-true,when-myprop-false">
...
</target>
Michael Rutherfurd
  • 13,815
  • 5
  • 29
  • 40
  • Strictly speaking, the "else" target could be coded as unless="optional.myprop.true" rather than if="optional.myprop.false". The macro could also be genericised to pass the name of the property as a parameter as well. – Michael Rutherfurd Jul 12 '10 at 07:15
3

You can put an "if" attribute in your targets.

<target name="do-this-when-myProp-is-true" if="myProp">
...
</target>

This will fire only if "myProp" is set. You'll need to define myProp elsewhere such that it's set if you want this target to fire & not if you don't. You can use unless for the alternate case:

<target name="do-this-when-myProp-is-false" unless="myProp">
...
</target>
G__
  • 7,003
  • 5
  • 36
  • 54