How do I check for the existence of a folder using Ant?
We can check the existence of a file, but can we do the same for a folder as well?
How do I check for the existence of a folder using Ant?
We can check the existence of a file, but can we do the same for a folder as well?
Here's a small example incorporating the
available
element into anif
test.Warning: you need ant-contrib.jar in your ANT_HOME\lib directory otherwise you won't have access to the
if
elements, and your script will fail with this error:Here is another approach, allows to call just one task without using ant-contrib.jar.
Here's my solution, which doesn't require setting properties and using targets with 'if' or 'unless':
Macro:
Usage:
You use the available task with type set to "dir".
For example:
The standard way to do conditional processing is with the condition task. In the example below, running doFoo will echo a message if the directory exists, whereas running doBar will echo a message unless the directory exists.
The dir.check target is required by both doFoo and doBar, it sets the dir.exists property to true or false depending on the result of the available task. The doFoo target will only run if that propery is set to true and doBar will only run if it is not set or set to false.
Antelope provides additional tasks, including an If task that can make the processing simpler (and to me, more intuitive), you can download the Antelope tasks from the download page.
My solution using ANT 1.8 version, older versions may not work due if/unless not supporting ${evalTrueOrFalse} syntax.
http://ant.apache.org/manual/properties.html#if+unless
[If/Unless] In Ant 1.7.1 and earlier, these attributes could only be property names. As of Ant 1.8.0, you may instead use property expansion. Compared to the older style, this gives you additional flexibility.