Using Ant tool

How to use Ant (Another Neat Tool) build tool

Ant is a Java-based build tool. It is built in java so you can use it on any platform that has JVM. It uses XML file to get the instructions for building process. This XML file contains project element as root element and all the tasks to be performed by ant are defined in sub elements. The XML file having some elements is given below.

<project name="My Example Project" default="default" basedir=".">

<target name="default">

<javac srcdir="./src" destdir="build"/>

<echo message="Application compiled"/>

</target>

</project>

In above build file:

<name attribute1="value1" attribute2="value2" ... />

where name is the name of the task, attribute is the attribute name, and value is the value for this attribute.

Here when execution this build process, javac command compiles all files from src directory and place them into build directory. After that on the console “Application compiles” massage is printed. The build file having file name build.xml will be recognized by ant automatically.

Build file represents the set of tasks and each task is represented by one target.

To run ant following command syntax is used;

ant [options] [target [target2 [target3] ...]]

Command-line Options Summary

Type the command ant –h then ant will show following output to you.

ant [options] [target [target2 [target3] ...]]

Options:

-help, -h print this message

-projecthelp, -p print project help information

-version print the version information and exit

-diagnostics print information that might be helpful to

diagnose or report problems.

-quiet, -q be extra quiet

-verbose, -v be extra verbose

-debug, -d print debugging information

-emacs, -e produce logging information without adornments

-lib <path> specifies a path to search for jars and classes

-logfile <file> use given file for log

-l <file> ''

-logger <classname> the class which is to perform logging

-listener <classname> add an instance of class as a project listener

-noinput do not allow interactive input

-buildfile <file> use given buildfile

-file <file> ''

-f <file> ''

-D<property>=<value> use value for given property

-keep-going, -k execute all targets that do not depend

on failed target(s)

-propertyfile <name> load all properties from file with -D

properties taking precedence

-inputhandler <class> the class which will handle input requests

-find <file> (s)earch for buildfile towards the root of

-s <file> the filesystem and use it

-nice number A niceness value for the main thread:

1 (lowest) to 10 (highest); 5 is the default

-nouserlib Run ant without using the jar files from ${user.home}/.ant/lib

-noclasspath Run ant without using CLASSPATH

Properties

A project can have a set of properties. A property has a name and a value pair and the name is case-sensitive. By placing the property name between "${" and "}" in the attribute value, Properties may be used to get the value of that property in task. Properties are specified as follow

<property name="src" location="srcDir"/>

<property name="build" location="buildDir"/>

Now we can specify the src directory and build directory as properties in above build.xml file.

<project name="My Example Project" default="default" basedir=".">

<property name="sourcecodeFolder" location="./src"/>

<property name="compileFolder" location="build"/>

<target name="default">

<javac srcdir="${sourcecodeFolder}" destdir="${compileFolder}"/>

<echo message="Application compiled"/>

</target>

</project>

Built-in Properties

In addition, Ant has some built-in properties:

basedir the absolute path of the project's basedir (as set

with the basedir attribute of <project>).

ant.file the absolute path of the buildfile.

ant.version the version of Ant

ant.project.name the name of the project that is currently executing;

it is set in the name attribute of <project>.

ant.java.version the JVM version Ant detected; currently it can hold

the values "1.1", "1.2", "1.3", "1.4" and "1.5".

Specifying classpath

Now if we want to compile and run the java program than we have to include some external library into classpath. Ant build script file can specifies the classpath to compile and run application.

Suppose we want to compile the servlet applications that will require the servlet-api.jar file must be in classpath at compile time. Now we write build script here.

Simple Example

Java source file

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class Servlet1 extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException
{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
java.util.Date d=new java.util.Date();
pw.write("Hello :"+d);
}
}

This file is placed in src directory of the current directory of application.

application directory structure

ant\

src\

Servlet1.java

build\

lib\

servlet-api.jar

Build file

<project name="My Example Project" default="default" basedir=".">
<property name="sourcecodeFolder" location="./src"/>
<property name="compileFolder" location="build"/>
<target name="default">
<javac srcdir="${sourcecodeFolder}" destdir="${compileFolder}">
<classpath>
<pathelement location="."/>
<pathelement location="./lib/servlet-api.jar"/>
</classpath>
</javac>
<echo message="Application compiled"/>
</target>
</project>

OUTPUT

H:\J2EE_Notes\ant>ant
Buildfile: build.xml

default:
[javac] Compiling 1 source file to H:\J2EE_Notes\ant\build
[echo] Application compiled

BUILD SUCCESSFUL
Total time: 3 seconds

You can specify sources files to be compiled and you can stop the compilation for some files using include and exclude element.

<include name="*.java"/>

<exclude name="Example.java"/>

Here all the file having extension .java will be compiles except Example.java file.

No comments:

Popular Posts