Archive

Archive for the ‘Apache Ant’ Category

Apache Velocity Template


If you are looking for Code generation engine Velocity Templates from Apache is your answer.

The template concept in this helps you to generate source code – java, c, c++, .Net, .txt list can go on…

Source code generated through Apache Velocity Template. Build and Deployment of this code using Apache ANT should give you awesome power 🙂

 

Ant – Pass values from one target to other

December 24, 2010 Leave a comment

Recently I faced an issue with ant script, which I thought should ‘ve been resolved in no time but took me almost a day to resolve!

I had a property defined globally… Using Ant-Contrib I reset the property value in Target1, and I wanted to reuse the updated property value in Target2. Now this is something not straight forward in Ant. In ant all the properties once defined can’t be changed (immutable), but courtesy Ant-Contrib I was able to reset the value using below option (rather cake walk!)

<ac:var name=”current.dir.name” unset=”true”/>

where ‘ac’ is Ant-Contrib name space as defined in my previous articles on ant

It took me some time to peep through the documentation of Ant and there I came across attribute ‘inheritAll‘.

inheritAll is by default set to false, setting it to true made that variable available in Target2!

That’s it!

Ant if-then-else condition

December 21, 2010 1 comment

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.

Following sample code should help you understand if-else-condition using Ant-contrib.

Note: Code in the previous post been extended for this demo. Please note that else block never gets executed in this demo.


<?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}"/>

 <!--Logic for if then else-->
 <ac:if>
 <equals arg1="${directory}" arg2="Test 1"/> <!-- if(directory=="Test 1") -->
 <then> <!--if directory name is Test 1-->
 <echo message="PING directory First directory"/>
 </then>
 <elseif>
 <equals arg1="${directory}" arg2="Test 2"/> <!-- if(directory=="Test 2") -->
 <then>
 <echo message="PING directory Second directory"/>
 </then>
 </elseif>
 <else>
 <echo message="PING No Directory. This never gets executed for this SCRIPT"/>
 </else>
 </ac:if>
 <!--end if then else condition-->

</target>

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

</project>

Following would be the output

Hope this post helps…

Ant set variable property & List ONLY Directory Name

December 21, 2010 1 comment

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…

Ant foreach Directory traversal

December 21, 2010 Leave a comment

This post is for parsing through directory list to list down sub-directories using ant scripting.

Prerequisite:

  1. You should’ve downloaded ant-contrib jar from http://ant-contrib.sourceforge.net/#install
  2. Add the 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 directories as output.

</code>

<?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"/>
<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">
<echo message="${dir.name}"/>
</target>

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

<code>

Following is what you should see on executing the above build script:

In the above code if you just want to display directory names instead of absolute path, you need to use <basename tag, which is explained in another post…

That’s all for now!

Ant for Loop

December 21, 2010 1 comment

This post explains you how to apply for-each iteration in your ant build script.

Prerequisite:

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

Steps:

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


<?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"/>
 <target name="test">
 <echo message="The first five letters of the alphabet are:"/>
 <ac:for list="a,b,c,d,e" param="letter">
 <sequential>
 <echo>Letter @{letter}</echo>
 </sequential>
 </ac:for>
 </target>

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

Save this as build.xml in your desired directory.

Note that specifying namespace ac: is extremely important to successfully execute build script.

Your script should execute and out put should get generated as shown below:

Basics of ANT

December 21, 2010 1 comment

Here are some basics of Apache ANT

Why ANT?

Ant is used for building projects, they are very small and capable of building complex projects. Initially, Ant was part of the Tomcat code base, when it was donated to the Apache Software Foundation. It was created by James Duncan Davidson, who is also the original author of Tomcat. Ant was there to build Tomcat, nothing else.

Soon thereafter, several open source Java projects realized that Ant could solve the problems they had with Makefiles. Starting with the projects hosted at Jakarta and the old Java Apache project, Ant spread like a virus and is now the build tool of choice for a lot of projects.

In January 2000, Ant was moved to a separate CVS module and was promoted to a project of its own, independent of Tomcat, and became Apache Ant.

The first version of Ant that was exposed to a larger audience was the one that shipped with Tomcat’s 3.1 release on 19 April 2000. This version has later been referred to as Ant 0.3.1.

The first official release of Ant as a stand-alone product was Ant 1.1, released on 19 July 2000. Refer the Link for more information.

Now let’s get into some basics.

Prerequisites:

  • Download apache ANT zip file from http://ant.apache.org/bindownload.cgi
  • Unzip in to the file system
  • Set the environment variable ANT_HOME to bin directory of ANT folder you just unzipped in step above
  • Now open a command prompt and type in ‘ant’ as command
  • You should see the following comment, this ensures that you are now ready to take off…

Ant-contrib:

The Ant-Contrib project is a collection of tasks (and at one point maybe types and other tools) for Apache Ant. This Software is distributed under the Apache Software License.

We’ll use Ant-Contrib for using if-else condition, for-each iterator

Note: ant-contrib jar file should be copied into <Apache_home>/lib directory . ANT script should be stored as build.xml file. Ant command should be executed from the directory that contains build.xml

A simple build.xml file looks like this:


<project name="Basic" default="build" basedir="."> <!-- there should be a target by name 'build', other wise error would be thrown-->

<target name="build"  depends="compile" description="compile source code"/> <!--this target calls 'compile'-->

<target name="compile"> <mkdir dir="${build.classes.dir}"/> <!-- create directory for storing compiled java code as .class-->

<javac destdir="${src.dir}" debug="true"> <!-- source directory, where you've java code-->

<src path="${src.audit.dir}"/>

</javac>

</target>

</project>

<code>

 

Here is a sample example for including ant-contrib jar file.
 <project name="DefaultTAPITemplate" 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"/>
</project>

<code>

Let's discuss on how to use for-each and if-else in next post...