15

How can I stop a build, and notify the user if a file does not exist? I know I can use the available task to set a property if a file exists, but I'm not sure how I would stop a build and echo something.

I would like to stick with core tasks if possible.

Ken
  • 1,691
  • 5
  • 22
  • 38
Steve
  • 53,375
  • 33
  • 96
  • 141

5 Answers5

19

You can use the fail task for all your failing needs. The last example on that page is actually pretty much what you need

<fail message="Files are missing.">
    <condition>
        <not>
            <resourcecount count="2">
                <fileset id="fs" dir="." includes="one.txt,two.txt"/>
            </resourcecount>
        </not>
    </condition>
</fail>
martin clayton
  • 76,436
  • 32
  • 213
  • 198
stimms
  • 42,945
  • 30
  • 96
  • 149
19

A little simpler (I wish it could be made simpler)

<fail message="file ${myfile} not set or missing">
    <condition>
        <not>
            <available file="${myfile}" />
        </not>
    </condition>
</fail>
leonbloy
  • 73,180
  • 20
  • 142
  • 190
6

Set your property and use the Fail task with the if attribute.

martin clayton
  • 76,436
  • 32
  • 213
  • 198
Restore the Data Dumps
  • 38,967
  • 12
  • 96
  • 122
4

This can be done more compactly (as indicated by Jason Punyon). Specifically, assuming the file you want is in the property file, do:

<available file="${file}" property="file.exists" />
<fail message="File missing: ${file}" unless="file.exists" />
darrenp
  • 4,265
  • 2
  • 26
  • 22
1

These kind of checks are common, so it might pay off to use a macro. Here is a macro based on the solution by leonbloy:

<macrodef name="require">
    <attribute name="file"/>
    <attribute name="message" default="file @{file} not set or missing"/>
    <sequential>
        <fail message="@{message}">
            <condition>
            <not>
                <available file="@{file}" />
            </not>
            </condition>
        </fail>
    </sequential>
</macrodef>

Use like this:

<require file="${myfile}" />

or

<require file="${myfile}" message="my custom message" />
Erik Lievaart
  • 413
  • 3
  • 7