Apache Ant - Building Simple Java Projects

In this tutorial, we will introduce you to Apache's Ant technology, which is a mean to build java projects.

Apache Ant - Building Simple Java Projects
About the Tutorial
In this tutorial, we will introduce you to Apache's Ant technology, which is a mean to build java projects. Ant is an open source tool that is easy to use and platform independent. This tutorial serves the purposes for both the beginners and experienced individuals. Going through it, a beginner can easily and quickly learn how to use Ant to build his/her own java projects; and if you are already exposed to Ant, then also you will be benefited by this tutorial learning more and advanced features of Ant. Introduction to Ant and its history, from installing it on your system to how to run and use it - every aspect will be described here with illustration and examples.

Ant Definition
Apache Ant is an open source, cross-platform based build tool that is used to describe a build process and its dependencies and implemented in XML scripts using Java classes that ensures its extensibility to any development environment (based on Java) and its integrity with other build tools.

Why 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 facilitates 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 crossplatform behavior. Ant is applicable to any integrated development environment based on java technology and therefore easy to use in building java projects across any platform. 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.).

7.xml. The best features of the Ant technology can be summarized as below y Easy to Use: It is not a programming language. It is easier to implement than any specific IDE because it is automated and ubiquitous. Ant is based on XML and uses java classes in automatic generation of build processes that makes it platform independent. Since then.1 of Ant was released on 27 June. With the advent of time. In contrast. The use of shell commands in make brings about the integrity with other languages too but this also makes it platform specific. A build file is generally named as build. Installation In this section. therefore easy to understand and implement. 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. The latest release of Ant is version 1. It is available for download at . it gained popularity and used widely. Its first independent release as a Jakarta subproject was made in the year 2000. The current version 1. i. It is a similar kind of tool like make but it does not use shell commands to extend the functionality.7. y Portable and Cross-platform based: Uses of Java classes makes it portable.History of Ant The development of Ant technology originated as an integral component of Tomcat application server based on Java Servlet and Java Server Faces. Ant has been used as a major tool in building java projects and now it has become a top-level Apache project for server-side solutions.. you will learn how to install Ant into your system.1. it can be run on any operating system. it is an XML based scripting tool. that¶s why its functionality can be extended to any development environment based on java. y y 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. It is applicable to any integrated development environment (IDE) that uses java. Ant. as a part of Apache Jakarta Project is an open source solution based on java platform. y Handling Dependencies between Targets: An Ant Project describes the target and tasks associated with it and also handles dependencies between various targets and tasks. y Extended Functionality: Ant is based on java platform.e. 2008. Introduction to Apache Ant (Another Neat Tool) Ant is an open source build technology developed by Apache intended to build processes in Java environment.

cgi.xml file contains only one project name and at least one target.xml file is the backbone of ANT (Another Neat Tool) technology. Ant is properly installed in your system. You may say that build. click it. Now.http://ant.1\lib. The name attribute is used to specify the project name and the default Requirement not necessary not necessary not necessary . Each build. to Path variable. Now. then it indicates that your installation is successful. To do this.zip archive to download the zip file. to CLASS_PATH variable.7.. path for the Ant directory will be C:\apache-ant-1. Set the path value C:\apache-ant-1. How to generate build. If the following message appears.7. Here.7. Therefore. You will see a button named "Environment Variables" appears. you have to check whether the installation process is done properly or not. unzip this file and install the apache-ant-1. we have to set the value C:\jdk to JAVA_HOME variable and the value C:\apache-ant1.org/bindownload. Select the item "System" in the Control Panel of your system and click on the tab named "Advanced". The project tag has only three attributes: Project Attribute Description name project name default target name which called by default the base directory having all path..1 folder to the root directory of C:\ drive.7. viz. Again in the System Variables. you have to click on the link apache-ant-1. we have to set some environment variables. After the download completes.1bin."> The project tag is used to create our project and its attributes are used to handle further processing of the project.7.xml file This example shows how to generate the build. ANT_HOME and CLASS_PATH. you will find two user variables.1.apache.zip in the . it basedir contain absolute path <project name="My Project" default="compile" basedir=". Under Environment variables.1\bin.xml file. open the Command Prompt and run the command C:\>ant.7.1 to ANT_HOME variable and the path value C:\apache-ant-1.

i. the target will not be executed and the description attribute is used for only giving details about target. then this target will be executed and unless attribute is used as like else condition whether property does not exist. one target may depend on one or more other targets.. the name of the property that must not be set in order for this target to unless execute. Next tag is property tag which has following four attribute: Property Attribute Description name of the property. name attribute is used for target name and the depends attribute is used to give sequence of target.e. which target will execute first. The if attribute is used in case of condition. description short description about target <target name="buildWar" depends="init" description="build a war file"/> <target name="init" if="build"/> <target name="init" unless="build"/> In the target tag. An another tag is Target tag which has following five attributes: Target Attribute Description name target name depends depends on another target the name of the property that must be if set in order for this target to execute.attribute is used to specify which target will be called as default when the program will run and the basedir attribute is used to calculate the absolute path of the parent directory. whether property exists or not. which is casename sensitive name of task attribute. this is done by placing the property name between value "${"name }" in the attribute value location it contain property name file name of the property file <property name="build" value="${build}"/> Requirement not necessary necessary not necessary not necessary Requirement necessary not necessary not necessary not necessary not necessary .

build" value="build"/> <property name="dir.build}"/> <mkdir dir="${dir.build}"/> </target> </project> The above build."> <jar jarfile="${dir. The output is as follows: . and the location tag is used to specify the location of task where it performs and the file tag is used to import all properties of ant file. The complete build. name attribute is used for property name.src" value="src"/> <property name="dir. then run with ant command on the command prompt."> <property name="dir.jar" basedir="${dir.dest}"/> <mkdir dir="${dir.jar file in to the 'dest' directory.xml file structure is as follows: <project name="My Project" default="jar" basedir="."> <delete dir="${dir. To check the program.dest}"/> </target> <target name="prepare" depends="clean"> <mkdir dir="${dir.<property name="src" location="src"/> <property file="build. compile source code.build}"/> </target> <target name="jar" depends="compile" description="Generates Roseindia.dest" value="dest"/> <target name="clean" description="Removing the all generated files.dest}/roseindia. The value tag is used to give the task which will be the name of property in this format "${name}".properties"/> In the property tag.build}"/> <delete dir="${dir. create jar file and clean the directory if already exists."> <javac srcdir="${dir. simply copy and paste the above code and give appropriate path.src}"/> </target> <target name="compile" depends="prepare" description="Compilation of all source code. it is case sensitive.src}" destdir="${dir.xml file is used to create directory.

out. class Hello { public static void main(String args[]){ System. When you make a java file in the src folder and run with ant command.println("sandeep kumar suman"). then the following output will be displayed: Download Source Code .The output shows that a jar file named roseindia. compile it with ant command on the console. the jar file is created completely.jar is created but in this jar file only manifest file is created. } } To check your program.