Apache Ant
Introduction:
Ant is developed by Apache Foundation. Ant (originally an acronym for Another Neat Tool), is a command-line build tool,its main aim is to drive processes described in the build files as targets and extension points depend on each other.Ant provides special support for the Java programming language , but can be used for any process which can be described in terms of targets and tasks. Ant is platform-independent; it is written purely in Java. Ant is an open source tool so that it is easy to use. Ant is particularly good at automating complicated repetitive tasks and thus is well suited for automating standardised build processes. Ant accepts instructions in the form of XML documents thus is extensible and easy to maintain ,and it is easy to understand and implement Ant can use and compile source code from a variety of version controls and packaging of the compiled code and resources can also be done.
Ant is developed by Apache Foundation ,it is a a part of Apache Jakarta Project. There are many build tools like make(platform dependent), jam and many but Ant has overcome other tool shortcoming as it uses java classes with XML scripting that makes it portable through cross-platform behavior. By default Ant uses build.xml as the name for a buildfile.
Installation:
In this section, we will learn how to install Ant into our system (Fedora 8 Os, Windows)
The current version 1.8.2 of was released on 27-December-2010 . It is available for download at http://ant.apache.org/bindownload.cgi. Here, you have to click on the link apache-ant-1.7.1-bin.zip in the .zip archive to download the zip file. After the download completes, unzip this file and install the apache-ant-1.7.1 folder to the /opt directory . Therefore, path for the Ant directory will be /opt/apache-ant-1.8.2. Now, we have to set the PATH information. Open the terminal and type “ vi .bash_profile ” this will open the file here we need to set ANT_HOME and PATH environment variables. Set the path value /opt/apache-ant-1.8.2 to ANT_HOME variable and the path value ${ANT_HOME}/bin; to PATH variable. Just look at the bash_profile file in fedora to set up the environment variables.
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
JAVA_HOME=/usr/lib/jvm/java-1.7.0-icedtea-1.7.0.0
ANT_HOME=/root/apache-ant-1.8.1
CATALINA_HOME=/root/apache-tomcat-6.0.18
PATH=$PATH:$HOME/bin:${JAVA_HOME}/bin:${ANT_HOME}/bin:${CATALINA_HOME}/bin:
export PATH JAVA_HOME CATALINA_HOME
export CLASSPATH
unset USERNAME
Now we will see how to set up the environment variables for Windows Os . After downloading our zipr archive file we will unzip and install the apache-ant-1.8.2 folder in the root directory C:\ drive. So the path for the Ant directory is C:\apache-ant-1.8.2 . Select the item "System" in the Control Panel of our system and click on the tab named "Advanced". We will see a button named "Environment Variables" appears, click it. Under Environment variables, we will find two user variables, viz., ANT_HOME and CLASS_PATH. Set the path value C:\apache-ant-1.8.2 to ANT_HOME variable and the path value C:\apache-ant-1.8.2\lib; to CLASS_PATH variable. Again in the System Variables, we have to set the value C:\jdk to JAVA_HOME variable and the value C:\apache-ant-1.8.2\bin; to Path variable.
Now, you have to check whether the installation process is done properly or not. To do this, open the System Terminal and run the command [root@linux ~]# ant If the following message
|
[root@linux ~]# ant
Buildfile: build.xml does not exist!
Build failed
[root@linux ~]# |
appears, then it indicates that your installation is successful; Ant is properly installed in your system..
Hello World in Ant
Each build file contains one project and one or more targets. Targets contain task elements. Each task element of the buildfile can have an id attribute and can later be referred to by the value supplied to this. The value has to be unique. For additional information, see the Using Apache Ant )
Let us create a small build file. This is file is in current directory.
<?xml version="1.0"?>
<project name="MyFirstAntProject" default="MyTarget">
<target name="init">
<echo>Running target init</echo>
</target>
<target name="MyTarget" depends="init">
<echo>Running target MyTarget</echo>
</target>
</project>
Now we will run this file using ant command. Output will be
Buildfile: build.xml
init:
[echo] Running target init
MyTarget:
[echo] Running target MyTarget
BUILD SUCCESSFUL
Total time: 0 seconds