Financial Accounting Tutorial
Financial Accounting Tutorial
This tutorial will teach you how to use Apache ANT to automate the build and
deployment process in simple and easy steps. After completing this tutorial, you
will find yourself at a moderate level of expertise in using Apache ANT from where
you can take yourself to next levels.
Audience
This tutorial is prepared for the beginners to help them understand basic
functionality of Apache ANT tool to automate the build and deployment process.
Prerequisites
We assume you have knowledge of software development using any programming
language, especially Java, and the software build and deployment process.
All the content and graphics published in this e-book are the property of Tutorials
Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy,
distribute or republish any contents or a part of contents of this e-book in any
manner without written consent of the publisher.
We strive to update the contents of our website and tutorials as timely and as
precisely as possible, however, the contents may contain inaccuracies or errors.
Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy,
timeliness or completeness of our website or its contents including this tutorial. If
you discover any errors on our website or in this tutorial, please notify us at
[email protected].
i
Apache ANT
Table of Contents
About the Tutorial ....................................................................................................................................i
Audience ..................................................................................................................................................i
Prerequisites ............................................................................................................................................i
1. ANT INTRODUCTION............................................................................................................ 1
build.xml ...............................................................................................................................................10
build.properties ....................................................................................................................................11
Fileset ...................................................................................................................................................13
Patternset .............................................................................................................................................13
Filelist....................................................................................................................................................14
Filterset .................................................................................................................................................14
Path ......................................................................................................................................................15
ii
Apache ANT
Attributes ..............................................................................................................................................20
build.properties ....................................................................................................................................29
build.xml ...............................................................................................................................................29
build.properties ....................................................................................................................................33
build.xml ...............................................................................................................................................33
iii
Apache ANT
1. ANT INTRODUCTION
ANT stands for Another Neat Tool. Before going into details of Apache ANT, you
must understand the need for a build tool.
To automate and simplify the above tasks, Apache ANT is useful. It is an operating
system build and deployment tool that can be executed from command line.
1
Apache ANT
2
Apache ANT
2. ANT ENVIRONMENT SETUP
Apache ANT is distributed under the Apache Software License, a full-fledged open
source license certified by the open source initiative.
The latest Apache Ant version, including full-source code, class files and
documentation can be found at http://ant.apache.org.
Ensure that the JAVA_HOME environment variable is set to the folder where
your JDK is installed.
Download the binaries from http://ant.apache.org
Unzip the .zip file to a convenient location using Winzip, winRAR, 7-zip or
similar tools, on say c:\folder.
Create a new environment variable called ANT_HOME that points to the
ANT installation folder, in this case, c:\apache-ant-1.9.4-bin folder.
Append the path to the Apache ANT batch file to the PATH environment
variable. In our case, this would be the c:\apache-ant-1.9.4-
bin\bin folder.
C:\>ant -version
Apache Ant(TM) version 1.9.4 compiled on December 20 2014
If you do not see the above reply, then please confirm if you followed the
installation steps properly.
3
Apache ANT
Installing Eclipse
This tutorial also covers integration of ANT with Eclipse IDE. Hence, if you have
not installed Eclipse already, please download and install Eclipse as given below:
4
Apache ANT
3. ANT BUILD FILES
Typically, ANT’s build file called build.xml should reside in the base directory of
the project. Although, you are free to use other file name or place for the build
file.
For this exercise, create a file called build.xml anywhere in your computer with
the following contents in it:
<?xml version="1.0"?>
<project name="Hello World Project" default="info">
<target name="info">
<echo>Hello World - Welcome to Apache Ant!</echo>
</target>
</project>
Note that there must be no blank line(s) or whitespace(s) before the xml
declaration. If you allow them, the following error message occurs while executing
the ant build –
All build files require the project element and at least one target element.
Attributes Description
default The default target for the build script. A project may contain any
number of targets. This attribute specifies which target should
be considered as the default. (Mandatory)
basedir The base directory (or) the root folder for the project. (Optional)
A target is a collection of tasks that you want to run as one unit. In our example,
we have a simple target to provide an informational message to the user.
Targets can have dependencies on other targets. For example, a deploy target
may have a dependency on the package target, the package target may have a
5
Apache ANT
dependency on the compile target, and so forth. Dependencies are denoted using
depends attribute. For example:
Attributes Description
depends Comma separated list of all targets that this target depends on.
(Optional)
unless Adds the target to the dependency list of the specified Extension
Point. An Extension Point is similar to a target, but it does not
have any tasks. (Optional)
The echo task in the above example is a trivial task that prints a message. In our
example, it prints the message Hello World.
To run the ANT build file, start command prompt and navigate to the folder where
the build.xml resides, and type ant info. You can also type ant instead. Both will
6
Apache ANT
work, because info is the default target in the build file. You should see the
following output:
C:\>ant
Buildfile: C:\build.xml
info:
[echo] Hello World - Welcome to Apache Ant!
BUILD SUCCESSFUL
Total time: 0 seconds
C:\>
7
Apache ANT
4. ANT PROPERTY TASK
ANT uses the property element which allows you to specify properties. This
allows the properties to be changed from one build to another, or from one
environment to another.
By default, ANT provides the following pre-defined properties that can be used in
the build file:
Properties Description
8
Apache ANT
ANT also makes the system properties. For Example, file.separator is available to
the build file.
In addition to the above, the user can define additional properties using
the property element. The following example shows how to define a property
called sitename:
<?xml version="1.0"?>
<project name="Hello World Project" default="info">
<property name="sitename" value="www.tutorialspoint.com"/>
<target name="info">
<echo>Apache Ant version is ${ant.version} - You are
at ${sitename} </echo>
</target>
</project>
Running ANT on the above build file produces the following output:
C:\>ant
Buildfile: C:\build.xml
info:
[echo] Apache Ant version is Apache Ant(TM) version 1.9.4
compiled on December 20 2014 - You are at www.tutorialspoint.com
BUILD SUCCESSFUL
Total time: 0 seconds
C:\>
9
Apache ANT
5. ANT PROPERTY FILES
Setting properties directly in the build file is fine if you are working with a handful
of properties. However, for a large project, it makes sense to store the properties
in a separate property file.
It allows you to reuse the same build file, with different property settings
for different execution environment. For example, build properties file can
be maintained separately for DEV, TEST, and PROD environments.
it is useful when you do not know the values for a property (in a particular
environment) up front. This allows you to perform the build in other
environments where the property value is known.
There is no hard and fast rule, but typically the property file is
named build.properties and is placed alongwith the build.xml file. You can
create multiple build properties files based on the deployment environment - such
as build.properties.dev and build.properties.test
The contents of the build property file are similar to the normal java property file.
They contain one property per line. Each property is represented by a name-value
pair. The name-value pairs are separated by equals (=) signs. It is highly
recommended that the properties are annotated with proper comments.
Comments are listed using the hash (#) character.
build.xml
<?xml version="1.0"?>
<project name="Hello World Project" default="info">
<property file="build.properties"/>
<target name="info">
<echo>Apache Ant version is ${ant.version} - You are
at ${sitename} </echo>
</target>
</project>
10
Apache ANT
build.properties
# The Site Name
sitename=www.tutorialspoint.com
buildversion=3.3.2
In addition to the above, ant comes with a number of predefined build properties,
which are listed in the previous section, but is represented below once again.
Properties Description
11
Apache ANT
12
Apache ANT
6. ANT SERVICES
Fileset
The fileset represents a collection of files. It is used as a filter to include or exclude
files that match a particular pattern.
For example, refer the following code. Here, the src attribute (${src}) points to
the source folder of the project. The fileset selects all .java files in the source
folder except those, which contain the word 'Stub'. The casesensitive filter is
applied to the fileset which means that a file with the name Samplestub.java will
not be excluded from the fileset.
Patternset
A patternset is a pattern that allows to filter the files or folders easily, based on
certain patterns. Patterns can be created using the following meta characters:
<patternset id="java.files.without.stubs">
<include name="src/**/*.java"/>
<exclude name="src/**/*Stub*"/>
</patternset>
13
Apache ANT
Filelist
The filelist service is similar to the fileset except the following differences:
Filelist contains explicitly named lists of files and it does not support wild
cards.
Filelist tag can be applied for existing or non-existing files.
Let us see the following example of filelist . Here, the attribute
webapp.src.folder points to the web application source folder of the project.
Filterset
Using filterset service along with the copy task, enables you to replace certain text
in all files that matches the pattern with a replacement value.
A common example is to append the version number to the release notes file, as
shown in the following code. Here, the attribute output.dir points to the output
folder of the project. The attribute releasenotes.dir points to the release notes
folder of the project. The attribute current.version points to the current version
folder of the project. The copy task, as the name suggests, is used to copy files
from one location to another.
<copy todir="${output.dir}">
<fileset dir="${releasenotes.dir}" includes="**/*.txt"/>
<filterset>
<filter token="VERSION" value="${current.version}"/>
</filterset>
14
Apache ANT
</copy>
Path
The path service is commonly used to represent a class path. Entries in the path
are separated using semicolons or colons. However, these characters are replaced
at the run time by the executing system's path separator character.
The classpath is set to the list of jar files and classes in the project, as shown in
the example below. Here, the attribute env.J2EE_HOME points to the
environment variable J2EE_HOME. The attribute j2ee.jar points to the name of
the J2EE jar file in the J2EE base folder.
<path id="build.classpath.jar">
<pathelement path="${env.J2EE_HOME}/${j2ee.jar}"/>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
</path>
15
Apache ANT
7. ANT BUILDING PROJECTS
Now that you have learnt basic services in ANT, it is time to put your knowledge
into action. We will build a project in this chapter. The aim of this chapter is to
build an ANT file that compiles the java classes and places them in the WEB-
INF\classes folder. Consider the following project structure:
C:\work\FaxWebApplication>tree
Folder PATH listing
Volume serial number is 00740061 EC1C:ADB1
C:.
+---db
+---src
. +---faxapp
. +---dao
. +---entity
. +---util
. +---web
+---war
+---images
+---js
+---META-INF
+---styles
+---WEB-INF
+---classes
16
Apache ANT
+---jsp
+---lib
Here is the build.xml required for this project. Let us consider it piece by piece.
The src.dir refers to the source folder of the project, where the java source files
can be found. The web.dir refers to the web source folder of the project. This is
where you can find the JSPs, web.xml, css, javascript, and other web related files.
Finally, the build.dir refers to the output folder of the project compilation.
<?xml version="1.0"?>
<project name="fax" basedir="." default="build">
<property name="src.dir" value="src"/>
<property name="web.dir" value="war"/>
<property name="build.dir" value="${web.dir}/WEB-INF/classes"/>
<property name="name" value="fax"/>
<path id="master-classpath">
<fileset dir="${web.dir}/WEB-INF/lib">
<include name="*.jar"/>
</fileset>
<pathelement path="${build.dir}"/>
</path>
</delete>
</target>
</project>
Let us disscuss above given build.xml in detail First, let us declare some properties
for the source, web, and build folders.
Properties can refer to other properties. The build.dir property makes a reference
to the web.dir property. The src.dir refers to the source folder of the project.
The default target of our project is the compile target. But first let us look at
the clean target. The clean target, as the name suggests, deletes the files in the
build folder.
The master-classpath holds the classpath information. In this case, it includes the
classes in the build folder and the jar files in the lib folder.
<path id="master-classpath">
<fileset dir="${web.dir}/WEB-INF/lib">
<include name="*.jar"/>
</fileset>
<pathelement path="${build.dir}"/>
</path>
Finally, the build target to build the files. First of all, we create the build directory,
if it does not exist. Then we execute the javac command specifying jdk1.5 as our
target compilation. We supply the source folder and the classpath to the javac
task and ask it to drop the class files in the build folder.
<mkdir dir="${build.dir}"/>
<javac destdir="${build.dir}" source="1.5" target="1.5" debug="true"
deprecation="false" optimize="false" failonerror="true">
<src path="${src.dir}"/>
<classpath refid="master-classpath"/>
</javac>
</target>
Executing ANT on this file compiles the java source files and places the classes in
the build folder.
C:\>ant
Buildfile: C:\build.xml
BUILD SUCCESSFUL
Total time: 6.3 seconds
19
Apache ANT
8. ANT BUILD DOCUMENTATION
Apache ANT can generate documents on demand. ANT exploits the configuration
options via the javadoc task.
Attributes
You can specify source using the attributes
sourcepath, sourcepathref, or sourcefiles.
the attribute sourcepath to point to the folder of the source files (for
example, src folder).
the attribute sourcepathref to refer to a path, which is referenced by the
path attribute (for example, delegates.src.dir).
the attribute sourcefiles when you want to specify the individual files as a
comma separated list.
You can specify destination path using the destdir folder (for example,
build.dir)
You can filter the javadoc task by specifying the package names to be
included. You can achieve this by using the packagenames attribute, a
comma separated list of package files.
You can filter the javadoc process to show only the public, private, package,
or protected classes, and members. You can achieve this by using the
private,public,package and protected attributes.
You can also tell the javadoc task to include the author and version
information using the respective attributes.
You can also group the packages together using the group attribute, so that
it is easy to navigate.
20
Apache ANT
The following example depicts javadoc task used in our project. Here, we specified
the javadoc to use the src.dir as the source directory, and doc as the target
directory. We also customized the window title, the header, and footer information
that appears on the java documentation pages.
Notice that the data package group has two packages - faxapp.entity and
faxapp.dao.
<target name="generate-javadoc">
<javadoc packagenames="faxapp.*" sourcepath="${src.dir}"
destdir="doc" version="true" windowtitle="Fax Application">
<doctitle><![CDATA[= Fax Application =]]></doctitle>
<bottom>
<![CDATA[Copyright © 2011. All Rights Reserved.]]>
</bottom>
<group title="util packages" packages="faxapp.util.*"/>
<group title="web packages" packages="faxapp.web.*"/>
<group title="data packages"
packages="faxapp.entity.*:faxapp.dao.*"/>
</javadoc>
<echo message="java doc has been generated!" />
</target>
Let us execute the javadoc ANT task. This generates and places the java
documentation files in the doc folder.
C:\>ant generate-javadoc
Buildfile: C:\build.xml
java doc has been generated!
BUILD SUCCESSFUL
Total time: 10.63 second
21
Apache ANT
The java documentation files are now present in the doc folder.
Typically, the javadoc files are generated as part of the release or package targets.
22
Apache ANT
9. ANT CREATING JAR FILES
The next logical step after compiling your java source files, is to build the java
archive, i.e. the JAR file. Creating JAR files with ANT is quite easy with the jar task.
The commonly used attributes of the jar task are:
Attributes Description
The base directory for the output JAR file. By default, this is
basedir
set to the base directory of the project.
compress Advises ANT to compress the file as it creates the JAR file.
update Advises ANT to overwrite files in the already built JAR file.
Continuing our Hello World Fax Web Application project folder structure, let
us add a new target to produce the jar files. But before that let us consider the jar
task given below. Here, the web.dir property points to the path of the web source
23
Apache ANT
files. In our case, this is where the util.jar is placed. The build.dir property points
to the build folder where the class files for the util.jar can be found.
In this example, we create a jar file called util.jar using the classes from
the faxapp.util.*package. However, we are excluding the classes that end with
the name Test. The output jar file is place in the web application lib folder.
<jar destfile="${web.dir}/lib/util.jar"
basedir="${build.dir}/classes"
includes="faxapp/util/**"
excludes="**/Test.class"
/>
<jar destfile="${web.dir}/lib/util.jar"
basedir="${build.dir}/classes"
includes="faxapp/util/**"
excludes="**/Test.class">
<manifest>
<attribute name="Main-Class"
value="com.tutorialspoint.util.FaxUtil"/>
</manifest>
</jar>
To execute the jar task, wrap it inside a target, most commonly the build or
package target, and execute them.
<target name="build-jar">
<jar destfile="${web.dir}/lib/util.jar"
basedir="${build.dir}/classes"
includes="faxapp/util/**"
excludes="**/Test.class">
<manifest>
<attribute name="Main-Class"
value="com.tutorialspoint.util.FaxUtil"/>
</manifest>
</jar>
24
Apache ANT
</target>
Running ANT on this file creates the util.jar file for us.
C:\>ant build-jar
Buildfile: C:\build.xml
BUILD SUCCESSFUL
Total time: 1.3 seconds
25
Apache ANT
Creating WAR files with ANT is extremely simple, and very similar to the creating
JAR files task. After all, WAR file, like JAR file is just another ZIP file.
The WAR task is an extension to the JAR task, but it has some nice additions to
manipulate what goes into the WEB-INF/classes folder, and generating the
web.xml file. The WAR task is useful to specify a particular layout of the WAR file.
Since the WAR task is an extension of the JAR task, all attributes of the JAR task
apply to the WAR task
Attributes Description
Continuing our Hello World Fax Web Application project folder structure, let
us add a new target to produce the jar files. But before that let us consider the
war task. Consider the following example:
As per the previous examples, the web.dir variable refers to the source web
folder, i.e, the folder that contains the JSP, css,javascript files etc.
The build.dir variable refers to the output folder - This is where the classes for
the WAR package can be found. Typically, the classes will be bundled into the
WEB-INF/classes folder of the WAR file.
26
Apache ANT
In this example, we are creating a war file called fax.war. The WEB.XML file is
obtained from the web source folder. All files from the 'WebContent' folder under
web are copied into the WAR file.
The WEB-INF/lib folder is populated with the jar files from the thirdpartyjars folder.
However, we are excluding the portlet.jar as this is already present in the
application server's lib folder. Finally, we are copying all classes from the build
directory's web folder and putting into the WEB-INF/classes folder.
Wrap the war task inside an Ant target (usually package) and run it. This will
create the WAR file in the specified location.
It is entirely possible to nest the classes, lib, metainf and webinf directors so that
they live in scattered folders anywhere in the project structure. But best practices
suggest that your Web project should have the Web Content structure that is
similar to the structure of the WAR file. The Fax Application project has its
structure outlined using this basic principle.
To execute the war task, wrap it inside a target (most commonly, the build or
package target, and run them.
<target name="build-war">
<war destfile="fax.war" webxml="${web.dir}/web.xml">
<fileset dir="${web.dir}/WebContent">
<include name="**/*.*"/>
</fileset>
<lib dir="thirdpartyjars">
<exclude name="portlet.jar"/>
</lib>
<classes dir="${build.dir}/web"/>
</war>
</target>
Running ant on this file will create the fax.war file for us..
C:\>ant build-war
Buildfile: C:\build.xml
BUILD SUCCESSFUL
Total time: 12.3 seconds
27
Apache ANT
The fax.war file is now placed in the output folder. The contents of the war file will
be:
fax.war:
+---jsp This folder contains the jsp files
+---css This folder contains the stylesheet files
+---js This folder contains the javascript files
+---images This folder contains the image files
+---META-INF This folder contains the Manifest.Mf
+---WEB-INF
+---classes This folder contains the compiled classes
+---lib Third party libraries and the utility jar files
WEB.xml Configuration file that defines the WAR package
28
Apache ANT
10. ANT PACKAGING APPLICATIONS
We learnt the different aspects of ANT using the Hello World Fax web application
in bits and pieces.
Now it is time to put everything together to create a full and complete build.xml
file. Consider build.properties and build.xml files as follows:
build.properties
The build.properties is used to mention user defined variables which are helpful to
build the project such as file path, classpath, dburls, etc. The following example
defines deploy.path variable.
deploy.path=c:\tomcat6\webapps
build.xml
Refer the following code. In this example, we first declare the path to the webapps
folder in Tomcat in the build properties file as the deploy.path variable. We also
declare the source folder for the java files in src.dir variable. Then we declare the
source folder for the web files in web.dirvariable. javadoc.dir is the folder for
storing the java documentation, and build.dir is the path for storing the build
output files. Then we declare the name of the web application, which is fax in our
case.
We define the master class path which conains the JAR files present in the WEB-
INF/lib folder of the project. We include the class files present in the build.dir in
the master class path.
The Javadoc target produces the javadoc required for the project and the usage
target is used to print the common targets that are present in the build file.
<?xml version="1.0"?>
29
Apache ANT
<path id="master-classpath">
<fileset dir="${web.dir}/WEB-INF/lib">
<include name="*.jar"/>
</fileset>
<pathelement path="${build.dir}"/>
</path>
<target name="javadoc">
<javadoc packagenames="faxapp.*" sourcepath="${src.dir}"
destdir="doc" version="true" windowtitle="Fax Application">
<doctitle><![CDATA[<h1>= Fax Application
=</h1>]]></doctitle>
<bottom><![CDATA[Copyright © 2011. All
Rights Reserved.]]></bottom>
<group title="util packages" packages="faxapp.util.*"/>
<group title="web packages" packages="faxapp.web.*"/>
<group title="data packages"
packages="faxapp.entity.*:faxapp.dao.*"/>
</javadoc>
</target>
<target name="usage">
<echo message=""/>
<echo message="${name} build file"/>
<echo message="-----------------------------------"/>
<echo message=""/>
<echo message="Available targets are:"/>
<echo message=""/>
<echo message="deploy --> Deploy application
as directory"/>
30
Apache ANT
31
Apache ANT
</war>
<copy todir="${deploy.path}" preservelastmodified="true">
<fileset dir=".">
<include name="*.war"/>
</fileset>
</copy>
</target>
The deploywar target builds the war file and then copies the war file to the deploy
directory of the application server.
32
Apache ANT
11. ANT FOR DEPLOYING
APPLICATIONS
You learnt how to package an application and deploy it to a folder. In this chapter,
we are going to deploy the web application directly to the application server deploy
folder, then we are going to add few ANT targets to start and stop the services.
Let us continue with the Hello World Fax Application folder structure.
build.properties
# Ant properties for building the springapp
appserver.home=c:\\install\\apache-tomcat-7.0.19
# for Tomcat 5 use $appserver.home}/server/lib
# for Tomcat 6 use $appserver.home}/lib
appserver.lib=${appserver.home}/lib
deploy.path=${appserver.home}/webapps
tomcat.manager.url=http://www.tutorialspoint.com:8080/manager
tomcat.manager.username=tutorialspoint
tomcat.manager.password=secret
build.xml
<?xml version="1.0"?>
33
Apache ANT
<path id="master-classpath">
<fileset dir="${web.dir}/WEB-INF/lib">
<include name="*.jar"/>
</fileset>
<pathelement path="${build.dir}"/>
</path>
<target name="javadoc">
<javadoc packagenames="faxapp.*" sourcepath="${src.dir}"
destdir="doc" version="true" windowtitle="Fax Application">
<doctitle><![CDATA[<h1>= Fax Application
=</h1>]]></doctitle>
<bottom><![CDATA[Copyright © 2011. All
Rights Reserved.]]></bottom>
<group title="util packages" packages="faxapp.util.*"/>
<group title="web packages" packages="faxapp.web.*"/>
<group title="data packages"
packages="faxapp.entity.*:faxapp.dao.*"/>
</javadoc>
</target>
<target name="usage">
<echo message=""/>
<echo message="${name} build file"/>
<echo message="-----------------------------------"/>
<echo message=""/>
<echo message="Available targets are:"/>
<echo message=""/>
<echo message="deploy --> Deploy application
as directory"/>
<echo message="deploywar --> Deploy application
as a WAR file"/>
34
Apache ANT
<echo message=""/>
</target>
35
Apache ANT
<fileset dir=".">
<include name="*.war"/>
</fileset>
</copy>
</target>
<path id="catalina-ant-classpath">
<!-- We need the Catalina jars for Tomcat -->
<!-- * for other app servers - check the docs -->
<fileset dir="${appserver.lib}">
<include name="catalina-ant.jar"/>
</fileset>
</path>
<taskdef name="install"
classname="org.apache.catalina.ant.InstallTask">
<classpath refid="catalina-ant-classpath"/>
</taskdef>
<taskdef name="reload"
classname="org.apache.catalina.ant.ReloadTask">
36
Apache ANT
<classpath refid="catalina-ant-classpath"/>
</taskdef>
<taskdef name="list"
classname="org.apache.catalina.ant.ListTask">
<classpath refid="catalina-ant-classpath"/>
</taskdef>
<taskdef name="start"
classname="org.apache.catalina.ant.StartTask">
<classpath refid="catalina-ant-classpath"/>
</taskdef>
<taskdef name="stop"
classname="org.apache.catalina.ant.StopTask">
<classpath refid="catalina-ant-classpath"/>
</taskdef>
In this exercise, we used Tomcat as our application server. First, in the build
properties file, we defined some additional properties.
Applications in Tomcat can be stopped and started using the Tomcat manager
application. The URL for the manager application, username and password are
also specified in the build.properties file. Next, we declare a new CLASSPATH that
contains the catalina-ant.jar. This jar file is required to execute Tomcat tasks
through Apache Ant.
37
Apache ANT
Properties Description
Let us issue the deploy-war command to copy the webapp to the Tomcat
webapps folder and then let us reload the Fax Web application. On executing the
ANT file, the following result is seen:
C:\>ant deploy-war
Buildfile: C:\build.xml
BUILD SUCCESSFUL
Total time: 6.3 seconds
38
Apache ANT
C:\>ant reload
Buildfile: C:\build.xml
BUILD SUCCESSFUL
Total time: 3.1 seconds
Once the above task is run, the web application is deployed and the web
application is reloaded.
39
Apache ANT
12. EXECUTING JAVA CODE
You can use ANT to execute java code. In this example below, the java class takes
in an argument (administrator's email address) and sends out an email.
<?xml version="1.0"?>
<project name="sample" basedir="." default="notify">
<target name="notify">
<java fork="true" failonerror="yes" classname="NotifyAdministrator">
<arg line="[email protected]"/>
</java>
</target>
</project>
C:\>ant
Buildfile: C:\build.xml
40
Apache ANT
notify:
[java] Administrator [email protected] has been notified
BUILD SUCCESSFUL
Total time: 1 second
In this example, the java code does a simple thing - to send an email. We could
have used the built in Ant task to do that. However, now that you have got the
idea you can extend your build file to call java code that performs complicated
things, for example: encrypts your source code.
41
Apache ANT
13. ECLIPSE INTEGRATION
Eclipse comes pre bundled with the ANT plugin, ready for you to use.
Make sure that the build.xml is part of your java project, and does not
reside at a location that is external to the project.
Enable ANT View by following Window > Show View > Other > Ant >
Ant.
Open Project Explorer, drag the build.xml into the ANT View.
Your ANT view looks similar to:
Click on the targets, build / clean / usage to run ANT with the target.
The ANT Eclipse plugin also comes with a good editor for editing build.xml files.
The editor is aware of the build.xml schema and can assist you with code
completion.
To use the ANT editor, right click your build.xml from the Project Explorer and
select Open with > Ant Editor. The Ant Editor should look something similar to:
42
Apache ANT
The Ant editor lists the targets on the right hand side. The target list serves as a
bookmark that allows you to jump straight into editing a particular target.
43
Apache ANT
14. ANT JUNIT INTEGRATION
Junit is the commonly used unit testing framework for development based on Java.
It is easy to use and easy to extend. There are a number of JUnit extensions
available. If you are not much familiar with Junit, download junit
from www.junit.org and read the junit manual.
This tutorial discusses about executing the junit tests using ANT. ANT makes this
straight forward through the Junit task.
Properties Description
jvm Command used to invoke the JVM. This is ignored when fork is
disabled.
showoutput Adivces Ant tosend the output to its logs and formatters
timeout Exits the tests that take longer to run than this setting (in
milliseconds).
44
Apache ANT
Let us continue the theme of the Hello World Fax web application folder structure
and add a junit target.
<target name="unittest">
<junit haltonfailure="true" printsummary="true">
<test name="com.tutorialspoint.UtilsTest"/>
</junit>
</target>
test:
[echo] Testing the application
[junit] Running com.tutorialspoint.UtilsTest
[junit] Tests run: 12, Failures: 0, Errors: 0, Time elapsed: 16.2 sec
BUILD PASSED
45
Apache ANT
15. EXTENDING ANT
ANT comes with a predefined set of tasks, however you are not limited to using
only the available tasks. You can create your own tasks, as shown in the example
below.
package com.tutorialspoint.ant;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.BuildException;
public class MyTask extends Task {
String message;
public void execute() throws BuildException {
log("Message: " + message, Project.MSG_INFO);
}
public void setMessage(String message) {
this.message= message;
}
}
To execute the custom task, you need to add the following to the Hello World Fax
Web Application build.xml:
<target name="custom">
<taskdef name="custom" classname="com.tutorialspoint.ant.MyTask" />
<custom message="Hello World!"/>
</target>
Executing the above custom task prints the message 'Hello World!'
c:\>ant custom
test:
[custom] Message : Hello World!
elapsed: 0.2 sec
46
Apache ANT
BUILD PASSED
This is just a simple example, you can use the power of ANT to improve your build
and deployment process.
47