Ant - 跳过任务,当目录不存在时

Ant - skip task, when directory does not exist

我有一组项目,它们使用来自 "parent" 项目的构建文件。一切正常,直到每个项目都包含 test 目录。 现在我有一个新项目,还没有测试,所以如果 test 我不想 运行 tests 任务目录不存在。 (原始脚本因 srcdir "C:\.jenkins\jobs\Cenne Papiry\workspace\test" does not exist! 而失败。) 我试图设置 test.src 属性 仅当 test 目录存在时:

<available file="test" property="test.src" value="test"/>

并根据 test.src 属性:

的存在条件 tests 任务
<target name="tests" depends="compile-tests" if="test.src">

test.scr 属性 未设置,但 ant 尝试执行 测试 任务还在:

C:\Users\vackova\workspace\commons-agata\build.xml:246: srcdir attribute must be set!

(<javac target="1.8" debug="true" srcdir="${test.src}" destdir="${class.dir}" encoding="UTF-8" >)

我怎样才能实现我的目标?

您的代码是正确的,应该适用于大多数 ant 版本。这是一个测试片段,应该 运行 独立,在 ant 1.7.1 和 1.9.4 中测试:

<project name="bar" default="tests" basedir=".">

    <available file="foo" property="test.src" value="anything"/>

    <target name="tests" if="test.src">
        <echo message="folder or file foo exists"/>
    </target>
</project>