Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I compare two properties with numeric values?

Tags:

ant

How can I find out which of two numeric properties is the greatest?

Here's how to check wheather two are equal:

<condition property="isEqual">
    <equals arg1="1" arg2="2"/>
</condition>
like image 622
Industrial Avatar asked Jan 09 '11 15:01

Industrial


3 Answers

The Ant script task allows you to implement a task in a scripting language. If you have JDK 1.6 installed, Ant can execute JavaScript without needing any additional dependent libraries. For example, this JavaScript reads an Ant property value and then sets another Ant property depending on a condition:

<property name="version" value="2"/>

<target name="init">
  <script language="javascript"><![CDATA[
    var version = parseInt(project.getProperty('version'));
    project.setProperty('isGreater', version > 1);
  ]]></script>

  <echo message="${isGreater}"/>
</target>
like image 55
Chin Huang Avatar answered Nov 11 '22 03:11

Chin Huang


Unfortunately, Ant's built in condition task does not have an IsGreaterThan element. However, you could use the IsGreaterThan condition available in the Ant-Contrib project. Another option would be to roll out your own task for greater than comparison. I'd prefer the former, because it's easier and faster, and you also get a host of other useful tasks from Ant-Contrib.

like image 4
AbdullahC Avatar answered Nov 11 '22 04:11

AbdullahC


If you don't want to (or cannot) use the Ant-Contrib libraries, you can define a compare task using javascript:

<!-- returns the same results as Java's compareTo() method: -->
<!-- -1 if arg1 < arg2, 0 if arg1 = arg2, 1 if arg1 > arg2 -->
<scriptdef language="javascript" name="compare">
    <attribute name="arg1" />
    <attribute name="arg2" />
    <attribute name="result" />
    <![CDATA[
    var val1 = parseInt(attributes.get("arg1"));
    var val2 = parseInt(attributes.get("arg2"));
    var result = (val1 > val2 ? 1 : (val1 < val2 ? -1 : 0));
    project.setProperty(attributes.get("result"), result);
    ]]>
</scriptdef>

You can use it like this:

<property name="myproperty" value="20" />
...
<local name="compareResult" />
<compare arg1="${myproperty}" arg2="19" result="compareResult" />
<fail message="myproperty (${myproperty}) is greater than 19!">
    <condition>
        <equals arg1="${compareResult}" arg2="1" />
    </condition>
</fail>
like image 2
user3151902 Avatar answered Nov 11 '22 02:11

user3151902