Hiya folks! Do you have ANTs in your build? Just kidding! Actually, I am not. Agreed that it sounds weird, but don’t fear as I am here! (Now, that’s what I call, rhyming.)
Today, let us ponder about what exactly this Apache ANT is and why it takes its place as an important building block in the construction of a WebDriver framework. By the end of this post, you should be able to handle any ANT project in general.
Jumping right in:
Another Neat Tool provided by The Apache Software Foundation is a build tool written in Java. It is old (Maven is winning this space and Gradle is getting along), quite popular and can still be found in most of the projects. The beauty of Apache ANT is that it only does what it is told to do. There isn’t much magic that is happening under the hood. (Beware, this can be a double-edged sword at times!)
To create an executable would mean,
- Compiling the required .java files under the src folder
- Creating the Manifest file for the JAR
- Zipping all the .class files along with the manifest file
- Using the jar command to create the executable
But with Apache ANT all these steps can be handled with a simple Target (we will see what a Target is, in a minute!).
Everything to be achieved is specified in an XML file, in a structured and modular way making it very easy to identify if something breaks. And this file is known as the famous, “build.xml”.
Apache ANT comes with a default buildfile named, build.xml. We can always edit or create a new XML file to suit our needs. This is basically a command-line tool. So, it is very easy to run Apache ANT from the command prompt using the command, “ant build_file_name.xml”. The best part here is, in case you did not rename the build.xml (just modified the provided default or created your own and named it build.xml), then just the command, “ant” would automatically know to look for build.xml and do what it is made for!
Apache ANT gives a lot of power, enough to cross the Thank You threshold! We can,
- Clean up the project
- Compile source code
- Generate a distributable JAR or WAR
- Handle code from version control systems such as CVS, SVN etc.
- Echo messages to loggers and listeners at a specified level (error, warning, info, verbose, debug)
- Create or delete directories
- Copy, move, delete files
- Can zip, unzip, tar, untar, unjar, unwar files
- Execute JUnit tests, test scripts etc.
- Execute a series of SQL statements via JDBC to a database
- Generate JUnit test report
And do much much more…
And guess what, it sounds intimidating but every single thing that I have mentioned so far can be achieved with just the buildfile.
Time to juggle with the elements of the much-hyped, buildfiles. These are written in XML.
Sticky Note: Below information is enough to get familiarized with an Apache ANT buildfile. For in depth knowledge, please visit the Apache ANT user manual. (http://ant.apache.org/manual/)
Each buildfile has,
- Project – contains a minimum of one Target
- Target – set of Tasks that perform a unit of work
- Task – piece of code that can be executed
Sample build.xml file is as follows,
Let us distill the buildfile one tag at a time.
Project: This has three attributes.
- name – The name of the project
- default – The default target that should be executed in case nothing is provided
- baseDir – The base directory from which the relative paths in the file are calculated. “.” refers to the current directory from which execution takes place
Target: As shown in the sample buildfile, a project can have one or more targets. Target is a set of Tasks. We can select which targets we would like ANT to execute by their names separated by commas. When no target is specified, then the default target is executed.
The important attribute to note here is “depends”. This specifies the target name that it is dependent on. For example, the compile target will execute only after its dependent, init target is executed. Thus, depends attribute specifies the order in which targets are to be executed.
Task: This is a piece of code that can be executed. Each task can have one or more attributes as key-value pairs.
<name attribute1=”value1” attribute2=”value2” … />
name is the name of the task, attributeN and valueN are the attribute and value names respectively.
Here is a long list of built-in tasks that come right out of the box. We can code our own tasks as well. (Remember? It is all written in Java).
You must be wondering, why to go so deep into build files rather than just installing it straight away and start some action! There is a reason for imparting so much ANT knowledge. And you will appreciate me for that as we go further in our journey. Just wait and watch…
Down to business now!
Step 1: Go to, ‘https://ant.apache.org/bindownload.cgi’ and click on ‘apache-ant-1.10.2-bin.zip’ to download the .zip file under ‘Current Release of Ant’.
Step 2: Once the .zip archive is downloaded, extract all the files to a directory on your local machine.
Step 3: Set the environment variables, ANT_HOME and Path as follows,
Right click on ‘Computer’ -> Properties -> Advanced system settings -> ‘Advanced’ tab -> Environment Variables -> Click ‘New’ under system variables.
ANT_HOME is set to the path of the folder in which Apache ANT files are extracted.
Similarly edit the Path variable to include %ANT_HOME%\bin.
Step 4: Verify if Apache ANT is installed by typing the following command in ‘Command Prompt’
Step 5: Next task is to open Eclipse IDE,
- Right-click on the Java project -> Export
- Under ‘General’, select ‘Ant Buildfiles’ and click ‘Next’
- Make sure the required project is selected
- Uncheck ‘Create target to compile project using Eclipse compiler’ in order to remove any dependency on Eclipse
Click on ‘Finish’ to see an eclipse auto-generated build.xml file.
And that’s it! You are all set to run your project as ANT build. It is laughably simple, isn’t it?
Sticky Note: Eclipse comes integrated with ANT. In order to make sure Eclipse’s ‘Ant Home’ points to the latest version of ANT, click on Windows -> Preferences -> Ant -> Runtime. Click on the ‘Classpath’ tab. Expand ‘Ant Home Entries’ and verify the path. If it is pointing to a different version, then click ‘Ant Home’ and browse the location of the folder in which Apache ANT files are extracted.
In our next post let us do just that and generate a JUnit report with the help of an ANT target.