Here comes the importance of a build tool like Ant. It stores, executes and automates all process in a sequential order mentioned in Ant’s configuration file (usually build.xml).

Benefit of Ant build

Ant creates the application life cycle i.e. clean, compile, set dependency, execute, report, etc. Third party API dependency can be set by Ant i.e. other Jar file’s class path 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 XML file and which can be executed from the command line. It makes your code clean as configuration is separate from actual application logic.

How to install Ant

Steps to install Ant in Windows is as follows 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 -> Computer -> right click here and select ‘Properties’ then click on Advanced System Settings

Step 4) A new window opens. Click on ‘Environment Variable…’ button.

Step 5) Click ‘New…’ button and set variable name as ‘ANT_HOME’ and variable value as the root path to unzipped folder and click OK.

Step 6) now select ‘Path’ variable from the list and click ‘Edit’ and append; %ANT_HOME%\bin.

Restart system one time and you are ready to use Ant build tool now. Step 7) To check the version of your Ant using command line: Ant –version

Understanding Build.xml

Build.xml is the most important component of Ant build tool. For a Java project, all cleaning, setup, compilation and deployment related task 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. 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

Property tags are used as variables in build.XML file to be used in further steps

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

path tag is used to bundle all files logically which are in the common location

pathelement tag will set the path to the root of common location where all files are stored

pathconvert tag used to convert paths of all common file inside path tag to system’s classpath format

fileset tag used to set classpath for different third party jar in our project

Echo tag is used to print text on the console

Delete tag will clean data from given folder

mkdir tag will create a new directory

javac tag used to compile java source code and move .class files to a new folder

jar tag will create jar file from .class files

manifest tag will set your main class for execution

‘depends’ attribute used to make one target to depend on another target

java tag will execute main function from the jar created in compile target section

Run Ant using Eclipse plugin

To run Ant from eclipse go to build.xml file -> right click on file -> Run as… -> click Build file

Example:

We will take a small sample program that will explain Ant functionality very clearly. Our project structure will look like –

Here in this example we have 4 targets

Set class path for external jars, Clean previously complied code Compile existing java code Run the code

Guru99AntClass.class Build.xml

How to Execute TestNG code using Ant

Here we will create a class with Testng methods and set class path for Testing in build.xml. Now to execute testng method we will create another testng.xml file and call this file from build.xml file. Step 1) We create a “Guru99AntClass.class” in package testAnt Guru99AntClass.class Step 2) Create a target to load this class in Build.xml Step 3) Create testng.xml testng.xml Step 4) Create Target in Build.xml to run this TestNG code Step 5) The complete Build.xml Step 6) Output

Download the above File

Ant with Selenium Webdriver:

So far, we have learned that using ANT we can put all third party jars in a particular location in the system and set their path for our project. Using this method we are setting all dependencies of our project in a single place and making it more reliable for compilation, execution, and deployment. Similarly, for our testing projects using selenium, we can easily mention selenium dependency in build.xml and we don’t need to add a class path of it manually in our application. So now you can ignore below-mentioned traditional way to set classpaths for project.

Example: We are going to modify the previous example Step 1) Set the property selenium.jars to selenium related jar in the resource folder Step 2) In the target setClassPath, add the selenium files Step 3) Complete Build.xml: Step 4) Now change previously created class Guru99AntClass.java with new code. Here in this example our steps using Selenium are:

Go to http://demo.guru99.com/test/guru99home/ Read all courses links one by one Print all courses hyperlink on console.

Guru99AntClass.java: Step 5) After successful execution output will look like:

Download the Above Example File

Summary

Ant is a build tool for Java. Ant used for code compilation, deployment, execution process. Ant can be downloaded from Apache website. Build.xml file used to configure execution targets using Ant. Ant can be run from the command line or suitable IDE plugin like eclipse.