Section 8.6. Deploying to Tomcat

8.6. Deploying to Tomcat

More often than not, you won't be developing your applications on the same server you want to deploy to. You can deploy to a Tomcat installation running on a remote server by contacting the Tomcat manager servlet via URL in a browser. To do that in Ant you use the get task, which gets a file when you pass it a URL. If you're using Java 1.4 or later, this task supports any URL schema, including http:, ftp:, jar:, and https:. This task is great if you want to download online content, or, as in this case, issue commands via URLs to online code.

Before getting to deployment, here's an example that uses get to retrieve the Ant home page and stores it in ant.html:

<get src="/books/1/253/1/html/2/" dest="ant.html"/>

You can upload Web applications to Tomcat using the manager servlet, passing the local location of the Web application to upload. For example, to upload a Web application from C:/ant/ch08/app in Windows, you'd use this location:


To upload a .war file, you add an ! at the end of the location to indicate you're uploading a file, not the contents of a directory, like this in Unix:


Example 8-3 shows how this works in practice. In this case, the build file deploys a Web application from C:\ant\deploy\app that consists of a servlet (org.antbook.ch08.Deploy) that displays the message "Project Deployment!" to Tomcat. Here's the URL you pass to the get task to tell the Tomcat manager servlet what you want to do:


You can see this at work in Example 8-4.

Example 8-4. Deploying with get (ch08/get/build.xml)
<?xml version="1.0" encoding="UTF-8" ?> <project default="main" basedir=".">      <property name="tomcat.port" value="8080" />    <property name="tomcat.username" value="admin" />    <property name="tomcat.password" value="password" />    <target name="main" >     <get src="/books/1/253/1/html/2/http://localhost:8080/manager/deploy?path=/deployment&amp;war=file:////c: \ant\deploy\app/"       dest="deploy.txt"       username="${tomcat.username}"       password="${tomcat.password}" />   </target>      </project>

Here's what the build file looks like in action:

%ant Buildfile: build.xml main:       [get] Getting: http://localhost:8080/manager/deploy?path=/deployment&war= file:////c:\ant\ch08\get\app/ BUILD SUCCESSFUL Total time: 1 second

Here's what Tomcat wrote to the output file, deploy.txt:

OK - Deployed application at context path /deployment

After deployment, navigating to http://localhost:8080/deployment/org.antbook.ch08.Deploy shows the deployed servlet, as seen in Figure 8-1.

Figure 8-1. Deploying to Tomcat

For more information on using the Tomcat manager servlet, look at manager/html-manager-howto.html in your Tomcat installation.

The attributes of the get task appear in Table 8-4.

Table 8-4. The get task's attributes






Specifies the file where you want to store the retrieved data.




Specifies you want to only log errors instead of treating them as fatal.




Specifies the password to use when connecting.

If username is set.



Specifies the URL where the data you want is.




Specifies the username for BASIC http authentication.

If password is set.



Specifies you want to download a file only after checking the timestamp of the local copy to ensure you don't overwrite a more recent version.




Specifies you want to see full information as this task executes. Set to true/false.



When the verbose option is on, this task will display a dot (.) for every 100 KB received.

    Ant. The Definitive Guide
    Ant: The Definitive Guide, 2nd Edition
    ISBN: 0596006098
    EAN: 2147483647
    Year: 2003
    Pages: 115
    Authors: Steve Holzner

    Similar book on Amazon © 2008-2017.
    If you may any questions please contact us: