How can I compare two properties with numeric values?

前端 未结 3 1252
一个人的身影
一个人的身影 2021-01-05 21:21

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

Here\'s how to check wheather two are equal:



        
相关标签:
3条回答
  • 2021-01-05 21:45

    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.

    0 讨论(0)
  • 2021-01-05 21:50

    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>
    
    0 讨论(0)
  • 2021-01-05 21:50

    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>
    
    0 讨论(0)
提交回复
热议问题