build.xml

Notes

Purpose
build.xml defines project properties used by Ant to control the building, installing, removing, deploying and undeploying of a project in the Tomcat Servlet/JSP container.

This code defines the context path, the document base, the debug level. the reloadable flag, and the cross context flag:

    <project name="Web Application" default="build" basedir=".">

        ... code to process build.properties files goes here ...

        ... code to define the Tomcat manager tasks goes here ...

        ... code to set up the CLASSPATH goes here ...

        ... code to define the targets goes here ...

    </project>


Process build.properties Files
Both your home directory and the application directory are checked for build.properties files:

    <property file="${user.home}/build.properties"/>
    <property file="build.properties"/>

See build.properties for a description of the build.properties file syntax and its complete annotated source code.


Define the Tomcat Manager Tasks
The Tomcat manager tasks from catalina-ant.jar in Ant's lib directory are set up ready for use in various target definitions:

    <taskdef name="deploy"    classname="org.apache.catalina.ant.DeployTask"/>
    <taskdef name="install"   classname="org.apache.catalina.ant.InstallTask"/>
    <taskdef name="list"      classname="org.apache.catalina.ant.ListTask"/>
    <taskdef name="reload"    classname="org.apache.catalina.ant.ReloadTask"/>
    <taskdef name="remove"    classname="org.apache.catalina.ant.RemoveTask"/>
    <taskdef name="resources" classname="org.apache.catalina.ant.ResourcesTask"/>
    <taskdef name="roles"     classname="org.apache.catalina.ant.RolesTask"/>
    <taskdef name="start"     classname="org.apache.catalina.ant.StartTask"/>
    <taskdef name="stop"      classname="org.apache.catalina.ant.StopTask"/>
    <taskdef name="undeploy"  classname="org.apache.catalina.ant.UndeployTask"/>


Set up the CLASSPATH
The CLASSPATH for compiling the Java classes is set to all the jar files in Tomcat's common/lib and shared/lib directories:

    <path id="classpath">
        <fileset dir="${tomcat.home}/common/lib">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="${tomcat.home}/shared/lib">
            <include name="*.jar"/>
        </fileset>
    </path>


The Targets

ant init
Initialises the time stamp:

    <target name="init">
        <tstamp/>
    </target>

ant clean
Deletes the Web Application's war directory and web archive file:

    <target name="clean" description="Deletes the Web Application's war directory and web archive file">
        <echo message="Deleting ${app.name}'s war directory and web archive file ..."/>
        <delete dir="${basedir}/war"/>
        <delete file="${basedir}/${app.name}.war"/>
    </target>

ant prepare
Creates the Web Application's war directory:

    <target name="prepare" description="Creates the Web Application's war directory" depends="init">
        <echo message="Creating ${app.name}'s war directory ..."/>
        <mkdir dir="${basedir}/war"/>
        <mkdir dir="${basedir}/war/META-INF"/>
        <mkdir dir="${basedir}/war/WEB-INF"/>
        <mkdir dir="${basedir}/war/WEB-INF/classes"/>
        <mkdir dir="${basedir}/war/WEB-INF/lib"/>
    </target>

ant build
Builds the Web Application by copying any html, jsp, txt, zip, sit, gif, jpg and png files into the war directory, copying the context.xml file into the war/META-INF directory, copying the web.xml file into the war/WEB-INF directory, and compiling any java files into the war/WEB-INF/classes directory:

    <target name="build" description="Builds the Web Application" depends="prepare">
        <echo message="Building ${app.name} ..."/>
        <copy todir="${basedir}/war">
            <fileset dir="${basedir}/web">
                <include name="**/*.html"/>
                <include name="**/*.jsp"/>
                <include name="**/*.txt"/>
                <include name="**/*.zip"/>
                <include name="**/*.sit"/>
                <include name="**/*.gif"/>
                <include name="**/*.jpg"/>
                <include name="**/*.png"/>
            </fileset>
        </copy>
        <copy todir="${basedir}/war/META-INF">
            <fileset dir=".">
                <include name="context.xml" />
            </fileset>
        </copy>
        <copy todir="${basedir}/war/WEB-INF">
            <fileset dir="${basedir}/web/WEB-INF">
                <include name="web.xml"/>
            </fileset>
        </copy>
        <javac srcdir="${basedir}/src" destdir="${basedir}/war/WEB-INF/classes">
            <include name="**/*.java"/>
            <classpath refid="classpath"/>
        </javac>
   </target>

ant package
Packages the web application's web archive file from the war directory:

    <target name="package" description="Packages the Web Application's web archive file" depends="build">
        <echo message="Packaging ${app.name}'s web archive file ..."/>
        <delete file="${basedir}/${app.name}.war"/>
        <jar jarfile="${basedir}/${app.name}.war">
            <fileset dir="${basedir}/war" includes="**"/>
        </jar>
    </target>

