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

半腔热情 提交于 2020-01-02 00:50:56

问题


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.


回答1:


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>



回答2:


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>



回答3:


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




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



回答5:


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


来源:https://stackoverflow.com/questions/1638176/how-can-i-check-if-file-exists-and-if-not-kill-the-build

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!