Ant task to run an Ant target only if a file exists?
FileAntBuild AutomationFile Problem Overview
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.
File Solutions
Solution 1 - File
<target name="check-abc">
<available file="abc.txt" property="abc.present"/>
</target>
<target name="do-if-abc" depends="check-abc" if="abc.present">
...
</target>
Solution 2 - File
This might make a little more sense from a coding perspective (available with ant-contrib: http://ant-contrib.sourceforge.net/):
<target name="someTarget">
<if>
<available file="abc.txt"/>
<then>
...
</then>
<else>
...
</else>
</if>
</target>
Solution 3 - File
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:
>
>
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:
>
>
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?.....
Solution 4 - File
I think its worth referencing this similar answer: https://stackoverflow.com/a/5288804/64313
Here is a another quick solution. There are other variations possible on this using the <available>
tag:
# exit with failure if no files are found
<property name="file" value="${some.path}/some.txt" />
<fail message="FILE NOT FOUND: ${file}">
<condition><not>
<available file="${file}" />
</not></condition>
</fail>
Solution 5 - File
DB_*/**/*.sql
Check Using Filename filters like Here is a variation to perform an action if one or more files exist corresponding to a wildcard filter. That is, you don't know the exact name of the file.
Here, we are looking for "*.sql" files in any sub-directories called "DB_*", recursively. You can adjust the filter to your needs.
NB: Apache Ant 1.7 and higher!
Here is the target to set a property if matching files exist:
<target name="check_for_sql_files">
<condition property="sql_to_deploy">
<resourcecount when="greater" count="0">
<fileset dir="." includes="DB_*/**/*.sql"/>
</resourcecount>
</condition>
</target>
Here is a "conditional" target that only runs if files exist:
<target name="do_stuff" depends="check_for_sql_files" if="sql_to_deploy">
<!-- Do stuff here -->
</target>
Solution 6 - File
You can do it by ordering to do the operation with a list of files with names equal to the name(s) you need. It is much easier and direct than to create a special target. And you needn't any additional tools, just pure Ant.
<delete>
<fileset includes="name or names of file or files you need to delete"/>
</delete>
See: FileSet.