Home arrow Java arrow Page 7 - Development and Build System with ANT

Development and Build System with ANT

This article will describe some of the development tools that are available from the Java Open Source community and explore how they can help you create high-quality software in a predictable and reproducible fashion, saving you precious time and money. It is taken from chapter three of the book Enterprise Java Development on a Budget, written by Brian Sam-Bodden and Christopher M. Judd (Apress, 2004; ISBN: 1590591259).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 25
September 08, 2005
  1. · Development and Build System with ANT
  2. · Continuous Integration
  3. · Automating the Build Process with Apacheís Ant
  4. · Introduction to Ant
  5. · More on Targets
  6. · Properties
  7. · Case Study: Building the Technology Conference Management System with Ant
  8. · JavaDoc Generation
  9. · Checking Code Conventions with Checkstyle
  10. · Generating Source-Code Metrics
  11. · The all Target

print this article

Development and Build System with ANT - Case Study: Building the Technology Conference Management System with Ant
(Page 7 of 11 )

To set the stage for the development throughout the rest of the book, you need to first create a suitable directory structure (see Figure 3-5) as well as an initial Ant buildfile for the TCMS system.

Figure 3-5.  Sample directory structure for the TCMS project

The projectís root directory is tcms. Under this directory youíll place the projectís main buildfile named build.xml. The subdirectories under tcms are organized as follows:

  • lib: Contains any libraries required at runtime by the application(s).


  • lib/development: Contains any libraries required at development time.


  • src: The root directory for all nongenerated sources.


  • src/java: The root directory for all Java sources.


From the previous directory structure, it should be clear that you must take measures to differentiate the needs of the application at development or build time vs. the needs of the runtime environment.

Now that you have a suitable directory structure, your next step should be to start putting together the tcms buildfile. The project element contains the name of your project and a nested description element.


Use the project element nested description. This description is shown on the console when invoking Ant with the -projecthelp command-line option.

The default target will be the all target, which youíll develop later in the chapter. The basedir is set to be the directory where the buildfile resides, which in this case is the tcms directory.

<?xml version="1.0"?>
    name="Enterprise Java Development on a Budget"
    This build script was developed to be a generic enterprise development
    build script using ANT 1.5.3 (ant.apache.org). To customize it or use it for
    other projects modify the build.properties file.

Next, properties are defined for the created directories. Notice that you can define properties using other properties as with the lib-dev property. Properties that represent a directory are defined using the location attribute instead of the value attribute. The location attribute gets resolved to the full path relative to the basedir specified in the project element.


Making all paths relative to the projectís basedir directory and avoiding the use of absolute paths guarantees that your buildfile will work anywhere. If your build depends on a resource whose location might change from environment to environment, you should place the location of said resource in a properties file or use environment variables such as ${os.name}.

The build directory is the root directory for all products of the build process, such as the classes directory, where the results of compiling the classes under src/java will be placed.

<!-- ====================================================== -->
<!-- Initialization
<!-- ====================================================== -->
<property name="build" location="build"/>
<property name="lib" location="lib"/>
<property name="lib-dev" location="${lib}/development"/>
<!-- Directories -->
<property name="src" location="src"/>
<property name="src-java" location="${src}/java"/> <property name="classes" location="${build}/classes"/> <property name="docs" location="docs"/>
<property name="api" location="${docs}/api"/>

Paths representing all the JAR files under the lib directory (class.path) and all class files under the classes directory are created.


A common practice in Ant buildfiles is to have an init task that all other tasks depend on. We advocate not using the init task for setting up properties, loading properties files, paths, patternsets, or taskdefs. Instead just place them before the first target and they will be added to the implicit target. As mentioned earlier, the contents of the implicit target always get called and you donít have to remember making all other targets dependent on an init target.

A patternset is also used to filter a directory for nonsource files. In the case where resources are part of the source directory, such as property files or images, you can use a patternset to copy them to the location of the compiled classes, which will require said resources.

<!-- Paths -->
<path id="class.path">
    <fileset dir="${lib}">
        <include name="*.jar"/>
<path id="app.class.path">
    <pathelement location="${classes}"/>
    <path refid="class.path"/>
<!-- Patternsets -->
<patternset id="non.source.set">
    <exclude name="**/*.java"/>
    <exclude name="**/read-me.txt"/>
    <exclude name="**/package.html"/>


Now itís time to add the first target to the buildfile, the compile target. This target will make use of the javac task, which is a wrapper to the javac command. Notice that before the javac task is invoked, all files under the ${src-java} directory that match the patternset non.source.set are copied to the ${classes} directory. This is done so that any resources such as Java properties files, images, and others are available to the compiled code under the classes directory. This is a common practice for many IDEs.

<!-- ====================================================== -->
<!-- Compiles all the classes
<!-- ====================================================== -->
    description="Compiles all classes.">
<path refid="ejb.source.path"/>
    <!-- copy non-source resources only if the compilation is successful -->
    <copy todir="${classes}">
        <fileset dir="${src-java}">
            <patternset refid="non.source.set"/>
<target name="compile-init">
    <mkdir dir="${classes}"/>
<target name="compile-clean">
    <delete dir="${classes}"/>

Notice that you added two more targets other than compile. These are compile-init and compile-clean. The compile-init target simply creates the classes directory by making use of the mkdir task. The compile-clean uses the delete task to remove the directory and all of its contents.


For each main target in the buildfile, add a target-init and a target-clean, where target is the name of the main target. This makes it fairly straightforward to determine the resources needed and created by a target and also makes it easier to maintain large buildfiles. For simple buildfiles a single clean target will usually suffice.

blog comments powered by Disqus

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials