Search code examples
antant-contrib

ant-contrib - if/then/else task


I am using ant, and I have a problem with if/then/else task, (ant-contrib-1.0b3.jar). I am running something that can be simplified with build.xml below.

I am expecting to obtain from 'ant -Dgiv=Luke' the message

input name: Luke
should be overwritten with John except for Mark: John

but it seems property "giv" is not overwritten inside if/then/else..

input name: Luke
should be overwritten with John except for Mark: Luke

Is it depending from the fact I am using equals task with ${giv} ? Otherwise what is wrong in my code?

build.xml CODE:

<project name="Friend" default="ifthen" basedir=".">

<property name="runningLocation" location="" />
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
    <classpath>
        <pathelement location="${runningLocation}/antlib/ant-contrib-1.0b3.jar" />
    </classpath>
</taskdef>

<target name="ifthen">
<echo message="input name: ${giv}" />
<if>
    <equals arg1="${giv}" arg2="Mark" />
    <then>
    </then>
    <else>
        <property name="giv" value="John" />
    </else>
</if>
<echo message="should be overwritten with John except for Mark: ${giv}" />
</target>
</project>

Solution

  • Ant Properties are very hard to overwrite (if not impossible). What you need is a Variable. These are also defined in the Ant Contrib JAR.

    Editing your example:

      <target name="ifthen"> 
        <var name="Evangelist" value="${giv}" />
        <echo message="input name: ${Evangelist}" />
        <if>
          <equals arg1="${Evangelist}" arg2="Mark" />
          <then>
          </then>
          <else>
            <var name="Evangelist" value="John" />
          </else>
        </if>   
        <echo message="should be overwritten with John except for Mark: ${Evangelist}" />
     </target>