How to compare two properties with numerical values?

How do you know which of the two numerical properties is the largest?

Here's how to verify that two are equal:

<condition property="isEqual"> <equals arg1="1" arg2="2"/> </condition> 
+4
source share
3 answers

The Ant script task allows you to implement the task in a scripting language. If you have JDK 1.6 installed, Ant can execute JavaScript without any additional dependent libraries. For example, this JavaScript reads the value of the Ant property, and then sets another Ant property, depending on the 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> 
+7
source

Unfortunately, Ant's built-in task condition does not have an IsGreaterThan element. However, you can use the IsGreaterThan condition available in Ant-Contrib . Another option is to derive your own task for more than comparison. I would prefer the first, because it is simpler and faster, and you also get many other useful tasks from Ant-Contrib .

+4
source

If you do not want (or cannot) use the Ant -Contrib libraries, you can define the compare task using javascript:

 <!-- returns the same results as Java 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 as follows:

 <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> 
+2
source

Source: https://habr.com/ru/post/898416/


All Articles