Friday, September 6, 2013

Apache Ant ( Another Neat Tool ) - Lesson 01


Apache Ant ( Another Neat Tool ) : Lesson 01

Apache Ant is a Java based build tool from Apache Software Foundation.
Ant's build files are written in XML.

Why do you need a build tool?

To automate the build and deployment we need a build tool. It is do the followings:
     - Compile code
     - Package the binaries
     - Deploy the binaries to the test server
     - Test your changes
     - Copy code from one location to another

Therefore, Apache Ant is a build and deployment tool that can be executed from a command line.

Features of Apache Ant -
* Ant is the most complete Java build and deployment tool available.
* Ant scripts are written using plain XML.
* Ant is good at automating complicated repetitive tasks.
* Ant comes with a big list of predefined tasks.
* Ant provides an interface to develop custom tasks.
* Ant can be easily invoked from the command line and it can integrate with free and commercial IDEs.

Installing Apache Ant -

Apache Ant is distributed under the Apache Software License, a fully-fledged open source license certified by the open source initiative.

1. Ensure that the JAVA_HOME environment variable is set to the folder where your JDK is installed.
2. Download the binaries from http://ant.apache.org
3. Unzip the zip file to a convenient location. (ex:- /usr/share/ )
4. Create a new environment variable called ANT_HOME that points to the Ant installation folder, in this case "/usr/share/ant".
5. Append the path to the Apache Ant batch file to the PATH environment variable.

To create the ANT_HOME, use followings :
    1. type command /$ sudo gedit /etc/environment
    2. Paste ANT_HOME="/usr/share/ant"
    3. Append to the PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:$JAVA_HOME:$JRE_HOME:$ANT_HOME"
6. Verify the installation, use below commands and should see an output similar to:

sanjeeva@sanjeeva:/$ ant -version
Apache Ant(TM) version 1.8.2 compiled on December 3 2011

sanjeeva@sanjeeva:/$ $ANT_HOME
bash: /usr/share/ant: Is a directory

Build Files -

Ant's build file, build.xml should live in the project's base directory.
Although you are free to use other file names or place the build file in some other location.

All build files require the project element and at least one target element.

project element has three attributes :

name : The Name of the project. (Optional)
default : The default target for the build script. A project may contain any number of targets. This attribute specifies which target should be considered as the default. (Mandatory)
basedir: The base directory (or) the root folder for the project. (Optional)

target is a collection of tasks that you want to run as one unit.

Below example, we have a simple target to provide an informational message to the user.

<?xml version="1.0"?>
<project name="Hello World Project" default="info">
      <target name="info">
             <echo>Hello World - Welcome to Apache Ant!</echo>
      </target>
</project>

* Targets can have dependencies on other targets.

For example,
a deploy target may have a dependency on the package target,
the package target may have a dependency on the compile target and so forth.

Dependencies are denoted using the depends attribute.

For example:
<target name="deploy" depends="pacakge">
      ....
</target>
<target name="pacakge" depends="clean,compile">
      ....
</target>
<target name="clean" >
      ....
</target>

<target name="compile" >
      ....
</target>

The target element has the following attributes:

name : The name of the target (Required)
depends : Comma separated list of all targets that this target depends on. (Optional)
description : A short description of the target. (optional)
if : Allows the execution of a target based on the trueness of a conditional attribute. (optional)
unless : Adds the target to the dependency list of the specified Extension Point.
An Extension Point is similar to a target, but it does not have any tasks. (Optional)

Run a build file :

1. Save the below as build.xml.

<?xml version="1.0"?>
<project name="Hello World Project" default="info">
     <target name="info">
          <echo>Hello World - Welcome to Apache Ant!</echo>
     </target>
</project>

2. Open a terminal and navigate to the folder where the build.xml resides.

3. Type ant info Or ant and see the output.

sanjeeva@sanjeeva:~$ ant
Buildfile: /home/sanjeeva/build.xml
info:
[echo] Hello World - Welcome to Apache Ant!

BUILD SUCCESSFUL

Total time: 0 seconds

Property element

Ant uses the property element which allows you to specify properties.
This allows the properties to be changed from one build to another. or from one environment to another.

By default, Ant provides the following pre-defined properties that can be used in the build file;

ant.file : The full location of the build file.
ant.version : The version of the Apache Ant installation.
basedir : The basedir of the build, as specified in the basedir attribute of the project element.
ant.java.version : The version of the JDK that is used by Ant.
ant.project.name : The name of the project, as specified in the name atrribute of the project element
ant.project.default-target : The default target of the current project
ant.project.invoked-targets : Comma separated list of the targets that were invoked in the current project
ant.core.lib : The full location of the ant jar file
ant.home : The home directory of Ant installation
ant.library.dir : The home directory for Ant library files - typically ANT_HOME/lib folder.

In addition to the above, the user can define additional properties using the property element.

Example : shows how to define a property called “devname”:
<?xml version="1.0"?>
<project name="Hello World Project" default="info">
       <property name="devname" value="Sanjeeva Pathirana"/>
             <target name="info">
                    <echo>Apache Ant version is ${ant.version} </echo>
                    <echo>Developer is ${devname} </echo>
            </target>
</project>
 
[ Out put ] :
Buildfile: /home/sanjeeva/build.xml
info:
[echo] Apache Ant version is Apache Ant(TM) version 1.8.2 compiled on December 3 2011
[echo] Developer is Sanjeeva Pathirana

Ant Property files -

Storing the properties in a separate file allows you to reuse the same build file, with different property settings for different execution environment. For example, build properties file can be maintained separately for DEV, TEST and PROD environments.

The property file is named build.properties and is placed along side the build.xml file.

You could create multiple build properties file based on the deployment environments - such as build.properties.dev and build.properties.test

Example : shows the build.xml and an associated build.properties file

<?xml version="1.0"?>
<project name="Hello World Project" default="info">
        <property file="build.properties"/>
             <target name="info">
                  <echo>Apache Ant version is ${ant.version} </echo>
                  <echo>Developer is ${devname} </echo>
                  <echo>Build Version is ${buildversion} </echo>
             </target>
</project>

build.properties
# The Site Name
devname=Sanjeeva Pathirana
buildversion=3.3.2

Output -
Buildfile: /home/sanjeeva/build.xml
info:
[echo] Apache Ant version is Apache Ant(TM) version 1.8.2 compiled on December 3 2011
[echo] Developer is Sanjeeva Pathirana
[echo] Build Version is 3.3.2
BUILD SUCCESSFUL
Total time: 0 seconds