Monday, March 26, 2012

Apache Ant Step-by-step tutorial

I have tried to give here a detailed step by tutorial on ANT for all
It can help you set up ANT from scratch.

The steps include download ANT , set up environment variables,
write sample build script and execute .

Find the article here





hy use Apache 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 exampleexample: 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

No comments:

Post a Comment

subversion video