Would you like to react to this message? Create an account in a few clicks or log in to continue.


 
HomeHome  SearchSearch  Latest imagesLatest images  RegisterRegister  Log in  

 

 Overview of Ant - a Java based build tool

Go down 
AuthorMessage
sojan

sojan


Number of posts : 29
Age : 40
Registration date : 2007-03-04

Overview of Ant - a Java based build tool Empty
PostSubject: Overview of Ant - a Java based build tool   Overview of Ant - a Java based build tool EmptyThu Mar 08, 2007 4:56 pm

Ant
Ant is a Java-based build tool - if you're familiar with make (or gnumake, nmake, etc.), it's similar but "without the wrinkles" according to the publicity. What they mean is
* Ant build files are portable across platforms since they don't include shell commands
* Ant build files are written in XML so you don't have to get tabs and spaces right

DOWNLOADING AND INSTALLING ANT
Ant can be downloaded from
http://ant.apache.org
in binary format. Current binary versions include an appropriate XML engine, and provided that you have an appropriate Java Runtime Environment on your system, you shouldn't need anything else to get started. Do be aware that there are may options in ant that will require extra downloads, and if you do a source download you'll have more work to do.

INSTALLING
Once you've got the binary distribution, untar it and put it into place:

bash-2.04$ su -
Password:
[root@dupiaza /root]# cd /usr/local
[root@dupiaza local]# tar xzf /home/bhajee/apache-ant-1.5.2-bin.tar.gz
[root@dupiaza local]# mv apache-ant-1.5.2 ant
[root@dupiaza local]# exit
logout

bash-2.04$


Set up your environment variables - the user account we used to write these was running bash, so we added

export ANT_HOME=/usr/local/ant
export PATH=$PATH:/usr/local/ant/bin

into the end of the ~/.bashrc file

A FIRST USE OF ANT

Let's use ant to control the compiling of the Well House Consultants' Java application called "Babysitter", which comprises five .java source files. The Java source files are in a subdirectory called Babysrc, and we're going to put the class files into a subdirectory called Babyclass.

Before we compile for the first time, here are the files we have:

bash-2.04$ ls -l Babysrc Babyclass
ls: Babyclass: No such file or directory
total 20

-rw-rw-r-- 1 trainee graham 1227 Mar 23 11:45 Babysitter.java
-rw-rw-r-- 1 trainee graham 573 Mar 23 11:46 BaseFilm.java
-rw-rw-r-- 1 trainee graham 317 Mar 23 11:46 Cinema.java
-rw-rw-r-- 1 trainee graham 293 Mar 23 11:46 Hire.java
-rw-rw-r-- 1 trainee graham 231 Mar 23 11:46 Tv.java
bash-2.04$


Let's now specify our build file - it's called build.xml and it contains

Run ant to perform the compiles ...

bash-2.04$ ant
Buildfile: build.xml

compiler:
[mkdir] Created dir: /home/trainee/j2/Babyclass
[javac] Compiling 5 source files to /home/trainee/j2/Babyclass

BUILD SUCCESSFUL
Total time: 25 seconds
bash-2.04$


and see the results in the extra files and directory created:

bash-2.04$ ls -l Babysrc Babyclass
Babyclass:
total 20
-rw-rw-r-- 1 trainee graham 1407 Mar 23 11:51 Babysitter.class
-rw-rw-r-- 1 trainee graham 622 Mar 23 11:51 BaseFilm.class
-rw-rw-r-- 1 trainee graham 328 Mar 23 11:51 Cinema.class
-rw-rw-r-- 1 trainee graham 320 Mar 23 11:51 Hire.class
-rw-rw-r-- 1 trainee graham 222 Mar 23 11:51 Tv.class

Babysrc:
total 20
-rw-rw-r-- 1 trainee graham 1227 Mar 23 11:45 Babysitter.java
-rw-rw-r-- 1 trainee graham 573 Mar 23 11:46 BaseFilm.java
-rw-rw-r-- 1 trainee graham 317 Mar 23 11:46 Cinema.java
-rw-rw-r-- 1 trainee graham 293 Mar 23 11:46 Hire.java
-rw-rw-r-- 1 trainee graham 231 Mar 23 11:46 Tv.java
bash-2.04$


THE BUILD FILE


The key to the use of Ant is the build file (default name build.xml) in which you describe what you want to build and how.

Your work is divided into projects, with projects in the build file being enclosed in to tags. Within each project, you will have a series of actions that you may wish to perform - compile, tidy up, make a jar, all of these, and each of these sets of actions is known as a target.

Our example build file contains just a single target at the moment, and we've told Ant that it's the default target for the project (i.e. what Ant is to do if it's simply instructed to run on the project).

Within a target, we specify a number of tasks to be performed to reach that target. There are two tasks within our single target in this example - a mkdir and a javac.

DEPENDENCIES

Why do you compile a source file? To generate an up-to-date class file. Do you need to recompile a source file if it hasn't been changed since last time you compiled? No you don't, but if you're using a manual or batch file system you might well do do just to be on the safe side, or because your tools are very simple and always compile.

Ant will check whether or not actions are necessary and will only perform those tasks that are necessary. If we re-run the Ant command from above:

bash-2.04$ ant
Buildfile: build.xml

compiler:

BUILD SUCCESSFUL
Total time: 8 seconds
bash-2.04$



we'll see that nothing was done as all the class files were up to date. Should we now edit one of the Java source files and run ant again, it will spot what it has to do:

bash-2.04$ ant
Buildfile: build.xml

compiler:
[javac] Compiling 1 source file to /home/trainee/j2/Babyclass

BUILD SUCCESSFUL
Total time: 26 seconds
bash-2.04$


FURTHER ASPECTS OF ANT

Ant build files use properties - variables, if you like - which are set with property directives, and used with a $ notation rather like a Perl variable. They also support DataTypes which allow you to perform validation and specific tasks on (for example) lists of file names.

There are many tasks built in to Ant, but if you want you can add your own tasks as well - and of course there are many more attributes to even the few tags that we've looked at so far. Finally, the issue of dependencies goes further; dependencies are formed into a tree, so that a change made in some obscure part of an application's source or configuration can effect the reconstruction of a whole series of elements if necessary.

Ant has grown rapidly from an offshoot of the Jakarta (Java) project at Apache into a project in its own right. Both O'Reilly and Sams publish books on it, and they're not slim volumes either
Back to top Go down
http://sojanctherakom.googlepages.com
 
Overview of Ant - a Java based build tool
Back to top 
Page 1 of 1
 Similar topics
-
» Download Java Runtime Environment
» Opinion for a good build file

Permissions in this forum:You cannot reply to topics in this forum
 :: Programming Languages :: JAVA-
Jump to: