Академический Документы
Профессиональный Документы
Культура Документы
Project Details:
Table of Contents
Ant is an automated build-and-deploy tool for simple and complex Java projects. Ant
has value for those creating all-Java applications and for those creating Web
applications using HTML, Java Server Pages (JSP), and Java Servlets. Ant can also
automate and simultaneously document during deployment.
Ant scripts are XML based. Ant handles many tasks that are useful when building and
deploying Java applications. Basic tasks include adding and removing directories,
creating JAR and ZIP files, copying and downloading files using FTP and creating
documentation. More-advanced features include checking out source code from
source code control systems such as CVS or SourceSafe, executing SQL queries or
scripts, transforming XML files to human-readable HTML, and generating stub files
for remote method invocation.
Let us see what are the differences between Ant and Make, the well-known build
tool many C developers use?
Ant is highly suitable for Java, with its own unique paradigms, including portability.
Whereas Make relies on non-portable operating system commands (so a Make file
running under Microsoft Windows won't be of any help to developers using UNIX),
pure Java projects built using Ant are portable, because Ant itself is written in Java
and Ant build files use XML syntax.
This article walks you through the installation, usage and basic Ant tasks
which are used widely.
Prerequisites
1) Jdk should be installed with version 1.3 onwards. The environment variable
JAVA_HOME set to point to the jdk.
2) Ant should be installed. Site to download Ant is http://www.apache.org and
the environment variable ANT_HOME set to point to Ant.
3) Any simple Text Editor like Notepad/Textpad/Editplus/vi
Installation of Ant
The ant executable generally comes in the form of a zip file which you can unzip at a
specific location you want.
The binary distribution of Ant consists of the following directory layout:
ant
+--- bin à contains important scripts like ant.bat for Windows or ant.sh for Unix
| platform
|
+--- lib à contains Ant jars as well as other necessary jar files
|
+--- docs à contains documentation
| +--- ant2 à a brief description of ant2 requirements
| |
| +--- images à various logos for html documentation
| |
| +--- manual à Ant documentation
|
+--- etc à contains XSL files to:
à a) Create an enhanced report from xml output of various
tasks.
à b) Migrate your build files and get rid of 'deprecated'
warning.
Only the bin and lib directories are required to run Ant. This directory will be pointed
to by ANT_HOME environment variable.
If Ant is installed as described in the earlier section, to run Ant from the command-
line just type ant
When you run ant on command line without any other file name then it searches for
build.xml file in the present working directory.
If you have created a file other than build.xml then use the following command to
run the scripts:-
ant –buildfile buildfilename
This works on Unix as well as Windows if you got a doubt about it.
ant targetname
If required you can also use the –verbose option to get more details like:-
ant –verbose > outputfile
Tasks:-
A task does the work of the Ant script, such as compiling java files.
There are hundreds of pre-built Ant tasks you can use to create your own Ant scripts.
Ant comes with support for about 75 core tasks and another 40 or so optional tasks.
Core tasks are included in the ant.jar file, and optional tasks are found in
optional.jar. Most of the optional tasks also have library dependencies that make it
necessary to install additional JAR files.
An Ant task is a Java class that extends org.apache.tools.ant.Task. You can also
create your own tasks. Tasks are Java programs that add functionality to Ant.
The basic structure of a well formed build XML file will be like:-
5.1 Project
Each project defines one or more targets. When starting Ant, you can select which
target(s) you want to have executed. When no target is given, the project's default
is used.
5.2 Targets
The target element can specify depends attribute which require the specified targets
to be run once before the current target starts.
You can only build a distributable when you have compiled first, so the distribute
target depends on the compile target.
Ant's depends attribute only specifies the order (from left to right) in which targets
should be executed.
<target name="A"/>
<target name="B" depends="A"/>
<target name="C" depends="B"/>
<target name="D" depends="C, B, A"/>
A target name can be any alphanumeric string valid in the encoding of the XML file.
Please avoid using the empty string "", comma "," and space " " which are is in this
set, because they will not be supported in future
Targets beginning with a hyphen such as "-restart" are valid, and can be used to
name targets that should not be called directly from the command line.
5.3 Property
To declare properties (or variables in programming language paradigm) to be
used later in the script you require:-
♦ Name of the property
♦ Value of the property
Use the property element with the attributes name and value.
For example:-
<property name="src" value="src"/>
Thus the property src can be used in the file where it is defined.
But if you have divided your script into multiple files and you want to use the same
property in multiple files then declare the properties in a properties file in the form of
name=value pair and use the properties file in the script.
For example:-
If you write the following properties in a file names common.properties:-
src=src
dest=D://VSS
kit=D://kit
JAVA_HOME=D://jdk1.5.0_03
to use this common.properties in a script use the property element with the
attribute file in the Ant script.
For example:-
<property file="common.properties"/>
It is advisable to define all properties in a properties file so that even any person not
knowing XML and/or ANT can just edit the properties file with the new values of the
properties (any Ant XML script need not be modified) and make a build.
The additional advantage of having separate properties file is that without modifying
any Ant XML script and just by changing the values of properties in the properties
file the same Ant script can be used on Windows as well as Unix.
5.4 Tasks
A task is a piece of code that can be executed.
A task can have multiple attributes (or arguments, you can say). The value of an
attribute might contain references to a property. These references will be resolved
before the task is executed.
Tasks have a common structure:
<name attribute1="value1" attribute2="value2" ... />
where name is the name of the task, attributeN is the attribute name, and valueN is
the value for this attribute.
All tasks share a task name attribute. The value of this attribute will be used in the
logging messages generated by Ant.
Writing of the Ant build script
Now let us see step-by-step how we convert the above written steps into Ant script:-
Create directory
Requirement:-
To create a directory we require: -
♦ The location where we want to create a directory
♦ The name of the directory.
Where dir attribute will contain the location where the directory has to be created
and the name of the directory to be created. Considering the dest property is
pointing to D://VSS the temp directory will be created under this.
Requirement:-
To compile the java classes we require:-
♦ Source directory where the java source files (.java) exist.
♦ Destination directory where to place the output of compilation (.class files)
♦ Class path if required.
The “includes” attribute in the javac task contains list of files specified using wild-
card patterns. Ant's special handling of wildcards expands **/ to any number of
directories so this task will act on all java files in this directory and all of its
subdirectories. The classpath element has pathelement which can locate the jar files
required by your application.
If you have to write multiple javac tasks and the same classpath has to be repeated
you can give it some id and use it in the task.
For example:-
<path id="compile.classpath">
<pathelement location="${src}"/>
<pathelement location="${JAVA_HOME}/jre/lib/jsse.jar" />
<pathelement location="${dest}"/>
</path>
Now you can use this compile.classpath in the javac tasks as shown in the next
example .
The javac task contains attributes which are similar to the options provided by javac
command.
Let us take a look at some more attributes and their usage:-
For example:-
<javac srcdir="${src} " destdir="${dest}/temp" includes="**/*.java"
failonerror=false" deprecation="on" debug="on">
<classpath refid="compile.classpath"/>
</javac>
Requirement:-
To create jar file we require:-
♦ Location and name of the jar file.
♦ The class files to be included in the jar file.
For example:-
<jar destfile="${kit}/common.jar" basedir="${dest}"/>
b) If you have to jar class files from multiple directories then you can use the jar
task containing nested fileset task with the dir attribute specifying the base
directory and includes attribute specifying the comma-separated file names or wild-
card patterns of class files to be included in the jar file.
For example:-
<jar destfile="${kit}/app.jar" >
<fileset dir="${dest}" includes="com /foundation/*.class"/>
<fileset dir="${dest}" includes="com /web/*.class"/>
</jar>
c) If you have to create an EJB jar then you can use the jar task containing
manifest attribute if you want to provide your own manifest file otherwise default
manifest file will be included in the jar file.
For example:-
<jar destfile="${kit}/AccMgrEJB.jar" manifest="${dest}/META-
INF/Manifest.mf">
<fileset dir="${dest}" includes="/business_objects/ejb/AccMgrEJB.class"/>
Requirement:-
To copy file(s) what is it that you need to know?
♦ The source directory from where to copy the file(s) and file name(s) to be
copied
♦ The destination directory where to copy the file(s).
b) To copy a set of files from source to destination use the copy task with todir
attribute to specify the destination directory where to copy the files and use the
nested fileset tasks with dir attribute to specify the source directory from where to
copy the files.
For example:-
<copy todir="${dest}/public_html/public/jsps/images/">
<fileset dir="${src}/images/">
<include name="bt_bfdbk.gif" />
<include name="bt_bsrch.gif"/>
<include name="bt_bsthp.gif" />
<include name="bullet.gif" />
</fileset>
</copy>
The fileset task contains nested include task where you can specify the names of
specific files to be copied. So only the files specified in the include task are copied to
the destination directory. Similarly you can also use exclude tasks to specify the
names of the files which you do not want to be copied.
Requirement:-
The requirements to create ear file are:-
♦ Name and location of the ear file.
♦ The location of application.xml file
♦ The location and names of the jar files to be included in the ear file.
For example:-
<ear destfile="${kit}/ear_war/${ear_filename}"
appxml="${src}/deployment_descriptor/application.xml">
<fileset dir="${dest}/lib" includes="*.jar"/>
</ear>
Requirement:-
To create a War file you need to know
♦ Where do the jsps, html, images, scripts reside
♦ The jar files to be provided to the lib directory.
♦ The location of web.xml file.
♦ Location and name of the war file.
Note:- The jar, ear and war tasks contain an attribute update set to false by default
which means that a new file will be created however it can be set to true when you
want to update an already existing file.
Requirement:-
To delete directory we require:-
♦ Location and Name of directory.
To delete a set of files we require:-
♦ Location and Name of directory.
♦ Names of the files.
WARNING: Do not set dir to ".", "${basedir}", or the full-pathname. It will remove
the entire contents of the current base directory (and the base directory itself, if
different from the current working directory).
b) If you want to delete a particular set of files you can use nested fileset task within
the delete task.
For example:-
<delete>
<fileset dir="${dest}/META-INF" includes="*.xml"/>
</delete>
7. Other tasks
A few other helpful tasks:-
7.3 Documentation
For generating documentation you can use the javadoc task which makes use of the
javadoc tool.
Requirement:-
♦ The source from which to generate the documentation.
Requirement:-
To change permissions you need:-
♦ The name of the directory or file.
♦ The new permissions
The way you have ln command in Unix to create symbolic links for files you have
symlink ant task to perform the same job in ANT.
Attribute Description
link The name of the link to be created
resource The resource where the link should point to.
Example and explanation
For example:-
<symlink link="${CONFIG_APP}/log" resource="/app/log"/>
SAMPLE
Click here to download the sample.