Java Projects: Ant Definition
Java Projects: Ant Definition
build java projects. Ant is an open source tool that is easy to use and platform
independent. This tutorial serves the purposes for both the beginners and experienced
individuals. Going through it, a beginner can easily and quickly learn how to use Ant
to build his/her own java projects; and if you are already exposed to Ant, then also you
will be benefited by this tutorial learning more and advanced features of Ant.
Introduction to Ant and its history, from installing it on your system to how to run and
use it - every aspect will be described here with illustration and examples.
Ant Definition
Apache Ant is an open source, cross-platform based build tool that is used to describe a
build process and its dependencies and implemented in XML scripts using Java
classes that ensures its extensibility to any development environment (based on Java)
and its integrity with other build tools.
Why Ant?
History of Ant
Installation
In this section, you will learn how to install Ant into your system.
The current version 1.7.1 of Ant was released on 27 June, 2008. It is available for
download at http://ant.apache.org/bindownload.cgi. Here, you have to click on the link
apache-ant-1.7.1-bin.zip in the .zip archive to download the zip file. After the
download completes, unzip this file and install the apache-ant-1.7.1 folder to the root
directory of C:\ drive. Therefore, path for the Ant directory will be C:\apache-ant-
1.7.1. Now, we have to set some environment variables. Select the item "System" in
the Control Panel of your system and click on the tab named "Advanced". You will see
a button named "Environment Variables" appears, click it. Under Environment
variables, you will find two user variables, viz., ANT_HOME and CLASS_PATH. Set
the path value C:\apache-ant-1.7.1 to ANT_HOME variable and the path value
C:\apache-ant-1.7.1\lib; to CLASS_PATH variable. Again in the System
Variables, we have to set the value C:\jdk to JAVA_HOME variable and the value
C:\apache-ant-1.7.1\bin; to Path variable.
Now, you have to check whether the installation process is done properly or not. To do
this, open the Command Prompt and run the command C:\>ant. If the following
message
appears, then it indicates that your installation is successful; Ant is properly installed in
your system..
This example shows how to generate the build.xml file. You may say that build.xml
file is the backbone of ANT (Another Neat Tool) technology. Each build.xml file
contains only one project name and at least one target. The project tag has only three
attributes:
Project
The project tag is used to create our project and its attributes are used to handle further
processing of the project. The name attribute is used to specify the project name and
the default attribute is used to specify which target will be called as default when the
program will run and the basedir attribute is used to calculate the absolute path of the
parent directory.
An another tag is Target tag which has following five attributes:
Target
In the target tag, name attribute is used for target name and the depends attribute is
used to give sequence of target, i.e., which target will execute first, one target may
depend on one or more other targets. The if attribute is used in case of condition,
whether property exists or not; then this target will be executed and unless attribute is
used as like else condition whether property does not exist, the target will not be
executed and the description attribute is used for only giving details about target.
Next tag is property tag which has following four attribute:
Property
In the property tag, name attribute is used for property name; it is case sensitive. The
value tag is used to give the task which will be the name of property in this format "$
{name}", and the location tag is used to specify the location of task where it performs
and the file tag is used to import all properties of ant file. The complete build.xml file
structure is as follows:
</project>
The above build.xml file is used to create directory, compile source code, create jar file
and clean the directory if already exists. To check the program, simply copy and paste
the above code and give appropriate path; then run with ant command on the
command prompt. The output is as follows:
The output shows that a jar file named roseindia.jar is created but in this jar file only
manifest file is created. When you make a java file in the src folder and run with ant
command, the jar file is created completely.
class Hello
{
public static void
main(String args[]){
System.out.println("sandeep
kumar suman");
}
}
To check your program, compile it with ant command on the console; then the
following output will be displayed:
This is a simple example that illustrates how to find the basedir name, file name,
project name, ant version, java version, operating system
name, ant home directory name, java home directory name, user home directory name
and user name. Ant provides you with certain built-in properties that you may find useful
during your build process. The following table shows the property name and it's
description.
Ant's built-in properties:
Property Description
<target name="echo">
<echo message="The operating system is: ${os.name}"/>
</project>
This build.xml file is used to compile and run the java file and print the value on
command prompt. Here we are using five targets, the "clean" target deletes any
previous "build", "classes" and "jar" directory; second one is used to create all
necessary directories and it depends on <target name="clean">; third one is used to
compile the java file from "src" directory to transform source files in to object files in
the appropriate location in the build directory and it depends on <target
name="prepare">; fourth one is used to create the jar file and it depends on <target
name="compile">, it means that after completion of compile target, it will be
executed; fifth one is used to run the jar file and it depends on <target name="jar">
and finally <target name="main"> is used to specify the default project name, it
means that when the program will be run, it will be called first and it depends on
<target name="run">.
<?xml version="1.0"?>
<project name="Roseindia" default="main" basedir=".">
<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/>
<property name="main-class" value="Roseindia"/>
<target name="clean">
<delete dir="${classes.dir}"/>
<delete dir="${jar.dir}"/>
<delete dir="${build.dir}"/>
</target>
<target name="prepare" depends="clean">
<mkdir dir="${build.dir}"/>
<mkdir dir="${classes.dir}"/>
<mkdir dir="${jar.dir}"/>
<mkdir dir="${src.dir}"/>
</target>
<target name="compile" depends="prepare">
<javac srcdir="${src.dir}" destdir="${classes.dir}"/>
</target>
<target name="jar" depends="compile">
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
</target>
<target name="main" depends="run">
<echo message="main target completed.." />
</target>
</project>
If you run this build.xml file using ant command, then the following output will be
displayed.
In this output display, the class name Roseindia is not found by the compiler. Create a
java file as shown below and put it in the src directory, and again run it with ant
command. The following output will be displayed.
class Roseindia{
public static void main(String args[]){
System.out.println("Roseindia Technology Pvt. Ltd.");
}
}
This example illustrates how to call class file through build.xml file. The build.xml
file is used to compile and run the java file and print the calculated value on command
prompt. Here we are using only four targets, "clean" target deletes any previous "build"
directory; second one is used to create "build" and "src" directories which depend on
<target name="clean">, after execution of the clean target, this target will be
executed; third one is used to compile the java file from "src" directory, it transforms
source files in to object files in the appropriate location in the build directory and it
depends on <target name="prepare">; forth one is used to run the class file and it
depends on <target name="compile">. The debug keyword is used to find the error
and optimize is used to find resources of source and destination directory. In the target
run, fork="true" is used to display output and failonerror is used to display error
report.
<?xml version="1.0"?>
<project name="add" default="run" basedir=".">
<target name="clean">
<delete dir="build"/>
</target>
</project>
Simply copy and paste the following source code in the src directory and then run with
ant command. The following output will be displayed:
class Addition{
public static void
main(String[] args) {
int a=5;
int b=10;
int add = a + b;
System.out.println("Addition =
" + add);
}
}
This example illustrates how to create table through the build.xml file by simply
running the ant command. In this build.xml file, we are using 4 property elements for
connectivity of database. The first property <property name="sql.driver"> is used to
connect the sql driver. The second property <property name="sql.url"> is used to
define the database url and database name. The third property <property
name="sql.user"> is used to define user name of the database. The fourth property
<property name="sql.pass"> is used to define the password name of the database.
<target name="createTables">
<sql driver="${sql.driver}" url="${sql.url}" userid="${sql.user}"
password="${sql.pass}" >
<transaction src="client.sql"/>
<transaction src="project.sql"/>
</sql>
</target>
</project>
client.sql
project.sql
Create the both client.sql and project.sql files with the build.xml file and simply run
the build.xml file with ant command in the appropriate path on command prompt. If
the program executes successfully, then the following output will be displayed.
This example illustrates how to insert data in table through the build.xml file by
simply running the ant command. In this build.xml file, we are using 4 property
elements for connectivity from database. The first element <property
name="sql.driver"> is used to connect from the sql driver
. The second element <property name="sql.url"> is used to define the database url and
database name. The third element <property name="sql.user"> is used to define user
name of the database. The fourth element <property name="sql.pass"> is used to
define the password name of the database.
In this build.xml file, <target name="createTables"> is used to execute the query
which is in the client.sql and project.sql file and <target name"insertData"> is used
to execute the query which is in the insertclient.sql and insertproject.sql file. The
source code of the build.xml file is as follows:
</project>
client.sql
project.sql
insertclient.sql
INSERT INTO client (client_name) VALUES
("Galanthus nivalis");
INSERT INTO client (client_name) VALUES
("Narcissus pseudonarcissus");
INSERT INTO client (client_name) VALUES
("Narcissus poeticus var. recurvus");
INSERT INTO client (client_name) VALUES
("Leucojum vernum");
INSERT INTO client (client_name) VALUES
("Iris pseudacorus");
INSERT INTO client (client_name) VALUES
("Crocus tommasinianus");
INSERT INTO client (client_name) VALUES
("Colchicum autumnale");
INSERT INTO client (client_name) VALUES
("Hyacinthoides non-scripta");
INSERT INTO client (client_name) VALUES
("Erythronium dens-canis");
INSERT INTO client (client_name) VALUES
("Fritillaria meleagris");
INSERT INTO client (client_name) VALUES
("Cyclamen coum");
INSERT INTO client (client_name) VALUES
("Tulipa turkestanica");
INSERT INTO client (client_name) VALUES
("Ranunculus ficaria");
insertproject.sql
This example illustrates how to insert and update data in table through the build.xml
file by simply running the ant command. In this build.xml file, we are using 4
property elements for connectivity from database. The first property <property
name="sql.driver"> is used to connect from the sql driver. The second property
<property name="sql.url"> is used to define the database url and database
name. The third property <property name="sql.user"> is used to define user name of
the database. The fourth property <property name="sql.pass"> is used to define the
password name of the database.
</project>
client.sql
create table client (
client_id int not null
auto_increment primary key,
client_name text not null
);
project.sql
create table project (
project_id int not null
auto_increment primary key,
project_name text not null
);
insertclient.sql
INSERT INTO client (client_name) VALUES
("Galanthus nivalis");
INSERT INTO client (client_name) VALUES
("Narcissus pseudonarcissus");
INSERT INTO client (client_name) VALUES
("Narcissus poeticus var. recurvus");
INSERT INTO client (client_name) VALUES
("Leucojum vernum");
INSERT INTO client (client_name) VALUES
("Iris pseudacorus");
INSERT INTO client (client_name) VALUES
("Crocus tommasinianus");
INSERT INTO client (client_name) VALUES
("Colchicum autumnale");
INSERT INTO client (client_name) VALUES
("Hyacinthoides non-scripta");
INSERT INTO client (client_name) VALUES
("Erythronium dens-canis");
INSERT INTO client (client_name) VALUES
("Fritillaria meleagris");
INSERT INTO client (client_name) VALUES
("Cyclamen coum");
INSERT INTO client (client_name) VALUES
("Tulipa turkestanica");
INSERT INTO client (client_name) VALUES
("Ranunculus ficaria");
insertproject.sql
INSERT INTO project (project_name)
VALUES ("codingdiary.com");
INSERT INTO project (project_name)
VALUES ("roseindia.net");
INSERT INTO project (project_name)
VALUES ("allcooljobs.com");
INSERT INTO project (project_name)
VALUES ("huntarticles.com");
INSERT INTO project (project_name)
VALUES ("onlinefreetrading.com");
INSERT INTO project (project_name)
VALUES ("allcoolloans.com");
INSERT INTO project (project_name)
VALUES ("newstrackindia.com");
INSERT INTO project (project_name)
VALUES ("artsandlitreture.com");
INSERT INTO project (project_name)
VALUES ("javajazzup.com");
INSERT INTO project (project_name)
VALUES ("whitecollers.com");
INSERT INTO project (project_name)
VALUES ("singlepane.com");
INSERT INTO project (project_name)
VALUES ("ilikeyoutube.com");
INSERT INTO project (project_name)
VALUES ("fake.com");
updateclient.sql
updateproject.sql
This example illustrates how to drop table through the build.xml file by simply
running the ant command. In this build.xml file, we are using 4 property elements for
connectivity of database. The first property <property name="sql.driver"> is used to
connect the sql driver. The second property <property name="sql.url"> is used to
define the database url and database name. The third property <property
name="sql.user"> is used to define user name of the database. The fourth property
<property name="sql.pass"> is used to define the password name of the database.
</project>
client.sql
project.sql
deleteclient.sql
drop table
client;
deleteproject.sql
drop table
project;
</project>
source code of build.properties:
property.example=Local File
property.file.example=build.properties
Run this program on command prompt - the following output will be displayed.
This example illustrates how to access various system properties using Ant. Ant
provides access to all system properties as if they had been defined using a
<property> task. Here is a list of the properties with descriptions.
Property Description
</project>
build.xml
<target name="check">
<fail unless="env.TOMCAT_HOME">TOMCAT_HOME class path must be set</fail>
</target>
</project>
Output:
But if TOMCAT_HOME environment variable is not set, then the following error
message will be displayed.
This example illustrates how to make directory, how to compile java file and how to
create jar file. This is a simple program that uses <classpath refid="test.classpath">
to map with the jar file. In this example five targets are used, the first target <target
name="clean"> is used to delete the build and the dist directory. The second target
<target name="prepare"> is used to create the build and the dist directory. The third
target <target name="compile"> is used to compile the java file and copy the class
file in build directory. The fourth target <target name="jar"> is used to create the jar
file in the dist directory from the name of test.jar. The fifth target <target
name="test"> is used to map with the class path by the reference id. The source code
of build.xml file is as follows:
<path id="test.classpath">
<pathelement location="dist/test.jar"/>
</path>
<target name="clean">
<delete dir="build"/>
<delete dir="dist"/>
</target>
</project>
Run this program on the appropriate path with ant command. The following output
will be displayed.
Setting properties in the build file is the first method of providing custom properties
with <property> element in an ant build file. Unlike the <project> and <target>
elements, the <property> element is defined as a task. This means that you can
include <property> elements inside a target depending on certain conditions or
depending on which target has been selected. You can also set properties at the
beginning of a build file so that they apply to the entire build file. This means that you
can set important constant values in a central location so that they are easy to find and
change. You should remember that properties
set inside a target override any properties set at the project level. Naming again comes
into this and you should consider whether your target level properties should be identified
as such by using a prefix to avoid confusion and possible namespaces clashes.
The simplest and most obvious use of the <property> task is to set a property using a
name value pair, as shown below.
You can set the value of a property to the value of another property. This can be useful
if you will be referencing a verbose built-in property multiple times. This is as simple
as placing a property marker in the value attribute of a <property> task as shown
below in source code:
build.xml:
<target name="custom">
<echo message="custom.value = ${custom.value}"/>
</target>
</project>
Run this program on the appropriate path, then the following output will be displayed.
This example illustrates how to set memory size of JVM (java virtual machine), when
ANT (another neat tool) is used outside of java virtual machine. In this example,
<property name="sourcedir"> is used to specify the location of source directory and
<property name="targetdir"> is used to specify the location of target directory and
<property name="librarydir"> is used to define the location of library directory.
In this build.xml file, <path id="libraries"> is used to put any jar file in the lib
directory. The target <target name="clean"> is used to delete the target directory and
library directory from base directory. The target <target name="prepare"> is used to
create the source directory, target directory and library directory and <target
name="compile"> is used to compile the source code. The fork="true" is used if
you don't run Java code in a separate JVM to the ant script, you can get some pretty
strange errors that are difficult to diagnose. For NoClassDefFoundError, the problem
was fixed by setting fork=true in the java target. The
memoryMaximumSize="1024m" for the underlying VM, if using fork mode;
ignored otherwise. Defaults to the standard VM memory setting. (Examples:
83886080, 81920k, or 80m) and memoryInitialSize="256m" is used for the
underlying VM, if using fork mode; ignored otherwise. Defaults to the standard VM
memory setting. (Examples: 83886080, 81920k, or 80m). The source code of
build.xml file is as follows:
<path id="libraries">
<fileset dir="${librarydir}">
<include name="*.jar"/>
</fileset>
</path>
<target name="clean">
<delete dir="${targetdir}"/>
<delete dir="${librarydir}"/>
</target>
</project>
Hello.java
class Hello{
public static void main(String
args[]){
System.out.println("Sandeep
kumar suman");
}
}
Create a class file in the 'src' folder and compile it on the console with ant command.
The following output will be displayed.
This example illustrates how to define the property file whether it is local or global.
When you create build.properties on local target, then the echo message prints that
this file is Local but when the file is not created on local target, then it shows the
message Global file.
<property file="build.properties"/>
<target name="global-file">
</target>
</target>
</project>
If any given property file which is not available on local target (code is given below).
<project name="Properties" default="local-file" basedir=".">
<property file="build.properties"/>
<target name="global-file">
<echo message="The value of property.example is: ${property.example}"/>
</target>
</project>
When you run this program, then the following output will be displayed.
In this example, path.separator is used to separate the path and file by semicolon (;).
When it is run, Ant checks for the path separator and directory separator characters
provided by the underlying JVM and uses those values. It successfully interprets either
the ";" or the ":" inside the build file. For example, when run on Unix machine, ant
interprets the path dir;dir\\subdir correctly as the dir;dir\\subdir. Separator must be used
consistently with in the same value type; the string dir;dir\\subdir, combining a
windows path separator (;) and a Unix directory separator (/) is not a good form.
<target name="echo">
<echo message="File: ${basedir}${path.separator}build.xml"/>
<echo message="Path: ${basedir}${path.separator}build.xml${path.separator}
${basedir}${path.separator}build.properties"/>
</target>
</project>
The following output will be displayed if you execute ant command on the command
prompt with appropriate path.