When using ANT, how can I define a task only if I have a specific version of java?

I had a problem that a certain step in Ant can only be performed if Java 1.5 is installed on the build computer. The jar file was used in the task definition, which was compiled using 1.5, so working with the 1.4 virtual machine will throw an IncompatibleClassVersion exception.

I have to find a solution, in the meantime, so that this task works on this particular project, which requires 1.4, but the question came to me. How can I avoid defining this task and completing this optional step if I don't have a specific version of java?

I can use "if" or "except" tags for the target tag, but they check if the property is set or not. I would also like to have a solution that does not require additional libraries, but I do not know if the built-in functions in the standard are enough for such a task.

+5
source share
2 answers

The Java version is displayed through the ant.java.version property. Use a condition to set a property and complete a task only if it is true.

<?xml version="1.0" encoding="UTF-8"?>

<project name="project" default="default">

    <target name="default" depends="javaCheck" if="isJava6">
        <echo message="Hello, World!" />
    </target>

    <target name="javaCheck">
        <echo message="ant.java.version=${ant.java.version}" />
        <condition property="isJava6">
            <equals arg1="${ant.java.version}" arg2="1.6" />
        </condition>
    </target>

</project>
+10
source

Property to check in the assembly file ${ant.java.version}.

You can use the element <condition>to make the task conditional if the property is equal to a specific value:

<condition property="legal-java">
  <matches pattern="1.[56].*" string="${ant.java.version}"/>
</condition>
+2

All Articles