Is there an ANT Task that would execute a block only if a given file exists? I have the problem that I have a generic ant script that should do some special processing but only if a specific configuration file is present.
Not tested:
<target name="check-abc">
<available file="abc.txt" property="abc.present"/>
</target>
<target name="do-if-abc" depends="check-abc" if="abc.present">
...
</target>
This might make a little more sense from a coding perspective:
<target name="someTarget">
<if>
<available file="abc.txt"/>
<then>
...
</then>
<else>
...
</else>
</if>
</target>
Since Ant 1.8.0 there's apparently also resourceexists
From http://ant.apache.org/manual/Tasks/conditions.html
Tests a resource for existance. since Ant 1.8.0
The actual resource to test is specified as a nested element.
An example:
<resourceexists> <file file="${file}"/> </resourceexists>
I was about rework the example from the above good answer to this question, and then I found this
As of Ant 1.8.0, you may instead use property expansion; a value of true (or on or yes) will enable the item, while false (or off or no) will disable it. Other values are still assumed to be property names and so the item is enabled only if the named property is defined.
Compared to the older style, this gives you additional flexibility, because you can override the condition from the command line or parent scripts:
<target name="-check-use-file" unless="file.exists"> <available property="file.exists" file="some-file"/> </target> <target name="use-file" depends="-check-use-file" if="${file.exists}"> <!-- do something requiring that file... --> </target> <target name="lots-of-stuff" depends="use-file,other-unconditional-stuff"/>
from the ant manual at http://ant.apache.org/manual/properties.html#if+unless
Hopefully this example is of use to some. They're not using resourceexists, but presumably you could?.....