Search code examples
antjavac

Ant - skip task, when directory does not exist


I have set of projects, that use build file from "parent" project. All works fine until each project contains test directory. Now I have a new project, that have no tests yet, so I would like to do not run tests task if the test directory does not exist. (Original script fails with srcdir "C:\.jenkins\jobs\Cenne Papiry\workspace\test" does not exist!.) I'd tried to set test.src property only if test directory exist:

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

and condition the tests task on existence of test.src property:

<target name="tests" depends="compile-tests" if="test.src">

test.scr property is not set, but ant tries to execute tests task still:

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" >)

How can I achieve my aim?


Solution

  • Your code is correct and should work in most ant versions. Here's a snippet for testing that should run standalone, tested in ant 1.7.1 and 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>