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.
This question is related to
file
ant
build-automation
DB_*/**/*.sql
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>
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?.....
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.
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>
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>
Source: Stackoverflow.com