Home > Apache Ant, General, Technology > Ant set variable property & List ONLY Directory Name

Ant set variable property & List ONLY Directory Name


Unlike my previous post, this demo lists only the Directory Names and implicitly helps you in understanding how to change property values.

It is said that property value once set can’t be changed in the ant script.  But in this demo you’ll see how to re-set the property values.

Prerequisite:

  1. You should’ve downloaded ant-contrib jar from http://ant-contrib.sourceforge.net/#install
  2. Place ant-contrib.jar into <ANT_HOME>/lib directory

Steps:

You first need to refer to your contrib jar in the <project> tag of build.xml.

I created two sample directories in my C:/AntTutorial for this demo

As you can see in the screen-shot there are 2 dirs, Test 1 and Test 2. The below ant script is going to list these two directory names as output.

 


<?xml version="1.0" encoding="UTF-8"?>
<project name="ForTest" default="build" basedir="." xmlns:ac="antlib:net.sf.antcontrib">
 <taskdef uri="antlib:net.sf.antcontrib"
 resource="net/sf/antcontrib/antlib.xml"
 classpath="../../resources/ant/ant-contrib-1.0b3.jar"/>

 <property name="directory" value=""/>

 <target name="test">
 <echo message="Folders in the directory are:"/>
 <ac:foreach target="list.dirs" param="dir.name">
 <path>
 <dirset dir="${basedir}" includes="*"/>
 </path>
 </ac:foreach>
 </target>

 <target name="list.dirs">
 <ac:var name="directory" unset="true"/>
 <basename property="directory" file="${dir.name}"/>
 <echo message="Absolute Path : ${dir.name}"/>
 <echo message="Directory     : ${directory}"/>
 </target>

 <target name="build" depends="test" description="Test For loop"/>

</project>

<code>


 

Yous should see the following output on executing the above build script.

Here we are re-using the property ‘directory’ for temporarily storing the directory name. You need to call unset=”true” over this property to reset the value.

 


<ac:var name="directory" unset="true"/>
 <basename property="directory" file="${dir.name}"/>

<code>


 

Hope this post helps you…

Advertisements
  1. September 19, 2013 at 3:36 PM

    We offer Agra tour by car, one day Agra tour, Agra tour packages, Agra tour, Agra tour from Delhi, same day Agra tour, private Agra tour, group tour packages, one day tour Agra, Agra tours, India Agra tour, Jaipur Agra tour, Delhi Agra Jaipur tour, day trip to Agra, holiday in Agra, honeymoon packages Agra, car rental services, Agra tour by Car.

    Like

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: