Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 743
Mario Ortegón Avatar asked Sep 29 '08 07:09

Mario Ortegón


2 Answers

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>
like image 95
McDowell Avatar answered Oct 07 '22 16:10

McDowell


The property to check in the buildfile 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>
like image 27
Lorenzo Boccaccia Avatar answered Oct 07 '22 17:10

Lorenzo Boccaccia