Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I have a way to check the existence of a directory in Ant (not a file)?

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?

like image 535
Chathuranga Chandrasekara Avatar asked Jul 22 '09 09:07

Chathuranga Chandrasekara


People also ask

What is Basedir in ant?

The 'basedir' is the base directory from which any relative directories used within the Ant build file are referenced from. If this is omitted the parent directory of the build file will be used.

Where is Ant lib folder?

The default directories scanned are ANT_HOME/lib and a user specific directory, ${user. home}/. ant/lib .


2 Answers

You use the available task with type set to "dir".

For example:

<available file="${dir}" type="dir"/> 

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.

<?xml version="1.0"?> <project name="test" default="doFoo" basedir=".">   <property name="directory" value="c:\test\directory"/>    <target name="doFoo" depends="dir.check" if="dir.exists">     <echo>${directory} exists</echo>   </target>    <target name="doBar" depends="dir.check" unless="dir.exists">     <echo>${directory} missing"</echo>   </target>    <target name="dir.check">     <condition property="dir.exists">       <available file="${directory}" type="dir"/>     </condition>   </target> </project> 

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.

like image 107
Rich Seller Avatar answered Sep 17 '22 14:09

Rich Seller


Here's a small example incorporating the available element into an if test.

<!-- Test if a directory called "my_directory" is present --> <if>   <available file="my_directory" type="dir" />   <then>     <echo message="Directory exists" />   </then>   <else>     <echo message="Directory does not exist" />   </else> </if> 

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:

Problem: failed to create task or type if Cause: The name is undefined. Action: Check the spelling. Action: Check that any custom tasks/types have been declared. Action: Check that any <presetdef>/<macrodef> declarations have taken place.  
like image 38
Dan J Avatar answered Sep 17 '22 14:09

Dan J