开发者

How can I check if file exists, and if not kill the build?

开发者 https://www.devze.com 2022-12-10 10:39 出处:网络
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 a

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.


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>


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>


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


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


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" />
0

精彩评论

暂无评论...
验证码 换一张
取 消