qodeninja
qodeninja

Reputation: 11296

In an Ant build script, how do you check for the existence of a file or directory as a dependency for a task?

Getting started on writing my first build script. I dont get the whole dependency thing. This kind of make sense when your compiling say java and you need the object files to create a jar. But what about if you just want to verify the existence of any file or directory thats not part of a compile task?

Ive been able to use available but I dont know how to use the result of that as a dependency for a task

Upvotes: 1

Views: 2586

Answers (2)

Mads Hansen
Mads Hansen

Reputation: 66796

With something like this:

<project name="foo-bar" basedir=".">
    <target name="bar" depends="foo" unless="isAvailable">
     <echo message="file is not available" />
    </target>

    <target name="foo">
      <available file="${basedir}/path/to/file.java" property="isAvailable"/>
    </target>
</project>
  • The "bar" target depends on the "foo" target.
  • When "foo" is executed, it checks for the presence of the file.
  • If it exists, it sets the isAvailable property.
  • The "bar" target will only execute and echo the message if isAvailable is not set.

You can also put the <available> at the "root level" of your build(outside of a target, as a direct child of the <project> and it will get evaluated before any of the targets are run:

<project name="foo" basedir=".">

    <available file="${basedir}/path/to/file.java" property="isAvailable"/>

     <target name="bar" unless="isAvailable">
     <echo message="file is not available" />
    </target>
</project>

Upvotes: 5

JB Nizet
JB Nizet

Reputation: 692231

If you want to test if a file exists, look at available

Upvotes: 1

Related Questions