ANT: A Build Tool - Tutorial

Hi All,

This information is dedicated to my those CS/IT engineer friends(obviously complete engineers) who have zeal to learn and explore unknown things. Recently I learnt this tool and hoping this information can be of any help to wide audience over here at any point of time in their future.
[ Note: This tutorial is entirely written by me after learning various information already available on web.]

Prerequisities: Before reading this post, make sure you are familiar to Java.

Ant Build tool

Ant is free and open source build tool written in Java and helps in automating the entire build process of a Java development project. It uses xml build files. By default ant look for build file named build.xml. This xml file basically contains information about how to build [build is just nothing compiling your java classes.] any particular project.

Installation of Ant class.

Step 1: Just download the latest Ant distribution –Check this out
Apache Ant - Binary Distributions
Step 2: Extract it in any location [e.g I have used H:\ apache-ant-1.7.1]
Step 3: Set the following system variables in your system
ANT_HOME =H:\ apache-ant-1.7.1 [Give the path of directory in your system where you extracted Ant distribution.]
PATH= %ANT_HOME%\bin
Step 4: Now checking whether installation is proper or not. Start->Run->cmd
Step5: Type ant –version [Any location]
If you get following screen that means Ant is installed successfully

[​IMG]

But there may be a case that sometimes it is not able to find appropriate Java path, in that case it may show error like following “Tools.jar[or any other jar] not find in JAVA_HOME. In that case, it would always be better to set JAVA_HOME variable in System properties. See Step 6 in such cases else you can skip that step.
STEP 6: Set the following system variables in your system
JAVA_HOME = C\Program Files\Java\jdk1.6.0_02 [Path of JDK installed directory in your system. Here I have shown my local]
Now add JAVA_HOME variable in PATH variable, make sure it is before ANT_HOME.
PATH = %JAVA_HOME%\bin ; %ANT_HOME%\bin
Step 7: Now again go to Step 5 and see if you are getting that screen showing successfully installation of Ant or not.
Now after done with the installation part, we can proceed to compile a simple Java file as well as can create a jar file out of it just simply using a build file.
Step 1: Create a sample folder named as “Ant Example” in your local system [Choose any path”
Step 2: Create a java file in it [Location: I:\Ant Example] , let’s say HelloWorld.java having following code in it.
public class HelloWorld {
    
public static void main(String[] args){
        
System.out.println("Hello World !");
    
}
}
Step 3: Now comes the actual part of creating a build.xml file.
Create a build.xml file. Simply open the notepad and type following piece of code in it and save as .xml file in the same location as you have saved your .java file




 Compilation Complete! 



 Building .jar file Complete! 


Now the thing that might be troubling you all would be what is happening in this build.xml file right?

Line 1: There is nothing to tell - it is a part of every xml file.
Line 2: element is the root element in all ant build files , name indicates the name of our project or simple java class file.
Line 3: element - it represents a single stage in the build process. Each project element can have multiple targets. In theabove file we have two targets - "complie" [For compiling the java file] and second is "compress" [For compressing the .class file of java file into a jar file] If you see the "compress" taget it depends on "compile" target. So "compile" target should be executed first and then "compress" target.
Line 4: “compile” target has two task elements and . element is nothing just a command used to compile .java files. Its attribute srcdir=”.” Indicates all the files in the current directory i.e. HelloWorld.java file
Line5: is just like println for printing the text on the console.
Line 6: In “compress” target, depends attribute indicates that this target is dependent on the execution of “compile” target. It would execute only if “compile” has executed successfully.
Line 7: “compress” target also has two task elements and . element as the name indicates is used to build a jar file. Attribute destfile =”HelloWorld.jar” indicates the name of the destination file created, basedir =”.” Indicates the all the files in the current directory and includes =”*.class” means all those files should be considered which ends with .class extension. So this would take only .class files to create the jar.

Now come the running of this build.xml file

Step 1: Start->Run->cmd
Step 2: Go to Ant Example directoty. In above example “I:\Ant Example”
Step 3: Type ant.
Step 4: See the following window and get surprised to see HelloWorld.class and HelloWorld.jar files created as an outcome of above learning in the folder I:\Ant Example.

[​IMG]

Expecting a good variety of questions.

PS: More information yet to come.

Thanks !

Replies

  • Kaustubh Katdare
    Kaustubh Katdare
    Awesome tutorial, Sookie .
  • crazyroshan
    crazyroshan
    AWESOME 😀 u taught me basics in 5 minutes...... m moving on to complex things of the same.....believe me it really helped....infact i registered to comment to this post only...keep up the good work...ciao 😁

You are reading an archived discussion.

Related Posts

Steganography is the art and science of writing hidden messages in such a way that no one apart from the sender and intended recipient even realizes there is a hidden...
Hello CE’s, This is my first post, I found this great forum while looking for people who liked engineering (amateurs, undergraduates or even graduates…) and have an absolute ignorance of...
Is there any difference in the way firewalls treat ICMP packets for ping and traceroute commands? a ping shows a succesfull response where as traceroute shows a timeout at one...
Hi to all. I got a doubt while I'm executing my program in ALP. it may be silly but though you should think for a while...... Why can't we execute...
HI friends😉, Recently our professor giving introduction to Data Communications had mentioned a computer with multiple displays and keyboards will be needed in some situations. The problem here is I...