使用 ANT 时,如果我有一些特定的 java 版本,我如何定义任务? [英] When using ANT, how can I define a task only if I have some specific java version?

查看:24
本文介绍了使用 ANT 时,如果我有一些特定的 java 版本,我如何定义任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题是,只有在构建计算机中安装了 Java 1.5 时,才能执行 Ant 中的特定步骤.任务定义使用的是使用 1.5 编译的 jar 文件,因此使用 1.4 虚拟机运行将抛出 IncompatibleClassVersion 异常.

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.

同时我必须找到一个解决方案才能让这个任务适用于这个需要 1.4 的特定项目,但是我遇到了一个问题.如果我没有特定的 Java 版本,如何避免定义此任务并执行此可选步骤?

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?

我可以在目标标签上使用if"或unless"标签,但那些只检查是否设置了属性.我也想要一个不需要额外库的解决方案,但我不知道标准中的内置功能是否足以执行这样的任务.

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.

推荐答案

Java 版本通过 ant.java.version 属性公开.使用条件来设置一个属性,只有当它为真时才执行任务.

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>

这篇关于使用 ANT 时,如果我有一些特定的 java 版本,我如何定义任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