Skip to main content

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.

Comments

Popular posts from this blog

Using HyperSQL (HSQLDB)

HSQLDB is a portable RDBMS implemented in pure java. It can be embedded with your application as well as can be used separately. It is very a small database that supports almost all features of the standard database system. It comes with small jar file that can be found in lib folder. The HSQLDB jar package is located in the /lib directory of the ZIP package and contains several components and programs. Core components of jar file are : HyperSQL RDBMS Engine (HSQLDB), HyperSQL JDBC Driver, Database Manager, and Sql Tool. Installing and Using Download: download latest release of HyperSQL database from http://hsqldb.org website and extract it. You will see following contents. Here "bin" directory contains some batch files those can be used to run a swing based GUI tool. You can use runManagerSwing.bat to connect to database, but database must be on before running it. Directory lib contains File hsqldb.jar . It is the database to be used by you. Running database First ...

In Process Mode of HSQLDB in web application.

If you want to use the database into your web application, you can use the HSQLDB in In_Process mode. In this mode, you can embed the HSQLDB into your web application and it runs as a part of your web application programm in the same JVM. In this mode, the database does not open any port to connect to the application on the hosing machine and you don't need to configure anything to access it. Database is not expposed to other application and can not be accessed from any dabase tools like dbVisualizer etc. In this mode ,database will be unknown from any other person except you. But in the 1.8.0 version, you can use Server intance for external as well as in process access.  To close the databse, you can issue SHUTDOWN command as an SQL query.   In the in-process mode, database starts from JDBC with the associated databse file provided through  connection URL. for example   DriverManager.getConnection("jdbc:hsqldb:mydatabase","SA","");   Here myd...

How to handle values from dynamically generated elements in web page using struts2

Some time you will see the form containing the button " Add More " . This facility is provided for the user to get the values for unknown number of repeating for some information. for example when you are asking to get the projects details from user, you need to put the option to add the more project for the user since you don't known how many projects user have. In the HTML form, you repeat the particular section to get the multiple values for those elements. In Html page , you can put the option to add new row of elements or text fields by writing the java script or using JQuery API. Now, the question is that how to capture the values of dynamically generated text fields on the server. Using the servlet programming you can get the values by using getParameters() method that resultants the array of the parameter having the same name. But this limit you to naming the text fields in the HTML form. To ally this approach, you have to take the same name for t...