Ant Build File to Compile and Package the Example

The following sample Ant build.xml file compiles the servlet code and packages all the Web application artifacts into a deployable WAR file; see Description of the build.xml File for additional information.

<project name="My Project" default="help" basedir=".">
  <!-- Define the properties used by the build -->
  <property name=""      value="hello"/>
  <property name="tcserver.home" value="/opt/vmware/vfabric-tc-server-standard/tomcat-6.0.25.A.RELEASE" />
  <property name="work.home"    value="${basedir}/work"/>
  <property name="dist.home"     value="${basedir}/dist"/>
  <property name="src.home"      value="${basedir}/src"/>
  <property name="web.home"      value="${basedir}/web"/>

  <target name="help">
    <echo>You can use the following targets:</echo>
    <echo> </echo>
    <echo>  help    : (default) Prints this message </echo>
    <echo>  all     : Cleans, compiles, and packages application</echo>
    <echo>  clean   : Deletes work directories</echo>
    <echo>  compile : Compiles servlets into class files</echo>
    <echo>  dist    : Packages artifacts into a deployable WAR</echo>
    <echo>For example, to clean, compile, and package all at once, run:</echo>
    <echo>prompt> ant all </echo>

  <!-- Define the CLASSPATH -->
  <path id="compile.classpath">
    <fileset dir="${tcserver.home}/bin">
      <include name="*.jar"/>
    <pathelement location="${tcserver.home}/lib"/>
    <fileset dir="${tcserver.home}/lib">
      <include name="*.jar"/>

  <target name="all" depends="clean,compile,dist"
          description="Clean work dirs, then compile and create a WAR"/>

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

  <target name="prepare" depends="clean"
          description="Create working dirs and copy static files to work dir">
    <mkdir  dir="${dist.home}"/>
    <mkdir  dir="${work.home}/WEB-INF/classes"/>
    <!-- Copy static HTML and JSP files to work dir -->
    <copy todir="${work.home}">
      <fileset dir="${web.home}"/>

  <target name="compile" depends="prepare"
          description="Compile Java sources and copy to WEB-INF/classes dir">
    <javac srcdir="${src.home}"
        <classpath refid="compile.classpath"/>
    <copy  todir="${work.home}/WEB-INF/classes">
      <fileset dir="${src.home}" excludes="**/*.java"/>


  <target name="dist" depends="compile"
          description="Create WAR file for binary distribution">
    <jar jarfile="${dist.home}/${}.war"


Description of the build.xml File

The Ant build.xml file defines targets for compiling and packaging the HelloWorld Web application. In preparation, the build process first creates an output directory and creates the required directory hierarchy below it for a standard Web application. This includes the WEB-INF directory that will contain the web.xml file. The build process also sets the build's CLASSPATH value to include the required JAR files in the tc Server distribution.

The compile target uses the Java compiler to compile the Java servlet file into a class and copies it to the output directory. The package target creates a JAR file of the output directory.