Nesting an islessthan within an if task - Ant

807 views Asked by At

I have an ant task like so:

<target name="mathWala" description="Some sample math operations.">
        <if>
            <islessthan arg1="${op1}" arg2="${limit}"/>
            <then>
                <echo>${op1} is less than ${limit}</echo>
                <antcall target="adder"></antcall>
            </then>
        </if>
    </target>

    <target name="adder">
        <math result="result" operand1="${op1}" operation="+" operand2="1" datatype="int"/>
        <echo>result = ${result}</echo>
    </target>

When I run the above task, the error I get is:
if does'nt support the nested "islessthan" element.

Can I use the <if> and <islessthan> statements together? I have gone through the documentation and did not find anything to suggest otherwise.

2

There are 2 answers

0
Rebse On

This is one of several bugs (for other oddities see here) in the latest version 1.0b3 of antcontrib,
but <if><islessthen ...> will work with version antcontrib 1.0b2 .
Alternatively you may use a macrodef with builtin javascript engine (contained in jdk => 1.6.0_06) instead of using target + antcall which is considered bad practice.
Here's some snippet with adapted version of an answer to another question:

<project>

<property name="foo" value="23"/>
<property name="limit" value="22"/>

<macrodef name="math">
 <attribute name="operation"/>
 <attribute name="operator1"/>
 <attribute name="operator2"/>
 <attribute name="result"/>
 <attribute name="when"/>
 <sequential>
 <script language="javascript">
   // note => use attribute @{when} without ''  !
   if(eval(@{when})) {
    tmp = 0;
    switch ("@{operation}") {
    case "+" :
      tmp = parseInt("@{operator1}") + parseInt("@{operator2}");
      break;
    case "-" :
      tmp = parseInt("@{operator1}") - parseInt("@{operator2}");
      break;
    case "*" :
      tmp = parseInt("@{operator1}") * parseInt("@{operator2}");
      break;
    case "/" :
      tmp = parseInt("@{operator1}") / parseInt("@{operator2}");
      break;
    }
   project.setProperty("@{result}", tmp);
   } else {
     println("Condition: @{when} false !");
   }
 </script>
 </sequential>
</macrodef>

<math operation="/" operator1="${foo}" operator2="11" result="foooo" when=" ${foo} &lt; ${limit} "/>

</project>

Intentionally used different math operations like -,+,*,/ instead of using eval(...) to restrict the operation types provided by macrodef.

0
Mark O'Connor On

The "if" task is not a standard ANT feature. Conditional execution of an ANT target can be accomplished as follows:

<project name="demo" default="build">

  <property name="opt1"  value="20"/>
  <property name="limit" value="50"/>

  <condition property="is.less.than">
    <scriptcondition language="javascript"><![CDATA[
    self.setValue(Number(project.getProperty("opt1")) < Number(project.getProperty("limit")));
    ]]></scriptcondition>
  </condition>

  <target name="build" if="is.less.than">
    <echo message="${opt1} is less than ${limit}"/>
  </target>

</project>