Tuesday, February 25, 2014

Nested if else in Apache Ant

Often we may have requirement to implement nested if in Apache Ant without using ant-contrib.

My requirement was to set a xml property by the following condition

1) If the property xml file is passed using -D parameter it should take the precedence
2) If I set an environment variable for property xml path it will take the 2nd preference
3) Above two conditions are not met it will take the property from a default location.

if( -Dparameter Set)
else
{
   if(Env variable set)
   {
  } else
 {
   //take from default
 }
}

This is how I accomplish the task above :

<target name="init-pls-config-xml"
         depends="init-pls-config-dparam,int-pls-config-osset"></target>
 <target name="init-pls-config-dparam" if="pls-config">
  <echo>*********** Using pls-config file as -D param : ${pls-config} *********** </echo>
  <xmlproperty file="${pls-config}" collapseattributes="true" keeproot="false"/>
 </target>
 <target name="int-pls-config-osset" unless="pls-config"
         depends="init-pls-config-osenvxml,init-pls-config-default"></target>
 <target name="init-pls-config-osenvxml" if="OS_ENV.PLS_ANT_CONFIG">
  <echo>*********** Using pls-config file from the environment variable : ${OS_ENV.PLS_ANT_CONFIG} *********** </echo>
  <xmlproperty file="${OS_ENV.PLS_ANT_CONFIG}" collapseattributes="true"
               keeproot="false"/>
 </target>
 <target name="init-pls-config-default" unless="OS_ENV.PLS_ANT_CONFIG">
  <echo>*********** Using pls-config file from the default location: ${OS_ENV.HOME}/pls-config.xml *********** </echo>
  <xmlproperty file="${OS_ENV.HOME}/pls-config.xml" collapseattributes="true"
               keeproot="false"/>
 </target>

No comments: