Tuesday, December 1, 2009

Apache Ant Script overview with a sample example


Apache Ant is probably the most popular build tool for Java. Its a bit like the old makecommand popularised under UNIX, but has a rich set of predefined tasks and is expressed in an XML syntax.

Example

Please see below for a very basic Ant build script. This does not explain indepth knowledge on building your project using Ant script.

Steps to install software

Download the Apache Ant from the link : Download Apache Ant
  • Unzip the files into your machine.
  • Set your system path variable to ANT's bin directory. For example d:/projects/ant/bin"
  • Set the ANT_HOME in your system variables. ANT_HOME should point to your Ant installation directory. For example "c:/dev/ant".
  • Set JAVA_HOME in your system environment variables.
  • Copy tools.jar file into the "JRE/lib" folder.
To test the setup open your command prompt and type as "ant". You will see a message saying

Buildfile: build.xml does not exist!
Build failed

Set up is done. Let's look into our first simple example program using ANT script.

This example program shows how to create the JAR file after building all the java files.

<project name="sampleProject" basedir="." default="jar">

In the above code "sampleProject" is the project name for this prticular build. We are setting base directory as the current directory by just giving .(dot). We are telling the script to start from "jar" target name. The default attribute is used for setting the starting point for the ANTscript.

We call a seperate task as target. We can define as many number of targets inside a build file. If we closely look into the build file, we have many number of targets based its operations. So, our build starts with "jar" target.

Target has depends attribute to indicate, that particular task can be executed only after the certain number of dependencies. In our case, before building the jar file, we have to compile the files. So, jar target calls compile target. That has some dependencies that is executed. Once the build is successful, test.jar will be created in the same directory.

Sample "build.xml" file
--------------------------------------

<?xml version="1.0"?>
<project name="sampleProject" basedir="." default="jar">
    <property name="src" value="ant-source"/>
    <property name="output" value="bin"/>

    <target name="compile" depends="create">
        <javac destdir="bin">           
            <src path="${src}"/>            
            <classpath refid="java"/>
        </javac>
    </target>

    <target name="jar" depends="compile">
        <jar destfile="test.jar">
            <fileset dir="bin"/>
        </jar>
    </target>


    <target name="clean">
        <delete dir="${output}"/>
    </target>

    <target name="create" depends="clean">
        <mkdir dir="${output}"/>
    </target>

    <path id="java">
        <fileset dir="D:\Jars\Hibernate">
            <include name="*.jar"/>
        </fileset>
    </path>
</project>


Stumble
Delicious
Technorati
Twitter

0 Comments:

Post a Comment