Now it's time to create a task to delete our application from the server.

    <!-- ==========================================================
       
             uninstall
             uninstall (remove) the web application from the server
       
         ========================================================== -->
    <target name="uninstall" description="Uninstall the webapp" depends="init">
       
        <echo level="info">
    ***********************************************************************
    *
    * Attempting to uninstall ${server.url}${webapp.path}
    *
    ***********************************************************************
        </echo>

        <!-- this task fails if no application in present -->
        <trycatch property="uninstall.error">
          <try>
            <remove url="${server.manager.url}"
                    username="${server.manager.username}"
                    password="${server.manager.password}"
                    path="${webapp.path}" />
            <echo>Uninstallation succesfful</echo>
            <echo level="info">
        ***********************************************************************
        *
        * Pausing for ${server.manager.pause} for the server to reload pages
        *
        ***********************************************************************
            </echo>
            <sleep seconds="${server.manager.pause}"/>
          </try>
          <catch>
            <echo level="warning">Received error ${uninstall.error}</echo>
            <!-- do some other tasks here later to remedy -->
          </catch>
        </trycatch>
    </target>

That's it. Assuming that something is still deployed, try

> ant uninstall
uninstall:
     [echo]
     [echo]     ***********************************************************************
     [echo]     *
     [echo]     * Attempting to uninstall http://localhost:8080/JavaPersistence
     [echo]     *
     [echo]     ***********************************************************************
     [echo]
   [remove] OK - Undeployed application at context path /JavaPersistence
     [echo] Uninstallation succesfful
     [echo]
     [echo]             ***********************************************************************
     [echo]             *
     [echo]             * Pausing for 5 for the server to reload pages
     [echo]             *
     [echo]             ***********************************************************************
     [echo]

BUILD SUCCESSFUL
Total time: 6 seconds

Now, just to prove a point, run it again (with no application deployed).

uninstall:
     [echo]
     [echo]     ***********************************************************************
     [echo]     *
     [echo]     * Attempting to uninstall http://localhost:8080/JavaPersistence
     [echo]     *
     [echo]     ***********************************************************************
     [echo]
   [remove] FAIL - No context exists for path /JavaPersistence
 [trycatch] Caught exception: FAIL - No context exists for path /JavaPersistence
     [echo] Received error FAIL - No context exists for path /JavaPersistence

BUILD SUCCESSFUL
Total time: 1 second

Here, this is actually an okay "successful" since the goal is to make sure that no application exists.

We can also make the install task a bit better now since we can make it dependent on uninstall.

    <target name="install" [...] depends="war, uninstall">

Now try it out.

> ant install
   [deploy] OK - Deployed application at context path /JavaPersistence
     [echo] Installation succesfful

And with a application already deployed, run install again

>ant install
   [deploy] OK - Deployed application at context path /JavaPersistence
     [echo] Installation succesfful

I hope you can see the power of this now for deployment. Let's save our work.

> svn inc.build.minor

commit