Training, Open Source Computer Languages

This is page http://www.wellho.net/solutions/java-ove ... -tool.html

Our email: info@wellho.net • Phone: 01144 1225 708225

Overview of Ant - a Java based build tool


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

<project name="Babysitter App" default="compiler" basedir=".">

<target name="compiler">
        <mkdir dir="Babyclass"/>
        <javac srcdir="Babysrc" destdir="Babyclass"/>
</target>

</project>

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 <project> to </project> 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


See also The Ant build tool

Please note that articles in this section of our web site were current and correct to the best of our ability when published, but by the nature of our business may go out of date quite quickly. The quoting of a price, contract term or any other information in this area of our website is NOT an offer to supply now on those terms - please check back via our main web site

Related Material

Java - Introduction to Ant
  [694] Ant and Make - (2006-04-22)

Web Application Deployment - Java - Ant build tool
  [3155] Rake - a build system using code written in Ruby - (2011-02-03)
  [694] Ant and Make - (2006-04-22)

resource index - Java
Solutions centre home page

You'll find shorter technical items at The Horse's Mouth and delegate's questions answered at the Opentalk forum.

At Well House Consultants, we provide training courses on subjects such as Ruby, Lua, Perl, Python, Linux, C, C++, Tcl/Tk, Tomcat, PHP and MySQL. We're asked (and answer) many questions, and answers to those which are of general interest are published in this area of our site.

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/solutions/java-ove ... -tool.html • PAGE BUILT: Wed Mar 28 07:47:11 2012 • BUILD SYSTEM: wizard