Академический Документы
Профессиональный Документы
Культура Документы
Index:
• Apache Ant - Building Simple Java Projects
• Built-In-Properties
• Check Properties
• Path Separator
• File Separator
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
Build Automation: Ant provides automated build processes that is faster and
more efficient than manual procedures and other build tools can also be
integrated with it.
Compilation of Source Code: Ant can use and compile source code from a
variety of version controls and packaging of the compiled code and resources can
also be done.
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
build.xml File
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
Attribute Description Requirement
name project name not necessary
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.
Target
Attribute Description Requirement
name target name necessary
depends depends on another target not necessary
the name of the property that must be set in
if not necessary
order for this target to execute.
the name of the property that must not be set
unless not necessary
in order for this target to execute.
description short description about target not necessary
<target name="buildWar" depends="init" description="build a war file"/>
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.
Property
Attribute Description Requirement
<property file="build.properties"/>
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.
Property Description
ant.file The absolute path of the build file
The name of the project as set in the <project> element's
ant.project.name
name attribute.
ant.home The root directory of ant
The version of this ant installation. This is not just the
ant.version version number and includes information such as the
compilation date.
ant.java.version The version of the java that ant uses
basedir The absolute path of the project
os.name Operating system name
java.home Java home directory name
user.home User directory name
user.name User name
<?xml version="1.0"?>
<target name="echo">
<echo message="The operating system is: ${os.name}"/>
</project>
<?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>
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.");
}
}
<?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);
}
}
<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.
</project>
client.sql
project.sql
insertclient.sql
insertproject.sql
</project>
client.sql
project.sql
insertclient.sql
insertproject.sql
updateclient.sql
updateproject.sql
</project>
client.sql
project.sql
deleteclient.sql
deleteproject.sql
of java. The target <target name="build.path"> is used to print the file name
and path of file and finally <target name="environment"> is used to print the
processor architecture name and operating system name and print the ant home
directory name.
</project>
property.example=Local File
property.file.example=build.properties
Run this program on command prompt - the following output will be displayed.
Property Description
the absolute path of the project's basedir (as set
basedir
with the basedir attribute of <project>).
ant.file the absolute path of the buildfile.
ant.version the version of Ant
the name of the project that is currently
ant.project.name executing; it is set in the name attribute of
<project>.
</project>
Checking Properties:
This example illustrates how to check properties using environment
variable whether it is set or not. In this code, there are three properties; the
first two are used to define source directory and destination directory. The
source directory is 'src' and the destination directory is 'build'. The element
<property environment="env"> is a path of jar file dependent on environment
variables, and these are available only if you use <property environment="env">
before you import the property file. The following example shows how to check
whether TOMCAT_HOME environment variable is set or not. If
TOMCAT_HOME environment variable is set, then the output will display build
successful... as given below.
build.xml
<property environment="env"/>
<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.
<path id="test.classpath">
<pathelement location="dist/test.jar"/>
</path>
<target name="clean">
<delete dir="build"/>
<delete dir="dist"/>
</target>
class Test{
public static void main(String args[]){
System.out.println("RoseIndia Technology Pvt. Ltd.");
}
}
Run this program on the appropriate path with ant command. The following
output will be displayed.
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.
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.
If any given property file which is not available on local target (code is given
below).
<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.
Path Separator
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.
<path id="project.classpath">
<pathelement location="${dir.src}"/>
<fileset dir="${tomcatHome}/common/lib">
<include name="*.jar"/>
</fileset>
<fileset dir="${dir.lib}">
<include name="*.jar"/>
</fileset>
</path>
<target name="clean">
<delete dir="${dir.build}"/>
<delete dir="${dir.lib}"/>
</target>
<mkdir dir="${dir.src}"/>
</target>
<target name="compile" depends="prepare">
<pathconvert targetos="windows" property="windowsPath" refid="project.classpath"/>
<echo>Path Directory = ${windowsPath}</echo>
<javac destdir="${dir.build}" srcdir="${dir.src}">
<classpath refid="project.classpath"/>
</javac>
</target>
</project>
File Separator:
This example allows you to build platform-specific paths and directory
hierarchies. When you build a path with any of ant's task, Ant is quite happy to
convert the separators in to ones appropriate for the operating system on which
it is running. Ant will also do the conversion if you pass the string that you have
built to its tasks. Therefore, you could rewrite the previous listing.
<target name="echo">
</target>
</project>
If the display shows a mixture of file separators, don't disturb ant that is still
treating these as strings. The following output will be displayed if you execute
ant command on the command prompt.