Saturday, 29 June 2013

ANT - Step By Step Build Procedure

Build Tools - Build tools are used to automate the repetitive tasks. This can be for example compiling source code, running software test and creating files and documentation for software deployment. Build tools are run from command prompt. Examples of Build tools are - Apache ANT, Maven etc.

ANT - Another Neat Tool, this is what it Stands for. Its a Java build and deployment tool written using plain XML.

Installing Apache ANT

Before starting working with ANT, you need to setup ANT in your system. Since I am using Windows operating system so I will concentrate over that only.
1. Get latest Apache ANT version from http://ant.apache.org .
2. Unzip the zip file to a location of your choice lets say C:\Ant.
3. Set up the environment variables ANT_HOME and JAVA_HOME as highlighted in the image below.















4.Now to verify whether ANT has been successfully installed or not, go to command prompt and type
   c:\>ant -versionApache
 Ant(TM) version 1.9.0 compiled on March 5 2013

 If you don't see the above output then something has gone wrong with the installation.

Step By Step creation of build.XML file. I have seggregated it into steps to have better understanding.

1. Every build.xml file starts with <project > tag. Here, default attribute will identify which target is to be    called while processing the build.xml and basedir is pointing to root directory.

<?xml version="1.0"?>
    <project name="Ant Build" basedir="." default="execute">


2. Define all the properties which would be needed. These properties will be used in the further steps. To make ANT script generic I have defined all the properties so that I can change these properties to build another java code in future. Moreover, we can have a separate build.properties files where we can define these properties as key value pair. 

 <property name="project" value="SAXParserImplementation"/>
 <property name="src.dir" value="src/parser"/>
 <property name="build.dir" value="build/class"/>
 <property name="jar.dir" value="jar"/>
 <property name="config.dir" value="config"/>
 <property name="parameters" value="D:/test.xml"/>
 <property name="jarFileName" value="SAXParser.jar"/>
 <property name="className" value="parser.SAXParserImplementation"/>
 <property name="includeFileList" value="SAXParserImplementation.java"/>

build.properties
src.dir=src/parser
build.dir=build/class


And, in the build.xml define <property file="build.properties"/>. No need to mention any other property. You can move all your properties to build.properties.

3. Our default target is execute, this target will execute the java code using java command. I have placed the test.xml inside the config folder and passed this as an argument to my java code. You can refer SAX Parser code from post.

<target name="execute" description="Executing Java Code" depends="compress">
   <echo>Executing Project - ${project}...</echo>
   <echo>Output...</echo>
   <java classpath="${build.dir}" classname="${className}">
      <arg line="${config.dir}/test.xml"/>
   </java>
</target>

But before executing it, it goes to compress target since this is dependent on it.

4. compress target will create a Jar file by using jar command and place it inside the jar.dir which I have already defined in the properties.

<target name="compress" description="Create a Jar file" depends="compile">
   <echo>Creating Jar file...</echo>
       <jar destfile="${jar.dir}/${jarFileName}" basedir="${build.dir}"/>
</target>

But before executing it, it goes to compile target.

5. compile target is used to create the class files which will take the java code from src.dir and compiled it to build.dir. The includes attribute defines which all files need to be compiled. Since in my project, number of java files were there in the folder. But I wanted to compile only SAXParserImplementation.java, so I defined it in that attribute. You can define more java classes also as a comma separated list.

<target name="compile" description="Create a class file" depends="init">
   <echo>Compiling Java file...</echo>
   <javac srcdir="${src.dir}" destdir="${build.dir}" target="1.6" debug="true" includes="${includeFileList}"/>
</target>

Again we can see that this target is dependent on init. So it will process init first.

6. init target is used to create and copy operation.

<target name="init" description="Make directory" depends="clean">
   <echo>Creating Build/Jar Directory...</echo>
   <mkdir dir="${build.dir}"/>
   <mkdir dir="${jar.dir}"/>
   <mkdir dir="${config.dir}"/>
   <copy file="${parameters}"        todir="${config.dir}"/>
</target>

But this is dependent on clean, so clean will be executed first.

7. clean target is used for deleting the directories.

<target name="clean" description="Delete Directories">
   <echo>Deleting Directories...</echo>
   <delete dir="${build.dir}"/>
   <delete dir="${jar.dir}"/>
   <delete dir="${config.dir}"/>
</target>

Now start in reverse order from 7 to 1, you can easily understand what we are doing. Summarizing all the steps - clean all the directories --> create new directories --> compile your code --> create Jar out of it --> Execute it to get the output.

<?xml version="1.0"?>
<project name="Ant Build" basedir="." default="execute">
 <!--<property file="build.properties"/>-->
 <property name="project" value="SAXParserImplementation"/>
 <property name="src.dir" value="src/parser"/>
 <property name="build.dir" value="build/class"/>
 <property name="jar.dir" value="jar"/>
 <property name="config.dir" value="config"/>
 <property name="parameters" value="D:/test.xml"/>
 <property name="jarFileName" value="SAXParser.jar"/>
 <property name="className" value="parser.SAXParserImplementation"/>
 <property name="includeFileList" value="SAXParserImplementation.java"/>
 
 <target name="clean" description="Delete Directories">
  <echo>Deleting Directories...</echo>
   <delete dir="${build.dir}"/>
   <delete dir="${jar.dir}"/>
   <delete dir="${config.dir}"/>
 </target>
 
 <target name="init" description="Make directory" depends="clean">
  <echo>Creating Build/Jar Directory...</echo>
  <mkdir dir="${build.dir}"/>
  <mkdir dir="${jar.dir}"/>
  <mkdir dir="${config.dir}"/>
  <copy file="${parameters}" todir="${config.dir}"/>
 </target>
  
 <target name="compile" description="Create a class file" depends="init">
  <echo>Compiling Java file...</echo>
  <javac srcdir="${src.dir}" destdir="${build.dir}" target="1.6" debug="true" includes="${includeFileList}"/>
 </target>
 
 <target name="compress" description="Create a Jar file" depends="compile">
  <echo>Creating Jar file...</echo>
  <jar destfile="${jar.dir}/${jarFileName}" basedir="${build.dir}"/>
 </target>
 
 <target name="execute" description="Executing Java Code" depends="compress">
  <echo>Executing Project - ${project}...</echo>
  <echo>Output...</echo>
  <java classpath="${build.dir}" classname="${className}">
   <arg line="${config.dir}/test.xml"/>
  </java>
 </target>
</project>

Running the Ant Project

Copy build.xml and build.properties file(if any) to the location from where it can target the src folder as you have defined in the properties. To run it, go to command prompt and go to the directory where you have placed build.xml. Run ant command and here you go... 


2 comments: