0% found this document useful (0 votes)
11 views9 pages

03 ANT Notes

Uploaded by

Haribabu K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views9 pages

03 ANT Notes

Uploaded by

Haribabu K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Ant -------------------------- Naresh I Technologies

ANT
(Another Neat Tool)
[ANT Topics List]

 Definition of ANT

 What is a Build Tool?

 Need of ANT Software

 Advantages and Features of ANT

 History of ANT

 Comparing ANT with some known build techniques

 ANT Software installation

 ANT software folder hierarchy

 Setting environment with ANT software

 Verifying ANT installation

 Finding different options of ant command

 ANT Basics

 Sample Programs

Definition of ANT
Ant is a Java based, open source, platform-independent build automation tool.
It is written purely in Java for automating Java projects build process.

Ant is a powerful technique that helps the developers to convert their developmental structures in deployment structures.

ANT software is developed by James Duncan Davidson to help building Tomcat.

It is developed in XML scripting using Java classes that ensures its extensibility to any development environment based on Java.

Ant is a free tool under the GNU License and is freely available at http://jakarta.apache.org/ant/

What is a Build Tool, build process, and why ANT software is called Build tool?
In ANT software terminology
- Build means “collection of all components of a project”.
- Build Process means “collecting all components of a project”. It means transforming the source into deployable and useable
software is called build process”.

In general terms,
- The war file creation process is called build process and
- that war file is called build

Build tool is software that provides a mechanism to describe the operations and structure of the build process. Since ANT software
provides a way to build Java projects, it is called Build tool.

To understand more about the above words, consider what is required to build a software system. Typically, there is much more to
building software than just typing in and then compiling the source code.

Naresh i Technologies, Page 1


Ant -------------------------- Naresh I Technologies

There are a number of steps required to transform the source into a deployable and useable software solution. The following is a
hypothetical build process you might use with a simple software system
1. Get the source. You may need to download or fetch the source from a source code repository (CVS/SVN). For this, you might
need to know the tag or version of the source code you want to build.
2. Prepare a build area. You will probably want to create a set of directories, perhaps according to some standardized
directory layout.
3. Configure the build. In this step, you will determine what optional components can be built based on the current
environment. You might want to set build numbers and version numbers to be included in the build.
4. Validate the source code. You may have a standard style guide and you wish to ensure all code conforms to this before you
build a release.
5. Compile the source code
6. Build the compiled code into libraries potentially including non-code resources such as properties, images and sound files.
7. Run the system's tests to validate the build.
8. Build the documentation for the software. This may range from something as simple as collecting text files up to processing
content through some form of publishing system to produce the documentation in its final form
9. Package up all of the components of the software – code, resources, images, documentation, etc. – into a deployable
package. You might need to produce several packages in different formats for different target users
10. Deploy the software to some standard location for use or distribution

If you try to use a manual process for building your software you would find it to be tedious, error prone and, in general, not very
repeatable. You might forget to set the version number or to provide a tar file for Unix users. You might change the directory
structure, confusing users who upgrade from the previous version of the software. Even worse, you may forget to test the software
and ship a version that may not even work. Such ad-hoc build processes are always a source of problems and the best solution is to
automate the build process. The tools you use to automate the build process are known, unsurprisingly, as build tools. Ant is such a
tool.

Need of ANT
Ant is developed by Apache Foundation specifically to build projects based on java platform.

But why to use Ant if there already exists other build tools like make, jam and many. Tool like make uses shell commands that
facilitate integration of other tools to extend its functionality on the operating system where it runs. But this limits the functionality
of the build tool specific to that OS only, e.g., make is specific to the UNIX platform. Ant has overcome this shortcoming as it uses
java classes with XML scripting that makes it portable through cross-platform behaviour.

Ant enables automatic generation of build processes that is more reliable than manual procedures. Ant can also compile source code
from version controls and package the compiled code and other resources (JAR, EAR, TAR etc.).

It allows the developer to automate the repeated process involved in the development of J2EE application. Developers can easily
write the script to automate the build process like compilation, archiving and deployment.

A build process is an essential part of any development cycle because it removes the gap between the development, integration, and
test environments. It also helps to remove other issues while deploying such as compilation, classpath, or properties that cost many
projects time and money.

Advantages of ANT over other Build techniques


1. Since the software is developed in JAVA, the ANT script file is Platform independent, hence no need to write separate file per
Operating system.
2. Since it accepts instructions in the form of XML documents thus it is extensible and easy to maintain.
3. Commands are executed in a controlled manner; if one command execution is failed its dependent command execution can
be terminated.
4. Executes only the required commands.
5. Single point of modification of order of commands execution
6. Like this we have many other advantages, you will learn them as part of course

Features of ANT
The best features of the Ant technology can be summarized as below -
Easy to Use: It is not a programming language, it is an XML based scripting tool, therefore easy to understand and implement.
Portable and Cross-platform based: Uses of Java classes makes it portable, i.e., it can be run on any operating system.
Extended Functionality: Ant is based on java platform, that’s why its functionality can be extended to any development
environment based on java. It is easier to implement than any specific IDE because it is automated and ubiquitous.
Build Automation: Ant provides automated build processes that is faster and more efficient than manual procedures and other
build tools can also be integrated with it.
Compilation of Source Code: Ant can use and compile source code from a variety of version controls and packaging of the
compiled code and resources can also be done.
Handling Dependencies between Targets: An Ant Project describes the target and tasks associated with it and also handles
dependencies between various targets and tasks.

Naresh i Technologies, Page 2


Ant -------------------------- Naresh I Technologies

History of ANT
The following is a brief history of Ant. It will give you a flavour for the evolution of Ant.

Ant started life as a quick hack by James Duncan Davidson to help building Tomcat, which was, at the time, Sun's servlet engine in
development. Ant was originally developed on a Windows laptop, which some people find surprising. James was instrumental in the
donation of the Tomcat codebase to the Apache Software Foundation by Sun to form the Apache Jakarta project. Included, indeed
some might say tucked away, in that donation was Ant. Apparently the name started out as an acronym for "Another Neat Tool".

Ant was added to the jakarta-tools CVS repository on October 9th, 1999. At this time, Ant was still considered part of the Tomcat
project. It underwent development as part of the Tomcat project and at around Christmas 1999, Ant was released as a part of
Tomcat 3.0. With this release, many people began to see Ant in operation and how it could be used to build software. Already
people within the Apache Jakarta and XML projects had begun to use Ant to build other projects besides Tomcat. For example the
Xerces XML parser project began using Ant in November 1999.

It became clear, with Ant's increasing popularity, that it was not really suitable to remain a part of Tomcat and that it should become
a top level sub-project of the Apache Jakarta project. In February 2000, the code for Ant was moved to its own repository, jakarta-
ant, at Apache. After some significant development work, Ant 1.1 was released in July, 2000. This was the first official release of Ant
as a separate entity. To avoid confusion with the version released with Tomcat, there was never a 1.0 release of Ant. In the end, the
original release as part of Tomcat 3.0 came to be known as Ant 0.3.

In November 2002, The Apache board promoted Ant to a top–level Apache project.

Since that initial release of Ant 1.1, there have been regular updates to Ant. For latest release check ANT home page
http://ant.apache.org/.

Comparison of ANT with some known build techniques


Before ANT, developers used some kind of extension files for executing more than one command / task on command prompt (md,
cd, copy, delete, javac etc), the extensions are .bat (batch), .cmd, .sh, etc... So with these files developer need not have to executes
each command on command prompt, instead developer can write all these commands on a plain notepad, save with .bat extension
and runs for many times

Below I have given problems with these files, and solutions for these problems using ANT

Problem 1:
If developer write one bat file for WINDOWS OS, the same file need to developed for other OSs also

Solution with ANT


Instead of running .bat file on OS (C:\>a.bat), ANT tool written in Java programming language (it is a java class). This tool nothing but
a java class runs on any OS, but developer must write and give all the commands in a plain notepad file / text editor, save the file
with .xml extension and passes into ANT tool (java class). ANT tool reads each command from .xml file, verifies current OS and
converts commands into OS specific command

"ANT is a Platform Independent Build/Batch Process Automation Tool"

Problem 2:
In a batch file if javac command followed by java command is written, batch runs each command one after another (sequential
manner) without verifying whether previous command(s) are executed.

Solution with ANT


In ANT all the commands are written and given in a XML file [called build.xml], when writing commands in this file, we can link one
command to another command. If first command execution fails the second command won't execute and the batch processing will
come out with a detailed error statements.

