Java ant tutorial

Java ant tutorial Print E-mail
User Rating: / 2
PoorBest 
Contributed by Howell   
Sunday, 18 June 2006

Ant Overview (Java ant tutorial)

Apache Ant is a Java-based build tool. In theory, it is kind of like Make, but without Make's wrinkles.

Why another build tool when there is already make, gnumake, nmake, jam, and others? Because all those tools have limitations that Ant's original author couldn't live with when developing software across multiple platforms. Make-like tools are inherently shell-based -- they evaluate a set of dependencies, then execute commands not unlike what you would issue in a shell. This means that you can easily extend these tools by using or writing any program for the OS that you are working on. However, this also means that you limit yourself to the OS, or at least the OS type such as Unix, that you are working on.

Makefiles are inherently evil as well. Anybody who has worked on them for any time has run into the dreaded tab problem. "Is my command not executing because I have a space in front of my tab!!!" said the original author of Ant way too many times. Tools like Jam took care of this to a great degree, but still have yet another format to use and remember.

Ant is different. Instead of a model where it is extended with shell-based commands, Ant is extended using Java classes. Instead of writing shell commands, the configuration files are XML-based, calling out a target tree where various tasks get executed. Each task is run by an object that implements a particular Task interface.

Granted, this removes some of the expressive power that is inherent by being able to construct a shell command such as `find . -name foo -exec rm {}`, but it gives you the ability to be cross platform -- to work anywhere and everywhere. And hey, if you really need to execute a shell command, Ant has an <exec> task that allows different commands to be executed based on the OS that it is executing on.

 A little history(Java ant tutorial)

Ant started out as an internal component of Tomcat, the servlet container that is used in the reference implementation for the Java Servlet and JavaServer Pages (JSP) technologies. The Tomcat codebase was donated to the Apache Software Foundation; there, it became part of the Apache Jakarta project, which has a mission to produce open source, server-side solutions for the Java platform. The usefulness of Ant was quickly recognized and its usage spread throughout the other Jakarta subprojects. Thus, it was made into a Jakarta subproject of its own, with the first independent release being made in July of 2000.

Since then, Ant's popularity has grown and grown. It has won numerous industry awards and become the de facto standard for building open source Java projects. In November of 2002, this success was acknowledged by Ant's promotion to a top-level Apache project.


Why do we think Ant makes a great build tool? (Java ant tutorial)

  • It has a very simple syntax, which is easy to learn, especially if you have used
    XML before.
  • It is easy to use, eliminating the full-time makefile engineer common on large
    Make-based software projects.
  • It is cross-platform, handling Java classpaths and file directory structures in a
    portable manner.
  • It is very fast. Java routines such as the Java compiler or the code to make a JAR
    file can all start inside the Ant JVM, reducing process startup delays. Ant tasks
    are also designed to do dependency checking to avoid doing any more work
    than necessary.
  • It integrates tightly with the JUnit test framework for XP-style unit testing.
  • It is easily extensible using Java.
  • It has built-in support for J2EE development, such as EJB compilation and
    packaging.
  • It addresses the deployment problems of Java projects: FTP, Telnet, application
    servers, SQL commands; all can be used for automated deployment
Hello Java World with Ant(Java ant tutorial)
 Preparing the project

We want to separate the source from the generated files, so our java source files will be in src folder. All generated files should be under build, and there splitted into several subdirectories for the individual steps: classes for our compiled files and jar for our own JAR-file.

The later directories are created by our buildfile, so we have to create only the src directory. (Because I am working on Windows, here is the win-syntax - translate to your shell):

md src

Here is the code of our HelloWorld.java which is to be compiled by the Ant utility.

package hotjava;

public class HelloWorld {
    public static void main(String[] args) {

        System.out.println("Hello Java World");

    }
}

Writing build.xml file

Ant uses configuration file called build.xml to work. This is the file where you defines the process of compiling, building and deploying.

Here is code of simple build.xml file

 <project>

    <target name="clean">
        <delete dir="build"/>
    </target>

    <target name="compile">
        <mkdir dir="build/classes"/>
        <javac srcdir="src" destdir="build/classes"/>
    </target>

    <target name="jar">
        <mkdir dir="build/jar"/>
        <jar destfile="build/jar/HelloWorld.jar" basedir="build/classes">
            <manifest>
                <attribute name="Main-Class" value="hotjava.HelloWorld"/>
            </manifest>
        </jar>
    </target>

    <target name="run">
        <java jar="build/jar/HelloWorld.jar" fork="true"/>
    </target>

</project>

 

Now you can compile

ant compile
ant jar
ant run

 Enhance the build file

 The project tag:

<project name="" default="build" basedir=".">

Here is the description of the attributes:

Attribute         Description
name             Represents the name of the project.
default           Name of the default target to use when no target is supplied.
basedir         Name of the base directory from which all path calculations are done.

 <project name="HelloWorld" basedir="." default="main">

    <property name="src.dir"     value="src"/>

    <property name="build.dir"   value="build"/>
    <property name="classes.dir" value="${build.dir}/classes"/>
    <property name="jar.dir"     value="${build.dir}/jar"/>

    <property name="main-class"  value="hotjava.HelloWorld"/>

 

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

    <target name="compile">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}"/>
    </target>

    <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}"/>
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
            <manifest>
                <attribute name="Main-Class" value="${main-class}"/>
            </manifest>
        </jar>
    </target>

    <target name="run" depends="jar">
        <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
    </target>

    <target name="clean-build" depends="clean,jar"/>

    <target name="main" depends="clean,run"/>

</project>

 

Buildfile: build.xml

clean:

compile:
    [mkdir] Created dir: C:\...\build\classes
    [javac] Compiling 1 source file to C:\...\build\classes

jar:
    [mkdir] Created dir: C:\...\build\jar
      [jar] Building jar: C:\...\build\jar\HelloWorld.jar

run:
     [java] Hello World

main:

BUILD SUCCESSFUL


Automate your build process using Java and Ant  From Javaworld
 Summary

Ant is a powerful scripting tool that lets you craft build processes around your code requirements using predefined tasks and provides expansion capability to handle even more difficult tasks. This article is an introduction to the powerful XML-based scripting tool that can automate your mundane tasks and allow you to concentrate on your business rules and code development.

Last Updated ( Saturday, 01 July 2006 )

  home              contact us

 

©2006-2008 DeveloperZone.biz   All rights reserved     powered by Mambo Designed by Siteground