views:

50

answers:

2

Hello, everyone!

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!

+1  A: 

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>
Greg Harman
+1  A: 

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
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