How to use Ant (Another Neat Tool) build tool
<project name="My Example Project" default="default" basedir=".">
<javac srcdir="./src" destdir="build"/>
<echo message="Application compiled"/>
-
project is root element and it may have three attributes name, default and basedir. Each attribute represents following.
-
target is the element that represent of build process. A build file may contain any number of targets. Each target element may have following attributes.
-
name represent the name of target through which target will be identified throughout the build file.
-
depends attribute specify the names of other targets to be process before process this target.
-
if attribute is used to specify the name of the property that must be set in order for this target to execute.
-
unless attribute is used to specify the name of the property that must not be set in order for this target to execute.
-
description attribute is used to specify a short description of this target's function.
-
-
javac element specifies the name task or a piece of code that can be executed. There number of task available in ant build tool. I will give brief description in this tutorial in next section. Attributes of task varies according to their specification. Common syntax to be used is as follow.
<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.
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] ...]]
-projecthelp, -p print project help information
-version print the version information and exit
-diagnostics print information that might be helpful to
-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
-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
-D<property>=<value> use value for given property
-keep-going, -k execute all targets that do not depend
-propertyfile <name> load all properties from file with -D
-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
<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"/>
<javac srcdir="${sourcecodeFolder}" destdir="${compileFolder}"/>
<echo message="Application compiled"/>
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
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
<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>
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
<exclude name="Example.java"/>
Here all the file having extension .java will be compiles except Example.java file.
Comments