Apache ANT – a Tool for Automating Software Build Processes and its Importance in Testing – Selenium Tutorial #23

By Vijay

By Vijay

I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated June 24, 2024

In the last tutorial, we tried to make you acquainted with the concept of generics and common methods. We also discussed the benefits we get out of generics like reusability. We also shared the practical approaches towards the creation of generics and their accessibility.

In the current tutorial in this Selenium automation series, we will shed light on a build tool named “Apache Ant”. We would broadly discuss its applicability and importance besides the practical approach.

Take note the tutorial is limited to testing aspects of using Apache Ant.

Apache Ant Automation Tool

Apache ANT

Apache Ant is a very popular and conventional build tool of our times. Ant is an open-source Java-based build tool provided by the Apache Software Foundation and freely distributed under a GNU license. Apache Ant plays a significant role in developers’ as well as testers’ day-to-day work schedules. The tool has immense power to build the development code into deployment utilities.

Ant is a tool that automates the software-building process. With Ant, you can achieve more than just code compilation; it enables packaging, testing, and much more through simple steps.

The tool works on the principle of targets and dependencies defined in the XML files. Ant libraries are used to build the applications. The libraries have a set of defined tasks to archive, compile, execute, document, deploy, test, and many more targets. Ant allows the user to create his/her tasks by implementing their libraries.

Developers primarily use Ant with Java Applications but they can still use it for applications built in other languages depending on the extended support.

The key benefit of using Ant is that it eliminates the need for additional code to build the application; instead, the process is defined by XML elements, called targets.

Apache Ant Benefits

  • Ease of Use: The tool provides a wide range of tasks that almost fulfill all the build requirements of the user.
  • Platform Independent: Ant is written in Java and thus is a platform-independent build tool. The only requirement for the tool is JDK.
  • Extensibility: As the tool is written in Java and the source code is freely available, a user is leveraged with the benefit of extending the tool’s capabilities by writing Java code for adding a task in Ant Libs.

Apache Ant Features

Consider the following characteristics:

  • Compile Java-based applications
  • Create Java Doc
  • Create war, jar, zip, and tar files
  • Copy files to different locations
  • Can delete or move files
  • Can send emails to the stakeholders
  • Supports JUnit 3, JUnit 4, TestNG, etc.
  • Can convert XML-based test reports to HTML reports
  • Can make directories
  • Check out the code from the version control system (SVN, GIT, CVS, etc).
  • Can execute test scripts and test suites

Environment Setup

Let us demonstrate the entire setup process step by step.

Step #1: Apache Ant Download

The step is to download the zipped folder of Apache Ant latest version from the repository. The distribution is available at “http://ant.apache.org/bindownload.cgi”.

Apache ANT 1

Step #2: Extract the folder and Set Environment Variables

Extract the zipped folder at any desired location on the local file system. Before setting up the environment for Ant, it is required to install and set JDK on to your system. I assume that the JDK is already set and installed, thus moving forward with the Ant Setup.

Create an environment variable for “ANT_HOME” and set the variable’s value to the location of the Ant folder. Refer to the following screenshot for the same.

(Click to enlarge the image)

Apache ANT 2

Edit the Path variable to append the location of the bin folder, i.e. compiler location.

The user can also verify the successful Ant installation by typing in the “ant -version” command in the command prompt. The user could see the following screen for the successful installation.

Apache ANT 3

Step #3: Download and Extract Junit Jar

Download the latest version of the Junít jar from “https://github.com/junit-team/junit/wiki/Download-and-Install” configure the project’s build path in Eclipse and add the jar as an external library. Refer to the following illustration.

configure the project’s build path

Thus, no other installation is required to use Apache Ant in collaboration with Junit and Selenium WebDriver to build, execute, and report the test scripts.

Note: Take a note to necessarily add the “ant-junit4.jar” jar file that can be found within the library folder of the Ant’s software distribution.

Sample Build.xml

The next step is to create the project’s build file. The build file is nothing but a collection of XML elements. Worth mentioning that one build file can relate to one and only one project, i.e. one build file per project or vice versa.

Build file is customarily located in the project’s root/base folder, but the user is leveraged to select the build’s location driven by his/her wish. Moreover, the user is free to rename the build file if he/she desires.

Each of the build files must have one project and at least one target element. Refer to the sample build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="Learning_Selenium" default="junitReport" basedir=".">
       <property name="src" value="./src" />
       <property name="lib" value="./lib" />
       <property name="bin" value="./bin" />
       <property name="report" value="./report" />
       <property name="test.dir" value="./src/com/tests" />
       <path id="Learning_Selenium.classpath">
              <pathelement location="${bin}" />
              <fileset dir="${lib}">
                     <include name="**/*.jar" />
              </fileset>
       </path>
       <echo message="-----------------------------------------------------------------" />
       <echo message="--------------------Selenium Learning Tests----------------------" />
       <echo message="-----------------------------------------------------------------" />
       <target name="init" description="Delete the binary folder and create it again">
              <echo message="----------Delete the binary folder and create it again----------" />
              <delete dir="${bin}" />
              <!-- Create the time stamp -->
              <tstamp>
                     <format property="lastUpdated" pattern="dd-MM-yyyy HH:mm:ss" />
              </tstamp>
              <!-- Create the build directory structure used by compile -->
              <mkdir dir="${bin}" />
       </target>
       <target name="compile" depends="init" description="Compile the source files">
              <echo message="----------Compile the source files----------" />
              <javac source="1.7" srcdir="${src}" fork="true" destdir="${bin}" includeantruntime="false" debug="true" debuglevel="lines,vars,source">
                     <classpath refid="Learning_Selenium.classpath" />
              </javac>
       </target>
       <target name="exec" depends="compile" description="Launch the test suite">
              <echo message="----------Launch the test suite----------" />
              <delete dir="${report}" />
              <mkdir dir="${report}" />
              <mkdir dir="${report}/xml" />
              <junit fork="yes" printsummary="withOutAndErr" haltonfailure="no">
                     <classpath refid="Learning_Selenium.classpath" />
                     <formatter type="xml" />
                     <batchtest fork="yes" todir="${report}/xml">                        
                           <fileset dir="${src}" includes="**/com/TestSuite.java" />
                     </batchtest>
              </junit>
       </target>
       <target name="junitReport" depends="exec" description="Generate the test report">
              <echo message="----------Generate the test report----------" />
              <junitreport todir="${report}">
                     <fileset dir="${report}/xml">
                           <include name="TEST-*.xml" />
                     </fileset>
                     <report format="frames" todir="${report}/html">
                           <param name="TITLE" expression="Selenium_Learning_Report" />
                     </report>
              </junitreport>
      </target>
</project>

Explanation of Build.xml

The project element fundamentally comprises 3 attributes:

<project name=”Learning_Selenium” default=”junitReport” basedir=”.”>

Each of the attributes has a “Key-Value pair” structure.

  • Name: The value of the name attribute represents the name of the project. Thus, in our case, the project’s name is “Learning_Selenium”.
  • Default: The value of the default attribute represents the compulsory target for the build.xml. A build.xml file can have any number of targets. This field is the required goal among all others.
  • Basedir: Represents the root folder or base directory of the project. Under this directory, there may be several other folders like src, lib, bin, etc.

<target name=”init” description=”Delete the binary folder and create it again”>

All the tasks in the Ant build file are defined under the Target elements. Each Target element corresponds to a particular task or goal. A single target can consist of multiple tasks if needed. As stated before, the user can create multiple targets in a specific build file.

In the above XML code, we have created targets for the following goals:

  1. Deleting and creating directories
  2. Compiling the code
  3. Executing the test classes
  4. Generating the test reports

<target name=”exec” depends=”compile” description=”Launch the test suite”>

Sometimes it is required to execute a particular target only when some other target is executed successfully. Take note that the targets are executed sequentially, meaning they are executed in the order they are mentioned in the build file. Also, I would like to mention that a particular target is executed once and only once for the current build execution.

