03 ANT Notes
03 ANT Notes
ANT
(Another Neat Tool)
[ANT Topics List]
Definition of ANT
History of ANT
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.
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.
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.
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.
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/.
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
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.
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
In this way we can achieve single point of modification to the execution sequence of commands.
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
ANT software installation is completed, now check its folders and jar files to update required environment variables.
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.
Flow below steps to create and update above environment variables permanently
1. Right click on "My Computer", Click on "Properties",
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.
It means you did not update Path environment variable correctly, double check it again.
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!");
}
}
<!--
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">
<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>
<!-- 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>
<!-- 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="clean">
<delete dir="${build.dir}"/>
</target>
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).
<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”.