“This way ANT executes commands in a controlled manner”

Problem 3:
In .bat file if the sequence of commands execution wants to be changed we need to cut and paste the sequence of commands

Solution with ANT


Though all commands are written in sequence they are not executed in given sequential manner. Their sequence must be specified
at the end of the document. If the commands sequence want to changed we must modify last statement but not the whole XML
document

In this way we can achieve single point of modification to the execution sequence of commands.

Naresh i Technologies, Page 3


Ant -------------------------- Naresh I Technologies

ANT Installation procedure


Download ANT distribution file from Apache home site http://ant.apache.org/.

Click on http://ant.apache.org/bindownload.cgi. It takes you to download page

Download zip extension file for Windows and tar.gz extension file Linux/Solaris OS.

Installing ANT software is simply extract that file into a drive, assume “C” drive. Once it is extracted you will find a folder
apache-ant-<version>
Ex: apache-ant-1.8.1

Therefore, The ANT_HOME will be C:\apache-ant-1.8.1.

ANT software installation is completed, now check its folders and jar files to update required environment variables.

ANT folder hierarchy

Directories provided with Ant


bin - Batch files, Perl scripts, and shell scripts for running Ant.
docs - Ant documentation.
lib - Libraries required by Ant to run.
src - Source code for Ant. Provided only in the source distribution.

Like other softwares,


 all its binary files are stored in bin folder and
 all its library files are stored in lib folder

Ant.bat is the main binary file used to execute build.xml. Hence, it must be accessed throughout the system.

In order to use this file from all drives and folders of computer we must create and update below environment variables.

Environment setup for ANT


Below environment variables must be created or updated to use ANT software.
 JAVA_HOME
 ANT_HOME
 Path

Flow below steps to create and update above environment variables permanently
1. Right click on "My Computer", Click on "Properties",

Naresh i Technologies, Page 4


Ant -------------------------- Naresh I Technologies

Below window is opened

2. Click on "Advanced System Settings" hyper link, below window is opened

3. Click on "Environment Variables" button, below window is opened

4. In System Variables portion click New button, to create ANT_HOME environment variable, below window is opened

5. Enter
Variable name as- ANT_HOME
Variable value as- C:\apache-ant-1.8.1
As show in the below diagram

click on Ok
It is always good practice to set software home directory with an environment variable.

6. Follow the same above steps and create JAVA_HOME environment variable, if not yet created.
7. To add ant binary files path, select Path environment variable and click on Edit button,

8. Press End key on your key board, press “;” and then add the path
C:\apache-ant-1.8.1\bin.
9. Click on Ok till all windows are closed.
Note: No need to set CLASSPATH environment variable for ANT.

Now your system is updated with ANT software. Rock !!!!! now.

Verifying ANT installation


Flow below steps to check ant installation

1. Open command Prompt [window+R, type cmd, press Enter key]


2. Type the command ant, press Enter key. If the following message

Naresh i Technologies, Page 5


Ant -------------------------- Naresh I Technologies

appears, then it indicates that your installation is successful;


Ant is properly installed in your system.

3. If the following message is appeared,

It means you did not update Path environment variable correctly, double check it again.

Finding different Options of ANT command


Type below command at command prompt, it shows all options of ant command 
C:\>ant –Naresh Technologies

Sample Programs

/**
* Below program explains compiling and executing using ant script
*
* Create a folder "anttest"
* Save this file with HelloWord.java in this folder.
*/
package anttest;
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}

<!-- Save this file as build.xml -->


<project name="test" basedir=".">
<target name="compile">
<javac srcdir="./anttest"></javac>
</target>
<target name="execute">
<java classname="anttest.HelloWorld"></java>
</target>
</project>

<!--
Execution:
1. Open command prompt
2. Change directory to current working directory
3. type the command "ant compile execute"
-->

<!-- build.xml with the attribute "depends", and execute with the command "ant execute"-->
<project name="test" basedir=".">
<target name="compile">

Naresh i Technologies, Page 6


Ant -------------------------- Naresh I Technologies

<javac srcdir="./anttest"></javac>
</target>
<target name="execute" depends="compile">
<java classname="anttest.HelloWorld"></java>
</target></project>

