Apache Ant

Edited by Lim Siong Boon, last dated 22-Sep-2010.

email:    contact->email_siongboon  

website: http://www.siongboon.com

Why use Apache Ant?

     I thought that I will never have to use Ant. The Eclipse IDE is fairly powerful with much automation task. This is until I start to deploy my java programs. Especially those java web application where they have a unqiue file structure from normal standalone java applications. I had to manually setup and copy files for deployment. There are so many steps, and the only way to remember what to do is to have them documented step by step. This is the reason I have built up www.siongboon.com, which is my documentation site. As things get more and more complex, the process gets longer and more tedious than before. I had to repeat the tasks for each deployment testing.

    Apache Ant is the solution to all my problem. It is an automated script that allows programmer to do various type of task. Almost any task can be automated. For example compile, build, copy, delete files and directory. The script releases me from the tedious and repective tasks, deploying and testing my codes. The step by step automated script can also become the documentation for deploying my project.

    The ant script "build.xml" contains instruction to execute a sequence of tasks. The sequence of tasks actually provides the documentation of how the java project should be build and subsequently deployed.

     It is rather difficult for me to learn at first. Once you go through the tutorial, it will become easier. I had included many comment in the build.xml file for learning purposes.

Running Ant
1- Download & Setup Apache Ant
apache antapache ant

- Download Apache Ant1.8.1
Please visit the website http://ant.apache.org/ to download the lastest version.

- Unzip Apache Ant. For this example, I copy the unzipped directory "apache-ant-1.8.1" to
"C:\Program Files\Apache Software Foundation\"

- Setup the environment variables in Windows OS
Go to Start>Control Panel>System Properties>Advanced>Environment Variables...>System variables
or press shortcut key "Windows Key" + "Pause"

Create a New System Variable:
     Variable name: "ANT_HOME"
     Variable value: "C:\Program Files\Apache Software Foundation\apache-ant-1.8.1"

Add/Modify to Path Variable:
     Variable name: "PATH"
     append the following to Variable value: ";%ANT_HOME%\bin"

Create a New System Variable:
     Variable name: "JAVA_HOME"
     Variable value: "C:\Program Files\Java\jdk1.6.0_21"
               <Java JDK installed directory>"

Add/Modify to Path Variable:
     Variable name: "PATH"
     append the following to Variable value: ";%JAVA_HOME%\bin"

You may need to reset the computer system for the setup to take effect.

To test if the Java environment is setup correctly, key in the following at command prompt,
     C:\Users\LSB>javac -version
The following output reponse indicates that the Java development environment is setup correctly,
     javac 1.6.0_21

To test if the Apache Ant environment is setup correctly, key in the following at command prompt,
     C:\Users\LSB>ant -version
The following output reponse indicates that the Apache Ant script environment is setup correctly,
     Apache Ant version 1.8.1 compiled on April 30 2010

If you get error message, please ensure again that the environment variable is setup correctly.
You may need to reset the computer system for the environment variables to take effect.

2- Setup a java project example
example: testAnt.zip
Ant script example: build.xml file inside the zip file. It is located at the root of the project folder.
Unzip the testAnt.zip onto a directory for the ant tutorial.

Before you begin the tutorial, ensure that you have properly setup your java compiler & environment.

a) cd.... to your project
b) command to build the java project. (compile and copy files)
ant build
"build" is the name of the task, or in Ant it is call a target.
c) command to run the default task.
By keying only "ant" as the command, the default target will be run. In this example, the default target is configured to be "build". Therefore, in this Ant script, the command "ant build" is the same as "ant".
d) command to run multiple task in sequence.
ant task1 task2 task3

3- More Ant build file example.

- build (hid).xml
  example includes: compiling java classes, running java programs, copying files, compile project into *.war archive
- build (pcAdmin).xml
  example includes: copying directory, executing command line, start/stop windows services or Tomcat 7 server.
- build (sms).xml
  latest ant build script dated: 2011-Feb-14
- build (magneticDetector).xml
  example includes: generating manifest file and build an auto-run jar file.

4- Other Ant reference

- Escape char for XML doc. This is applicable to the build.xml file because it is write in XML format.

- command line for starting or stopping services in Windows operating system
  To start tomcat 7 server:
   net start "Tomcat7"
  To stop tomcat 7 server:
   net stop "Tomcat7"

The service name "Tomcat7" can be extracted from Win7>Control Panel>Administrative Tools>services. Right click the services that you wish to activate, then click Properties. The Service name: "Tomcat7" will be indicated clearly in the properties dialog box.

5- Batch / script file to execute Ant & Javac

- to run the java program, it is usual to type the following

java -Djava.ext.dirs=lib; -cp .\lib\merapi\*;.\resource;.\classes; i2r.starhome.projectXID.STARhome_XID

- instead of typing the long command, they can be save to the windows batch/text file and be execute in Windows. The batch file is quite useful in a number of cases. The following example execute ant to do the necessary directory structure and cleaning, after which the java program is run.

Example 2:

java -classpath .;resource;bin;src;lib/* com.picControl.iotCashier.MyMain

The folder access sequence by Java is determine by the order of the classpath entries (classpath specification order). "." means to search the root first. This is followed by "resource", then "bin". Eclipse tends to copy file from resource to the bin folder, therefore it is better to access the folder "resource" first, followed by the folder "bin". This ensure that the Java do not load the duplicated file from the bin folder first.

- java.bat

There are other type of automated script which maybe useful in various situation.

- Visual Basic Script

- AutoIt Script



refer to Apache Ant manual for further details:
- http://ant.apache.org/manual

- http://www.exubero.com/ant/antintro-s5.html