views:

1476

answers:

2

I have the problem that an specific step in Ant can only be executed when we have Java 1.5 installed in the build computer. The task definition uses uses a jar file that was compiled using 1.5, so running with a 1.4 virtual machine will throw an IncompatibleClassVersion exception.

I have to find a solution meanwhile to have this task working for this specific project that requires 1.4, but a question came to me. How can I avoid defining this task and executing this optional step if I don't have a specific java version?

I could use the "if" or "unless" tags on the target tag, but those only check if a property is set or not. I also would like to have a solution that doesn't require extra libraries, but I don't know if the build-in functionality in standard is enough to perform such a task.

+7  A: 

The Java version is exposed via the ant.java.version property. Use a condition to set a property and execute the 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>
McDowell
+1  A: 

the property to check in the build file is ${ant.java.version}

you could use the <condition> element to make a task conditional when a property equals a certain value: <condition property="legal-java"> <matches pattern="1.[56].*" string="${ant.java.version}"/> </condition>

Lorenzo Boccaccia
Ahhh its a shame that I have to use Ant 1.6! This regexp functionality is only available since ant 1.7!
Mario Ortegón