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

旧街凉风 提交于 2019-12-21 14:39:12

问题


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.


回答1:


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>



回答2:


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>


来源:https://stackoverflow.com/questions/147850/when-using-ant-how-can-i-define-a-task-only-if-i-have-some-specific-java-versio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!