ant install
Installs the web application into the Tomcat Servlet/JSP container:

    <target name="install" description="Installs the Web Application" depends="package">
        <echo message="Installing ${app.name} ..."/>
        <install url="${manager.url}" username="${username}" password="${password}" path="/${app.name}"
            config="file:${basedir}/war/META-INF/context.xml" war="file:${basedir}/${app.name}.war"/>
    </target>

ant reload
Reloads the web application into the Tomcat Servlet/JSP container:

    <target name="reload" description="Reloads the Web Application" depends="package">
        <echo message="Reloading ${app.name} ..."/>
        <reload url="${manager.url}" username="${username}" password="${password}" path="/${app.name}"/>
    </target>

ant remove
Removes the web wpplication from the Tomcat Servlet/JSP container:

    <target name="remove" description="Removes the Web Application">
        <echo message="Removing ${app.name} ..."/>
        <remove url="${manager.url}" username="${username}" password="${password}" path="/${app.name}"/>
    </target>

ant deploy
Deploys the web application into the Tomcat Servlet/JSP container:

    <target name="deploy" description="Deploys the Web Application" depends="package">
        <echo message="Deploying ${app.name} ..."/>
        <deploy url="${manager.url}" username="${username}" password="${password}" path="/${app.name}"
            war="file:${basedir}/${app.name}.war"/>
    </target>

ant undeploy
Undeploys the web application from the Tomcat Servlet/JSP container:

    <target name="undeploy" description="Undeploys the Web Application">
        <echo message="Undeploying ${app.name} ..."/>
        <undeploy url="${manager.url}" username="${username}" password="${password}" path="/${app.name}"/>
    </target>

ant start
Starts the web application in the Tomcat Servlet/JSP container:

    <target name="start" description="Start the Web Application">
        <echo message="Start the Web Application ..."/>
        <start url="${manager.url}" username="${username}" password="${password}" path="/${app.name}"/>
    </target>

ant stop
Stops the web application in the Tomcat Servlet/JSP container:

    <target name="stop" description="Stop the Web Application">
        <echo message="Stop the Web Application ..."/>
        <stop url="${manager.url}" username="${username}" password="${password}" path="/${app.name}"/>
    </target>

ant list
Lists installed and deployed web applications in the Tomcat Servlet/JSP container:

    <target name="list" description="Lists Installed and Deployed Web Applications">
        <echo message="Listing Installed and Deployed Web Applications ..."/>
        <list url="${manager.url}" username="${username}" password="${password}"/>
    </target>

ant resources
Lists Tomcat Servlet/JSP container global resources of all types:

    <target name="resources" description="Lists Tomcat Global Resources of All Types">
        <echo message="Listing Tomcat Global Resources of All Types ..."/>
        <resources url="${manager.url}" username="${username}" password="${password}"/>
    </target>

ant roles
Lists Tomcat Servlet/JSP container security roles:

    <target name="roles" description="Lists Tomcat Security Roles">
        <echo message="Listing Tomcat Security Roles ..."/>
        <roles url="${manager.url}" username="${username}" password="${password}"/>
    </target>

ant debug
Lists the build.xmlproperties for debugging purposes:

    <target name="debug">
        <echo message="Lists the properties for debugging purposes ..."/>
        <echo message="app.name    = ${app.name}"/>
        <echo message="basedir     = ${basedir}"/>
        <echo message="user.home   = ${user.home}"/>
        <echo message="tomcat.home = ${tomcat.home}"/>
        <echo message="manager.url = ${manager.url}"/>
        <echo message="username    = ${username}"/>
   <!-- <echo message="password    = ${password}"/> commented out for security reasons -->
   </target>


Complete Source Code


