Friday, April 24, 2009

Ant task to verify java version

If you're using Ant 1.4.1 or greater, the <condition> task works great. Here's an example that ought to give you what you need:

<project basedir="." default="test">

<target name="get-jvm">
<condition property="jvm.ok">
<not>
<or>
<equals arg1="${ant.java.version}" arg2="1.1"/>
</or>
</not>
</condition>
</target>

<target name="check-jvm" depends="get-jvm" unless="jvm.ok">
<fail message="Wrong JVM - ${ant.java.version}"/>
</target>
<target name="test" depends="check-jvm">
<echo message="JVM OK - ${ant.java.version}"/>
</target>
</project>


here is no "greater than" or "less than" logic, but there is certainly room to add that if you'd like to tackle a patch to Ant's code. With combinations of <equal>, <and>, <or>, and <not> you should be able to achieve the precise control you need. Be sure to leave it open ended for JVM 1.4 and up (so you don't have to touch your build file when upgrading JVM's) - thats why I constructed my example with a <not> and <or>.

No comments:

Post a Comment