Chapter 3:
Ant
Ant is becoming increasingly widely used as a build and deploy tool for
Java applications. This chapter is about how I am using Ant in this
tutorial. It is nothing particular to do with Jini.
3.1.
Introduction
Applications consisting of multiple source benefit from having some
build tool to automate compilation, deployment, testing, etc.
Many of these tools are operating system specific such as
make
(although Windows versions now exist).
Ant has become an increasingly popular tool for Java applications
since it has cross-platform support and and ant build files can be
written in an O/S independant way.
This tutorial is being adapted to use ant
instead
of make
and Unix shell scripts. This chapter is
about the use of ant
for this tutorial. It is not
about Jini at all, so unless you want to see how the applications
are currently built, skip past this chapter. Individual build
files for each project will be given in the relevant chapters.
3.2.
Toplevel build file
There are two general parameters that need to be set for your own
environment
-
jini.home
- the pathname to where Jini
has been unpacked. This is used to define the
jini.jars
variable which contains the
standard Jini class files
-
localhost
- the IP address or hostname of the
current machine. In my testing, I run basic tests from this
machine
These are defined in the build.xml
in the
tutorial's root directory.
Similar to many projects, we adopt the following directory structure
-
src
- the directory for all source files
-
build
- where all class files are built
-
dist
- where distribution files such as
jar
files are created
-
resources
- where things like policy files
and configuration files are kept
-
httpd.classes
- this is non-standard, but is where
we need to copy files so that an HTTP server can find them
These are all defined in the build.xml
file in the
tutorial's root directory.
The following targets are defined
-
compile
- compile all source files
-
dist
- build the distribution, typically jar files
-
build
- compile and distribute (redundant)
-
deploy
- copy files to their destination, typically
some jar files to an HTTP server
-
clean
- remove all class files, jar files and
source backups
-
run -DrunFile=...
- run a project
-
usage
- print a list of options
The toplevel file build.xml
defines these targets.
The main function of each target is to run the target again in each of
the projects. So compile
runs compile
in each project; deploy
runs deploy
in each project; whereas run
calls run
only in the selected project. The projects are each defined in an Ant file
in the antBuildFiles
directory.
The build.xml
file is
<project name="Jini book" default="usage" basedir=".">
<!-- CONFIGURABLE STUFF HERE -->
<property name="jini.home" value="/usr/local/jini2_0"/>
<property name="localhost" value="192.168.1.13"/>
<!-- END CONFIGURABLE STUFF -->
<!-- Libraries -->
<property name="jini.jars"
value="${jini.home}/lib/jini-core.jar;${jini.home}/lib/jini-ext.jar;${jini.home}/lib/sun-util.jar"/>
<path id="compile.classpath">
<pathelement path="${jini.jars}" />
<pathelement path="build" />
</path>
<!-- Directories -->
<property name="src" value="${basedir}\src"/>
<property name="dist" value="${basedir}\dist"/>
<property name="build" value="${basedir}\build"/>
<property name="res" value="${basedir}/resources"/>
<property name="httpd.classes" value="/home/httpd/html/classes/"/>
<!-- Show the usage options to the user -->
<target name="usage" >
<echo message=" compile"/>
<echo message=" dist"/>
<echo message=" build"/>
<echo message=" deploy"/>
<echo message=" clean"/>
<echo message=" run -DrunFile='...' [-Dconfig='...']"/>
<echo message=" usage"/>
</target>
<target name="all" depends="init,compile"/>
<!-- CLEAN -->
<target name="clean">
<!-- Delete our the ${build}, and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
<!-- delete all ~ backup files -->
<delete>
<fileset dir="." defaultexcludes="false" includes="**/*~"/>
</delete>
<!-- delete all .bak backup files -->
<delete>
<fileset dir="." defaultexcludes="false" includes="**/*.bak"/>
</delete>
</target>
<target name="init">
<!-- Create the build directory structure used by compile N deploy -->
<mkdir dir="build"/>
<mkdir dir="dist"/>
</target>
<!-- call "compile" target in all build files in "antBuildFiles" dir -->
<target name="compile" depends="init">
<subant target="compile" inheritall="true">
<fileset dir="antBuildFiles"
includes="*.xml"/>
</subant>
</target>
<!-- call "dist" target in all build files in "antBuildFiles" dir -->
<target name="dist" depends="compile">
<subant target="dist" inheritall="true">
<fileset dir="antBuildFiles"
includes="*.xml"/>
</subant>
</target>
<!-- call "deploy" target in all build files in "antBuildFiles" dir -->
<target name="deploy" depends="dist">
<subant target="deploy" inheritall="true">
<fileset dir="antBuildFiles"
includes="*.xml"
/>
</subant>
</target>
<target name="build" depends="dist,compile"/>
<!-- call "run" on antfile determined by "runFile" property -->
<target name="run">
<ant
antfile="antBuildFiles/${runFile}.xml"
target="run"/>
</target>
</project>
3.3.
Project files
Each project is defined in an Ant file in the antBuildFiles
directory. The purpose is to implement the toplevel build targets for each
project. Each of these project files inherits values from the toplevel file,
namely
-
jini.home
-
jini.jars
-
src
-
dist
-
build
-
httpd.classes
Each project uses only a small number of the files from the
src
directory. These are defined in the
src.files
variable. For example, for the
complete.FileClassifierServer
project discussed
in the chapter "A Simple Example" the source files are defined by
<property name="src.files"
value="
common/MIMEType.java,
common/FileClassifier.java,
complete/FileClassifierImpl.java,
complete/FileClassifierServer.java
"
/>
Since Jini is a distributed system, not all class files are required by
all components. Typically a server will require some files whereas a
client will require others. These are defined by two further variables
<!-- Class files to run the server -->
<property name="class.files"
value="
common/MIMEType.class,
common/FileClassifier.class,
complete/FileClassifierImpl.class,
complete/FileClassifierServer.class
"
/>
<!-- Class files for the client to download --->
<property name="class.files.dl"
value="
complete/FileClassifierImpl.class
"
/>
The rest of each project file is fairly straightforward. The
compile
target compiles all files in the
src.files
list; the dist
target builds jar files: usually two of them, one for the server
and one for the client; the deploy
target copies
the jar files for the client to an HTTP server; the run
target starts a JVM with appropriate parameters. Note that the JVM must be
started as a separate VM as it sets a security policy (discussed later)
which cannot be done within an already running Ant JVM.
The complete project file for complete.FileClassifierServer
is in the file complete.FileClassifierServer.xml
:
<!--
Project name must be the same as the filename which must
be the same as the main.class. Builds jar files with the
same name
-->
<project name="complete.FileClassifierServer">
<!-- Inherits properties from ../build.xml:
jini.home
jini.jars
src
dist
build
httpd.classes
localhost
-->
<!-- files for this project -->
<!-- Source files for the server -->
<property name="src.files"
value="
common/MIMEType.java,
common/FileClassifier.java,
complete/FileClassifierImpl.java,
complete/FileClassifierServer.java
"/>
<!-- Class files to run the server -->
<property name="class.files"
value="
common/MIMEType.class,
common/FileClassifier.class,
complete/FileClassifierImpl.class,
complete/FileClassifierServer.class
"/>
<!-- Class files for the client to download -->
<property name="class.files.dl"
value="
common/MIMEType.class,
common/FileClassifier.class,
complete/FileClassifierImpl.class
"/>
<!-- Uncomment if no class files downloaded to the client -->
<!-- <property name="no-dl" value="true"/> -->
<!-- derived names - may be changed -->
<property name="jar.file"
value="${ant.project.name}.jar"/>
<property name="jar.file.dl"
value="${ant.project.name}-dl.jar"/>
<property name="main.class"
value="${ant.project.name}"/>
<property name="codebase"
value="http://${localhost}/classes/${jar.file.dl}"/>
<!-- targets -->
<target name="all" depends="compile"/>
<target name="compile">
<javac destdir="${build}" srcdir="${src}"
classpath="${jini.jars}"
target="1.2"
includes="${src.files}">
</javac>
</target>
<target name="dist" depends="compile"
description="generate the distribution">
<jar jarfile="${dist}/${jar.file}"
basedir="${build}"
includes="${class.files}"/>
<antcall target="dist-jar-dl"/>
</target>
<target name="dist-jar-dl" unless="no-dl">
<jar jarfile="${dist}/${jar.file.dl}"
basedir="${build}"
includes="${class.files.dl}"/>
</target>
<target name="build" depends="dist,compile"/>
<target name="run" depends="build,deploy">
<java classname="${main.class}"
fork="true"
classpath="${jini.jars}:${dist}/${jar.file}">
<jvmarg value="-Djava.security.policy=${res}/policy.all"/>
<jvmarg value="-Djava.rmi.server.codebase=${codebase}"/>
</java>
</target>
<target name="deploy" depends="dist" unless="no-dl">
<copy file="${dist}/${jar.file.dl}"
todir="${httpd.classes}"/>
</target>
</project>
3.4. Copyright
If you found this chapter of value, the full book is available
from
APress or
Amazon .
There is a review of the book at
Java Zone . The current edition of the book does not yet deal with Jini 2.0, but the
next edition will.
This file is Copyright (©) 1999, 2000, 2001, 2003, 2004
by Jan Newmarch
(http://jan.netcomp.edu.au)
jan.newmarch@infotech.monash.edu.au.
This work is licensed under a
Creative Commons License, the replacement for the earlier Open Content License.
| | Copyright© 1998-2004 All Rights Reserved. No portion of this site may be reproduced or redistributed without prior written permission from VistaEdge Technologies
All registered trademarks appearing on this site are the property of their respective owners. Java is a trademark or registered trademark of Sun Microsystems, Inc. in the United States and other countries. This site is not connected to Sun Microsystems, Inc. and is not sponsored by Sun Microsystems, Inc. | |
|