tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject DO NOT REPLY [Bug 4917] New: - Better build.xml for tomcat application developers guide
Date Fri, 16 Nov 2001 12:40:01 GMT

Better build.xml for tomcat application developers guide

           Summary: Better build.xml for tomcat application developers guide
           Product: Tomcat 4
           Version: 4.0.1 Final
          Platform: All
        OS/Version: Other
            Status: NEW
          Severity: Enhancement
          Priority: Other
         Component: Unknown

I've been training up developers using an example project based on the 
build.xml in the tomcat developer's guide and the struts sample application.
I've found that there are some duplication issues in the sample build.xml 
which invite errors and increase maintenance. 

There are a couple of bugs with the stated functionality (e.g. in where 
properties are loaded from) but the main issue is with the separate definition 
of compile.classpath . This requires developers to maintain not only the 
classpath but an identical list of files to be copied into the distribution. 
The (working) build.xml below is a rearrangement of the example in the 
documentation to fix these issues, and to make the build work 'out of 
the box' for more projects. See initialled comments ('BE') in the file for more

<!-- A "project" describes a set of targets that may be requested
     when Ant is executed.  The "default" attribute defines the
     target which is executed if no specific target is requested,
     and the "basedir" attribute defines the current working directory
     from which Ant executes the requested task.  This is normally
     set to the current working directory.

<project name="My Project" default="compile" basedir=".">

<!-- ===================== Property Definitions =========================== -->


  Each of the following properties are used in the build script.
  Values for these properties are set by the first place they are
  defined, from the following list:
  * Definitions on the "ant" command line (ant -Dcatalina.home=xyz compile)
  * Definitions from a "" file in the top level
    source directory
  * Definitions from a "" file in the developer's
    home directory
  * Default definitions in this build.xml file

  You will note below that property values can be composed based on the
  contents of previously defined properties.  This is a powerful technique
  that helps you minimize the number of changes required when your development
  environment is modified.  Note that property composition is allowed within
  "" files as well as in the "build.xml" script.

  <!-- BE: added lines below to make the statements above true!! -->
  <property file=""/>
  <property file="${user.home}/"/>

<!-- ==================== File and Directory Names ======================== -->


  These properties generally define file and directory names (or paths) that
  affect where the build process stores its outputs.             Base name of this application, used to
                       construct filenames and directories.
                       Defaults to "myapp".

  app.version          Version identifier for this application.

  build.home           The directory into which the "prepare" and
                       "compile" targets will generate their output.
                       Defaults to "build".

  catalina.home        The directory in which you have installed
                       a binary distribution of Tomcat 4.  This will
                       be used by the "deploy" target.

  deploy.home          The name of the directory into which the
                       deployment hierarchy will be created, and into
                       which the build directory will be copied.
                       Defaults to "${catalina.home}/webapps/${}".

  dist.home            The name of the base directory in which
                       distribution files are created.
                       Defaults to "dist".


  <property name=""      value="myapp"/>
  <property name="app.version"   value="1.0"/>
  <property name="build.home"    value="build"/>
  <!-- BE: set to default path used by tomcat installer. That way it works out 
of the box for some -->
  <property name="catalina.home" value="C:/Program Files/Apache Tomcat 4.0"/> 
<!-- UPDATE THIS! -->
  <property name="deploy.home"   value="${catalina.home}/webapps/${}"/>
  <property name="dist.home"     value="dist"/>

<!--  ==================== Compilation Control Options ==================== -->


  These properties control option settings on the Javac compiler when it
  is invoked using the <javac> task.

  compile.debug        Should compilation include the debug option?

  compile.deprecation  Should compilation include the deprecation option?

  compile.optimize     Should compilation include the optimize option?


  <property name="compile.debug"       value="true"/>
  <property name="compile.deprecation" value="false"/>
  <property name="compile.optimize"    value="true"/>

<!-- ==================== External Dependencies =========================== -->


  Use property values to define the locations of external JAR files on which
  your application will depend.  In general, these values will be used for
  two purposes:
  * Inclusion on the classpath that is passed to the Javac compiler
  * Being copied into the "/WEB-INF/lib" directory during execution
    of the "deploy" target.

  Because we will automatically include all of the Java classes that Tomcat 4
  exposes to web applications, we will not need to explicitly list any of those
  dependencies.  You only need to worry about external dependencies for JAR
  files that you are going to include inside your "/WEB-INF/lib" directory.


<!-- Dummy external dependency -->
  <property name="foo.jar"

<!-- ==================== All Target ====================================== -->


  The "all" target is a shortcut for running the "clean" target followed
  by the "compile" target, to force a complete recompile.


  <target name="all" depends="clean,compile"
   description="Clean build and dist, then compile"/>

