Few hints about Apache Ant

Apache Ant is one of the most awesome automation tools, even though it is not as good as Maven. If nowadays I want to select an automation tool for a new project definitely I got with Maven, but before it existence Ant was rolling.

Recently, I had to work with Apache Ant to make few tasks done and after little bit of exploring on the internet I found the solutions and the correct way of doing them. So I am sharing here.

I have listed the tasks that I wanted to do in my Apache Ant project.

  1. Run a bash script from Apache Ant
  2. Add dependencies between targets
  3. Copy some files from source to destination in Ant
  4. Creating directory if not exists
  5. Adding cleaning function

For number 1 case, I have found the following solution,

<target name=”install” depends=”main”>

bin/bash”>

<arg value=”install.sh”/>

</exec>

</target>

In above code, exec parameter entry defines what programshould be run which is “/bin/bash” and the arg entry refers to the argument that will be passing to the exec.

Number 2 is a very easy task. You should note that this is possible if you have more than one target in your build.xml file. In this example we define a dependency in which main is dependent to jar entry. Look at this code for better understanding,

</pre>
<target name="jar">
………….
</target>
<target name="main" depends="jar">
………….
</target>
<pre>

Case number 3 is even simpler, you just need to add this code to your build.xml to be able to do copy,

<copy file="MyFile.txt" todir="${build.dir}"/>

Number 4 is more or less similar to 3. Just have  a look on how you can create a directory in Ant if not exists,

<mkdir dir="MyDirectory"/>

And for the last one which is case number 5. You just need to add another target let’s call it clean and then call delete function inside of it like below,

<target name="clean">
<delete dir="temp"/>
</target>

Leave a Reply