jeremychan
jeremychan

Reputation: 4459

if else for ant script

hi current i have this ant script:

        <target name="Cleanup Snapshots" description="Cleanup TrueCM snapshots">
        <echo>Executing: ${TrueCM_App}\ssremove.exe  -h${TrueCM_Host} "${TrueCM_New_SS}"</echo>
        <exec executable="${TrueCM_App}\ssremove.exe"   failonerror="${TrueCM_Failon_Exec}">
            <arg line="-h ${TrueCM_Host}"/>
            <arg line='-f "${TrueSASE}/${Product_version}/${name}"'/>
        </exec>


    </target>

what this script does is that it will execute ssremove.exe with some parameters as shown.

however, this script above is only valid when the parameter ${Product_version} contains the word "Extensions" for example 6.00_Extensions

Else if they dont contain "Extensions" the script should look like this:

    <target name="Cleanup Snapshots" description="Cleanup TrueCM snapshots">
        <echo>Executing: ${TrueCM_App}\ssremove.exe  -h${TrueCM_Host} "${TrueCM_New_SS}"</echo>
        <exec executable="${TrueCM_App}\ssremove.exe"   failonerror="${TrueCM_Failon_Exec}">
            <arg line="-h ${TrueCM_Host}"/>
            <arg line='-f "${TrueSASE}/${name}"'/>
        </exec>


    </target>

so my question is how should i add the if else statements that contain exactly the line "Extensions"? or how do i check if "Extensions" word is present?

Upvotes: 2

Views: 13265

Answers (2)

Aaron Digulla
Aaron Digulla

Reputation: 328556

Just to outline a solution if you can't add a custom library to your Ant build. Note that you need a pretty recent version of Ant (>= 1.7, I think).

First, you need to put the result of the test in a property (use a condition with contains; see the Ant Manual). Then you can use that property in exec to skip it when the property is true.

<condition property="hasExtensions">
    <contains string="${Product_version}" substring="Extensions">
</condition>

<exec ... unless="hasExtensions">
    ...
</exec>

Upvotes: 1

oers
oers

Reputation: 18704

There is an ant library : antlib ( http://ant-contrib.sourceforge.net)

which supports if/else statements ( http://ant-contrib.sourceforge.net/tasks/tasks/index.html)

Examples from the site:

<if>
 <equals arg1="${foo}" arg2="bar" />
 <then>
   <echo message="The value of property foo is bar" />
 </then>
 <else>
   <echo message="The value of property foo is not bar" />
 </else>
</if>


<if>
 <equals arg1="${foo}" arg2="bar" />
 <then>
   <echo message="The value of property foo is 'bar'" />
 </then>

 <elseif>
  <equals arg1="${foo}" arg2="foo" />
  <then>
   <echo message="The value of property foo is 'foo'" />
  </then>
 </elseif>


 <else>
   <echo message="The value of property foo is not 'foo' or 'bar'" />
 </else>
</if>

Upvotes: 6

Related Questions