Udi
Udi

Reputation: 1110

Ant strings comparison

I have a script in ant, and I need to compare 2 strings lexicographically. something like:

 "1.2.3".compareTo("1.2.4")

I can't find a way to do so... Any ideas? I'm using ant 1.8, and ant-contrib.

Thanks

Upvotes: 3

Views: 2299

Answers (1)

FailedDev
FailedDev

Reputation: 26930

For this solution to work you will need to have your JAVA_HOME pointing to JRE 1.6 or later.

<project name="test" default="test">
<scriptdef name="compare" language="javascript">
     <attribute name="lhs" />
     <attribute name="rhs" />
     <attribute name="property" />
     <![CDATA[
       var lhs = attributes.get("lhs");
       var rhs = attributes.get("rhs");
       project.setProperty(attributes.get("property"), lhs > rhs);
     ]]>
</scriptdef>

<target name="test">
    <compare lhs="1.2.3" rhs="1.2.4" property="result"/>
    <echo message="Result is : ${result}"/>
</target>
</project>

Output :

test:
 [echo] Result is : false

Upvotes: 4

Related Questions