Apache ANT
In general, while developing a software product, we need to take care of different third party API, classpath, cleaning of executable binary files, compiling and execution of source code, creation of reports and deployment code base etc. If we manually do all these tasks one by one, it will take more time, and the process will lead to errors. To overcome this situation we use a build tool called ANT. It executes and automates all the processes in a sequential order which are mentioned in the Ants configuration file (using build.xml file).
Installation of Apache ANT
Now let’s see the installation part for Apache ANT in Windows
Step-1: Go to https://ant.apache.org/bindownload.cgi Or Download .zip file from apache-ant-1.9.4-bin.zip
Step-2:Â Unzip the folder and go to and copy path to the root of unzipped folder
Step-3:Â Go to Start -> Edit the system environment variables
Step-4: A window opens. Now click on the ‘Environment Variables’ button.
Step-5:Â Under User variables, click the New button.Â
Now set variable name as ‘ANT_HOME’ and variable value as the root path to unzipped folder and click OK.
Step-6:Â Under System variables, click on Path and then Edit.
Now click on the New button and add bin path of downloaded apache-ant-1.9.4.Â
Once the path is added, click ok of each window.
Step-7: To check the version of your Ant using command line ‘Ant –version’
Note: If you are not getting the Ant version, restart the system and check. If it doesn’t work, check if you added the paths correctly under system and environment variables.
Build.xml
Build.xml is the most important component of the Ant build tool. For a Java project, all cleaning, setup, compilation and deployment related tasks are mentioned in this file in XML format. When we execute this XML file using command line or any IDE plugin, all instructions written into this file will get executed in sequential manner.Â
First, let’s understand the code within a sample build.XML
Project tag is used to mention a project name and basedir attribute. The basedir is the root directory of an application <project name="YTMonetize" basedir=".">
Property tags are used as variables in build.XML file to be used in further steps
<property name="build.dir" value="${basedir}/build"/>
<property name="external.jars" value=".\resources"/>
<property name="ytoperation.dir" value="${external.jars}/YTOperation"/>
<property name="src.dir"value="${basedir}/src"/>
Target tags used as steps that will execute in sequential order. Name attribute is the name of the target. You can have multiple targets in a single build.xml <target name="setClassPath">
path tag is used to bundle all files logically which are in the common location <path id="classpath_jars">
pathelement tag will set the path to the root of common location where all files are stored <pathelement path="${basedir}/"/>
pathconvert tag used to convert paths of all common file inside path tag to system’s classpath format <pathconvert pathsep=";" property="test.classpath" refid="classpath_jars"/>
fileset tag used to set classpath for different third party jar in our project <fileset dir="${ytoperation.dir}" includes="*.jar"/>
Echo tag is used to print text on the console <echo message="deleting existing build directory"/>
Delete tag will clean data from given folder <delete dir="${build.dir}"/>
mkdir tag will create a new directory <mkdir dir="${build.dir}"/>
javac tag used to compile java source code and move .class files to a new folder
       <javac destdir="${build.dir}" srcdir="${src.dir}">
<classpath refid="classpath_jars"/>
</javac
jar tag will create jar file from .class files
<jar destfile="${ytoperation.dir}/YTOperation.jar" basedir="${build.dir}"
manifest tag will set your main class for execution
<manifest>
<attribute name="Main-Class" value="test.Main"/>
</manifest>
‘depends’ attribute used to make one target to depend on another target
<target name="run" depends="compile">
java tag will execute main function from the jar created in compile target section
<java jar="${ytoperation.dir}/YTOperation.jar" fork="true"/>
Run ANT using Eclipse
Here I have created a sample program to explain Ant functionality clearly.Â
Step-1:Â To generate a build.xml file, First create a project.
Once the project is created, click on File → Export → General → Ant Buildfiles →Next → select related project → click Finish.
Step-2:Â Now create a class and write the Java program and update the build
In this example we have to
Set classpath for external jars,
Clean previously compiled code
Compile existing java code
Run the code
Update Build file with Project name, classpath, and class name as shown below.
Step-3: To run Ant from eclipse go to build.xml file → right click on file → Run as → click related Ant Build file
Step-4:Â Select Run option in targets tab and Click on Run button
Here is the Result, deleting the previous build, creating directory and compiling the source file
Execute TestNG code using Ant
Now let's check how to run Ant using TestNG. If you convert the previous program to TestNG, it will generate a testng.xml file. We call this file in build.xml file
Step-1: Here we are updating the above program using TestNG methods and then set classpath for testing in build.xml.
Step-2: Update the testng.xml file with class name and test name as shown
Step-3: Create a target to load this class in Build.xml
Step-4:Â Create Target in Build.xml to run this TestNG code
Complete build file is shown below
Step-5: To run Ant from eclipse go to build.xml file → right click on it → Run as → click related Ant Build file.
Now Select "ApacheAntTestNG" option under Targets tab and Click on Run button
Step-6:Â Result in console looks as below
Features of Apache Ant
It complies with Java-based applications
Creates zip, jar, war, tar files
Creates Java Doc
It can copy files to different locations
We can move or delete files
It supports TestNG, Junit 4, etc.
It will convert XML based test reports to HTML reports
Emails can be sent to end-users
Benefits of Apache ANT:
Ant creates an application life cycle i.e. clean, compile, set dependency, execute, report..
Third party API dependency can be set by Ant i.e. other Jar file’s classpath is set by Ant build file.
A complete application is created for End to End delivery and deployment.
It is a simple build tool where all configurations can be done using an XML file and which can be executed from the command line.
It makes your code clean as configuration is separate from actual application logic.
Ant is implemented and written in Java language thus is a platform-independent build tool. JDK is the only requirement for the tool.
Conclusion:
An Ant is an open-source tool that automates the software build process. Apache Ant is a build tool developed using java. It was implemented for the Java project because it has an in-build feature with Java but it can still be used for applications built on other languages. Apache Ant is the most popular and conventional build tool. It was provided by Apache Software Foundation and freely distributed under the GNU license. In day to day work schedule, Apache Ant plays an important role in developers as well as Testers environment. It has very immense power to build the code into deployment utilities.
Comments