<project name="Web Application" default="build" basedir=".">

    <property file="${user.home}/build.properties"/>
    <property file="build.properties"/>

    <taskdef name="deploy"    classname="org.apache.catalina.ant.DeployTask"/>
    <taskdef name="install"   classname="org.apache.catalina.ant.InstallTask"/>
    <taskdef name="list"      classname="org.apache.catalina.ant.ListTask"/>
    <taskdef name="reload"    classname="org.apache.catalina.ant.ReloadTask"/>
    <taskdef name="remove"    classname="org.apache.catalina.ant.RemoveTask"/>
    <taskdef name="resources" classname="org.apache.catalina.ant.ResourcesTask"/>
    <taskdef name="roles"     classname="org.apache.catalina.ant.RolesTask"/>
    <taskdef name="start"     classname="org.apache.catalina.ant.StartTask"/>
    <taskdef name="stop"      classname="org.apache.catalina.ant.StopTask"/>
    <taskdef name="undeploy"  classname="org.apache.catalina.ant.UndeployTask"/>

    <path id="classpath">
        <fileset dir="${tomcat.home}/common/lib">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="${tomcat.home}/shared/lib">
            <include name="*.jar"/>
        </fileset>
    </path>

    <target name="init">
        <tstamp/>
    </target>

    <target name="clean" description="Deletes the Web Application's war directory and web archive file">
        <echo message="Deleting ${app.name}'s war directory and web archive file ..."/>
        <delete dir="${basedir}/war"/>
        <delete file="${basedir}/${app.name}.war"/>
    </target>

    <target name="prepare" description="Creates the Web Application's war directory" depends="init">
        <echo message="Creating ${app.name}'s war directory ..."/>
        <mkdir dir="${basedir}/war"/>
        <mkdir dir="${basedir}/war/WEB-INF"/>
        <mkdir dir="${basedir}/war/WEB-INF/classes"/>
        <mkdir dir="${basedir}/war/WEB-INF/lib"/>
    </target>

    <target name="build" description="Builds the Web Application" depends="prepare">
        <echo message="Building ${app.name} ..."/>
        <javac srcdir="${basedir}/src" destdir="${basedir}/war/WEB-INF/classes">
            <include name="**/*.java"/>
            <classpath refid="classpath"/>
        </javac>
        <copy todir="${basedir}/war/WEB-INF">
            <fileset dir="${basedir}/web/WEB-INF">
                <include name="web.xml"/>
            </fileset>
        </copy>
        <copy todir="${basedir}/war">
            <fileset dir="${basedir}/web">
                <include name="**/*.html"/>
                <include name="**/*.jsp"/>
                <include name="**/*.txt"/>
                <include name="**/*.zip"/>
                <include name="**/*.gif"/>
                <include name="**/*.jpg"/>
                <include name="**/*.png"/>
            </fileset>
        </copy>
        <copy todir="${basedir}/war/META-INF">
            <fileset dir=".">
                <include name="context.xml" />
            </fileset>
        </copy>
    </target>

    <target name="package" description="Packages the Web Application's web archive file" depends="build">
        <echo message="Packaging ${app.name}'s web archive file ..."/>
        <delete file="${basedir}/${app.name}.war"/>
        <jar jarfile="${basedir}/${app.name}.war">
            <fileset dir="${basedir}/war" includes="**"/>
        </jar>
    </target>

    <target name="install" description="Installs the Web Application" depends="package">
        <echo message="Installing ${app.name} ..."/>
        <install url="${manager.url}" username="${username}" password="${password}" path="/${app.name}"
            config="file:${basedir}/war/META-INF/context.xml" war="file:${basedir}/${app.name}.war"/>
    </target>

    <target name="reload" description="Reloads the Web Application" depends="package">
        <echo message="Reloading ${app.name} ..."/>
        <reload url="${manager.url}" username="${username}" password="${password}" path="/${app.name}"/>
    </target>

    <target name="remove" description="Removes the Web Application">
        <echo message="Removing ${app.name} ..."/>
        <remove url="${manager.url}" username="${username}" password="${password}" path="/${app.name}"/>
    </target>

    <target name="deploy" description="Deploys the Web Application" depends="package">
        <echo message="Deploying ${app.name} ..."/>
        <deploy url="${manager.url}" username="${username}" password="${password}" path="/${app.name}"
            war="file:${basedir}/${app.name}.war"/>
    </target>

    <target name="undeploy" description="Undeploys the Web Application">
        <echo message="Undeploying ${app.name} ..."/>
        <undeploy url="${manager.url}" username="${username}" password="${password}" path="/${app.name}"/>
    </target>

    <target name="start" description="Start the Web Application">
        <echo message="Start the Web Application ..."/>
        <start url="${manager.url}" username="${username}" password="${password}" path="/${app.name}"/>
    </target>

    <target name="stop" description="Stop the Web Application">
        <echo message="Stop the Web Application ..."/>
        <stop url="${manager.url}" username="${username}" password="${password}" path="/${app.name}"/>
    </target>

    <target name="list" description="Lists Installed and Deployed Web Applications">
        <echo message="Listing Installed and Deployed Web Applications ..."/>
        <list url="${manager.url}" username="${username}" password="${password}"/>
    </target>

    <target name="resources" description="Lists Tomcat Global Resources of All Types">
        <echo message="Listing Tomcat Global Resources of All Types ..."/>
        <resources url="${manager.url}" username="${username}" password="${password}"/>
    </target>

    <target name="roles" description="Lists Tomcat Security Roles">
        <echo message="Listing Tomcat Security Roles ..."/>
        <roles url="${manager.url}" username="${username}" password="${password}"/>
    </target>

    <target name="debug">
        <echo message="Lists the properties for debugging purposes ..."/>
        <echo message="app.name    = ${app.name}"/>
        <echo message="basedir     = ${basedir}"/>
        <echo message="user.home   = ${user.home}"/>
        <echo message="tomcat.home = ${tomcat.home}"/>
        <echo message="manager.url = ${manager.url}"/>
        <echo message="username    = ${username}"/>
   <!-- <echo message="password    = ${password}"/> commented out for security reasons -->
   </target>

</project>