email:
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 |
![]() ![]() - 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"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. ant 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. http://www.hdfgroup.org/HDF5/XML/xml_escape_chars.htm - command line for starting or stopping services in Windows operating system To start tomcat 6 server: net start "apache tomcat 6" To stop tomcat 6 server: net stop "apache tomcat 6" |
| 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.bat There are other type of automated script which maybe useful in various situation.
|