<!-- build.xml with the attribute "default", and execute with the command just "ant"-->
<project name="test" basedir="." default="execute">
<target name="compile">
<javac srcdir="./anttest"></javac>
</target>
<target name="execute" depends="compile">
<java classname="anttest.HelloWorld"></java>
</target>
</project>

<!-- build.xml with the attribute "default" with a target to have centralized place to change the order of execution of targets -->
<project name="test" basedir="." default="all">
<target name="compile">
<javac srcdir="./anttest"></javac>
</target>

<target name="execute" depends="compile">


<java classname="anttest.HelloWorld"></java>
</target>

<target name="all" depends="compile, execute"/>


</project>

<!-- build.xml to create directory and store class files in that directory -->
<project name="test" basedir="." default="all">

<target name="mkdir">
<mkdir dir="./build"/>
</target>

<target name="compile" depends="mkdir">


<javac srcdir="./src/anttest" destdir="./build"></javac>
</target>

<target name="execute" depends="compile">


<java classname="anttest.HelloWorld" classpath="./build">
</java>
</target>

<target name="all" depends="execute"/>


</project>

<!-- build.xml to create executable jar file, adding manifest attributes to execute class from jar -->
<project name="test" basedir="." default="all">

<target name="clean">
<delete dir="build"/>
</target>

<target name="compile" depends="clean" >


<mkdir dir="build/classes"/>
<javac srcdir="src" destdir="build/classes"/>
</target>

<target name="jar" depends="compile">


<mkdir dir="build/jar"/>

Naresh i Technologies, Page 7


Ant -------------------------- Naresh I Technologies

<jar destfile="build/jar/HelloWorld.jar" basedir="build/classes">


<manifest>
<attribute name="Main-Class" value="anttest.HelloWorld"/>
</manifest>
</jar>
</target>

<target name="execute" depends="jar">


<java jar="build/jar/HelloWorld.jar" fork="true"/>
</target>

<target name="all" depends="execute"/>


</project>

<!-- build.xml with property tag-->


<project name="HelloWorld" basedir="." default="main">

<property name="src.dir" value="src"/>

<property name="build.dir" value="build"/>


<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/>

<property name="main-class" value="anttest.HelloWorld"/>

<target name="clean">
<delete dir="${build.dir}"/>
</target>

<target name="compile" depends="clean">


<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}"/>
</target>

<target name="jar" depends="compile">


<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar"
basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>

<target name="execute" depends="jar">


<java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
</target>

<target name="main" depends="execute"/>


</project>

Creating a deployable WAR file from Eclipse Project


Below ant script shows preparing deployable war file of a web application, that is created using Eclipse and then deploy that war file
directly into Tomcat server.
Steps to prepare war file using Eclipse directly – not using ANT
Eclipse allows us to run the application and then export the application to a standard WAR file. But then someone has to manually
do the following steps:
 Export the project to a WAR file using Eclipse Interface
 Copy the WAR file from the location created to the Tomcat webapps folder

Here is a better alternative and a solution to the question:

Create an ANT script that will look at the Eclipse Project structure and build the WAR file and copy over the WAR file to the
destination (Tomcat webapps folder).

Here is the ANT script:

<project name="Deploy From Eclipse to Tomcat" basedir=".">


<property name="warfile" value="test"/>
<target name="unpack">

Naresh i Technologies, Page 8


Ant -------------------------- Naresh I Technologies

<unwar src="${warfile}.war" dest="${warfile}" />


</target>

<target name="create">
<war destfile="${warfile}.war" webxml="WebContent/WEB-INF/web.xml" update="true">
<classes dir="build\classes"/>
<fileset dir="WebContent">
<exclude name="WEB-INF/web.xml"/>
</fileset>
</war>
</target>

<target name="copy">
<copy todir="C:\apache-tomcat-6.0.26\webapps" overwrite="true">
<fileset dir=".">
<include name="*.war"/>
</fileset>
</copy>
</target>

<target name="deploy">
<antcall target="create"/>
<antcall target="copy"/>
</target>
</project>

Save the above code into a file and name it “build.xml”. Place the build.xml into the root folder of your eclipse project. For example,
if “C:\AntTest” is my eclipse workspace and “HelloServlet” is the name of my project then all the files related to the project will be
under “C:\AntTest\HelloServlet”.

Naresh i Technologies, Page 9

You might also like