0

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?

agad
  • 2,192
  • 1
  • 20
  • 32
  • Possible duplicate http://stackoverflow.com/questions/1163998/do-i-have-any-way-to-check-the-existence-of-a-directory-in-ant-not-a-file and http://stackoverflow.com/questions/6334939/how-to-check-if-directory-exists-before-deleting-it-using-ant – CiaPan Jan 29 '15 at 11:58
  • I seem to recall that if and unless on target want a boolean value (true|false) in the property. – Hank Lapidez Jan 29 '15 at 12:05
  • @HankLapidez that applies to ANT >= 1.8. Prior to that it's only a check whether the property is defined. Either way the "available" task only sets the property if file exists. That said the code from above works for me. – Hubert Grzeskowiak Jan 29 '15 at 12:28
  • @CiaPan your links ask, what I know (** - this works fine), my question is completely different. – agad Jan 29 '15 at 13:08

1 Answers1

0

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>
Hubert Grzeskowiak
  • 15,137
  • 5
  • 57
  • 74
  • 1
    This works. Thanks, but full answer is: 1. Property must not be defined: I had ** and in *init* task: ** - **It doesn't work**. 2. A task which fails needs to be conditioned also, so I need not only **, but also ** – agad Jan 29 '15 at 13:04
  • 1
    Right. the "if" condition evaluates to false if the property is not defined OR if it is defined and evaluates to false. Simply don't define it and you're fine. Important point to note: ANT properties are not changeable. That means if you set the property first, you won't be able to set it to something else anymore. Quote from docs: "Properties are immutable: whoever sets a property first freezes it for the rest of the build; they are most definitely not variables." – Hubert Grzeskowiak Jan 29 '15 at 14:31