<!-- ==================== Clean Target ==================================== -->


  The "clean" target deletes any previous "build" and "dist" directory,
  so that you can be ensured the application can be built from scratch.


  <target name="clean"
   description="Delete old build and dist directories">
    <delete dir="${build.home}"/>
    <delete dir="${dist.home}"/>

<!-- ==================== Compile Target ================================== -->


  The "compile" target transforms source files (from your "src" directory)
  into object files in the appropriate location in the build directory.
  This example assumes that you will be including your classes in an
  unpacked directory hierarchy under "/WEB-INF/classes".

  Rather than relying on the CLASSPATH environment variable, Ant includes
  features that makes it easy to dynamically construct the classpath you
  need for each compilation.  The example below constructs the compile
  classpath to include the servlet.jar file, as well as the other components
  that Tomcat makes available to web applications automatically, plus anything
  that you explicitly added.

  BE: moved classpath definition to here. Since the path now depends on files
  repositioned by the 'prepare' target, we cannot define the path until 
  prepare has been executed.

  <target name="compile" depends="prepare"
   description="Compile Java sources">

    <!-- Compile Java classes as necessary -->
    <mkdir    dir="${build.home}/WEB-INF/classes"/>
    <javac srcdir="src"
        <classpath id="compile.classpath">

          <!-- Include all JAR files from /WEB-INF/lib -->
          <pathelement location="${build.home}/WEB-INF/classes"/>
          <fileset dir="${build.home}/WEB-INF/lib">
            <include name="*.jar"/>

          <!-- Include all elements that Tomcat exposes to applications -->
          <pathelement location="${catalina.home}/common/classes"/>
          <fileset dir="${catalina.home}/common/lib">
            <include name="*.jar"/>
          <pathelement location="${catalina.home}/classes"/>
          <fileset dir="${catalina.home}/lib">
            <include name="*.jar"/>

    <!-- Copy associated resource files -->
    <!-- BE: removed 'library' fom path - it placed properties outside 
classpath -->
    <copy  todir="${build.home}/classes">
    <fileset dir="src" includes="**/*.properties"/>


<!-- ==================== Deploy Target =================================== -->


  The "deploy" target copies the contents of the build directory into a
  location required by our servlet container, and picks up any external
  dependencies along the way.  AFter restarting the servlet container, you
  can now test your web application.


  <target name="deploy" depends="compile"
   description="Deploy application to servlet container">

    <!-- Copy the contents of the build directory -->
    <mkdir     dir="${deploy.home}"/>
    <copy    todir="${deploy.home}">
      <fileset dir="${build.home}"/>

    <!-- BE: removed copying of libraries, now in 'prepare' target -->

<!-- ==================== Dist Target ===================================== -->


  The "dist" target creates a binary distribution of your application
  in a directory structure ready to be archived in a tar.gz or zip file.
  Note that this target depends on two others:
  * "deploy" so that the entire web application (including external
    dependencies) will have been assembled
  * "javadoc" so that the application Javadocs will have been created


  <target name="dist" depends="deploy,javadoc"
   description="Create binary distribution">

    <!-- Copy documentation subdirectory -->
    <copy    todir="${dist.home}/docs">
      <fileset dir="docs"/>

    <!-- Create application JAR file -->
    <jar jarfile="${dist.home}/${}.war"

    <!-- Copy additional files to ${dist.home} as necessary -->


<!-- ==================== Javadoc Target ================================== -->


  The "javadoc" target creates Javadoc API documentation for the Java
  classes included in your application.  Normally, this is only required
  when preparing a distribution release, but is available as a separate
  target in case the developer wants to create Javadocs independently.

  BE: added classpath from compilation, and made this task document
  all packages by default - more useful out of the box

  <target name="javadoc" depends="compile"
   description="Create Javadoc API documentation">

    <mkdir          dir="${dist.home}/docs/api"/>
    <javadoc sourcepath="src"
      <classpath refid="compile.classpath"/>


<!-- ==================== Prepare Target ================================== -->


  The "prepare" target is used to create the "build" destination directory,
  and copy the static contents of your web application to it.  If you need
  to copy static files from external dependencies, you can customize the
  contents of this task.

  Normally, this task is executed indirectly when needed.


  <target name="prepare">

    <!-- Create build directory and copy static content -->
    <mkdir  dir="${build.home}"/>
    <copy todir="${build.home}">
      <fileset dir="web"/>

    <!-- BE: moved copies here so theres no classpath duplication -->
    <!-- Copy external dependencies as required -->
    <mkdir  dir="${build.home}/WEB-INF/lib"/>
    <copy todir="${build.home}/WEB-INF/lib" file="${foo.jar}"/>



To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message