10 Minutes Guide to Ant

Previous Tutorial Index Next

Introduction
Well for the next 10 minutes get ready to devote to the ant guide. This will make some sence to the ant. Ant is a free tool under GNU Licence and is freely available at http://jakarta.apache.org/ant/ , current version of ant is 1.4.1. Ant 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. For this tutorial I am using 1.4.1 and jdk1.4. I am also using Jboss 3.0 (with tomcat) downloaded from http://www.apache.org to deploy and test J2EE application developed in this lesson. In order to understand this tutorial you should have a solid knowledge of java programming and development of web application using java technologies.

Lesson 1
Downloading and Installing Ant Before installing Ant make sure you have JDK1.3 or above installed on your machine. For this tutorial download ant from http://jakarta.apache.org/ant/ and unzip the file into your favorite directory. Set the class path to the bin directory of the ant. Let's assume that Ant is installed in c:\ant\. The following code has to be put into autoexec.bat file: set ANT_HOME=c:\ant set JAVA_HOME=c:\jdk1.3 set PATH=%PATH%;%ANT_HOME%\bin

Testing Ant Go to command prompt and issue the following command.

Ant uses configuration file called build.xml file which compiles a java file present in src directory and places compiled class file in build/src directory. Now here is code of simple build. building and deploying. Now its time to do some work with Ant.xml file build. <?xml version="1.xml is a xml file used by ant utility to compile.xml to work.Build file for our first application --> <project name="Ant test project" default="build" basedir=". build and deploy or run the application. This is the file where you defines the process of compiling."> <target name="build" > <javac srcdir="src" destdir="build/src" debug="true" includes="**/*.C:\anttest>Ant Buildfile: build.0"?> <!-.xml does not exist!Build failed C:\anttest> If every this is installed correctly Ant will give the above message. Writing build.java" /> </target> </project> .

Name of the default target to use when no target is supplied. Third line is the project tag. default and basedir.out. One project may contain one or more targets. The project tag: <project name="Ant test project" default="build" basedir=". Each buildfile contains one project tag and all the instruction are written in the project tag.println("This is example 1"). Here is the code of our test1.java file which is to be compiled by the Ant utility.First line of the build. In this example there is only one target.xml file represents the document type declaration. } } . Next line is comment entry. Name of the base directory from which all path calculations are done."> requires three attributes namely name.java" /> </target> Which uses task javac to compile the java files. class test1{ public static void main (String args[]){ System. All the attributes are required. <target name="build" > <javac srcdir="src" destdir="build/src" debug="true" includes="**/*. Here is the description of the attributes: Attribute Description name default basedir Represents the name of the project.

xml build: [javac] Compiling 1 source file to C:\anttest\example1\build\src BUILD SUCCESSFUL Total time: 4 seconds C:\anttest\example1> Above mentioned process compiles the file and places in the build\src directory. files will be unzipped in anttest directory.Download the code of this tutorial and unzip it in c:\.0 . You will receive the following out put. build and deploy your web application to the Jboss 3. In the next lesson I will show you how to use ant to compile. C:\anttest\example1>ant Buildfile: build. To run the Ant utility to compile the file go to c:\anttest\example1 and issue ant command.

Sign up to vote on this title
UsefulNot useful