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.
1- Download & Setup Apache 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: "
Variable name: "
Variable name: "
Add/Modify to Path Variable:
Variable name: "
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,
The following output reponse indicates that the Java development environment is setup correctly,
To test if the Apache Ant environment is setup correctly, key in the following at command prompt,
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
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)
"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
|4- Other Ant reference
- Escape char for XML doc. This is applicable to the build.xml file because it is write in XML format.
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.
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.
There are other type of automated script which maybe useful in various situation.