Thus, when the user is required to generate dependency between the target, he/she has to use the depends attribute. The value of the “depends” attribute shall be the name of the target on which it depends. A target can depend on more than one target as well.

Built-in Tasks

Ant build file provides a variety of tasks. A few of them are discussed below:

File Tasks: File tasks are self-explanatory.

  1. <copy>
  2. <concat>
  3. <delete>
  4. <get>
  5. <mkdir>
  6. <move>
  7. <replace>

Compile Tasks

  1. <javac> – Compiles source files within the JVM
  2. <jspc> – Runs jsp compiler
  3. <rmic> – Runs rmic compiler

Archive Tasks

  1. <zip>, <unzip> – Creates a zipped folder
  2. <jar>, <unjar> – Creates a jar file
  3. <war>, <unwar> – Creates a war file for deployment

Testing Tasks

  1. <junit> – Runs JUnit testing framework
  2. <junitreport> – Generates the test report by converting JUnit-generated XML test reports.

Property Tasks

  1. <dirname> – Sets the property
  2. <loadfile> – Loads a file into the property
  3. <propertyfile> – Creates a new property file

Misc. Tasks

  1. <echo> – Echoes the text message to be printed either on the console or written within an external file.
  2. <javadoc> – Generates the java-based documentation using the javadoc tool.
  3. <sql> – Establishes a JDBC connection and hits dash of SQL commands.

Execution

The easiest section is to execute the test suite with Ant. To execute the test suite with Ant, right-click on “build.xml” and select the “Run As -> Ant Build” option. Thus, the option hits the execution. See the following figure for further clarification.

execute the test suite

After the entire execution is completed, Ant generates a test execution report for review inside the “Report” folder.

The execution can also be initiated outside the eclipse by hitting the command on the command prompt. The user is expected to navigate to the directory where build.xml is kept and type “ant”.

Conclusion

In this tutorial, we emphasized useful information related to Ant, its installation, and various Ant tasks. Our motive was to at least introduce you to the basic conceptual picture and its importance as a tool altogether concerning testing. Hence, we discussed build.xml describing the various components.

The briefing Ant is a tool that automates the software-building process. Ant can achieve more than just the compilation of code – it can also handle packaging, testing, and much more with a few simple steps.

Next Tutorial #24: We will learn about Maven – a build automation tool. Maven simplifies the code handling and process of building the project. Most of the projects follow the maven structure. We will learn how to use Maven and Maven project setup for Selenium.

Was this helpful?

Thanks for your feedback!

Recommended Reading

8 thoughts on “Apache ANT – a Tool for Automating Software Build Processes and its Importance in Testing – Selenium Tutorial #23”

  1. Thanks for this. But again this tutorial is in a small length and without any example of a program. It would be great if a simple example could be included in this tutorial for example opening a google.com and and building that selenium program

    Reply
  2. Hi there,

    I am passing the variables as ant -DClient_secret run
    client_secret contain “=”. Ex:- P@ssw=ord=, Ant run is failing because ant doesn’t allow = anywhere.
    I have tried pass the secret value like this “P@ssw=ord=”. Still, i am getting the error.
    Please help me how to pass the variable like string contains anything.

    Thanks,
    sarath

    Reply
  3. ant is not getting compatible with cucumber

    is cucumber supports to ant 1.9.7 bcoz i am facing issues with jar versions i am using 1.0.2 cucumber-java and cucumber-junit , but not able to run test cases
    but in maven its working fine ?

    Reply
  4. I would like to implement the same in TestNG framework. Can i just change the junit as TestNG in the build.xml file. Will it work. Please clarify. If possible please share build.xml file for TestNG

    Reply
  5. I would like to implement the same in TestNG framework. Can i just change the as . Will it work. Please clarify. If possible please share build.xml file for TestNG

    Reply
  6. What a great work really. please keep it up with same way,I appreciate. Thanks a lot for define a tedious and complex article in a simple way.

    Reply

Leave a Comment