|
8.6. Deploying to TomcatMore 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/http://ant.apache.org/" 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: file:////C:\ant\ch08\app/ 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: jar:file://///ant/ch08/app.war!/ 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: http://localhost:8080/manager/deploy?path=/deployment&war=file:////c:\ant\deploy\app/ 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&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
The attributes of the get task appear in Table 8-4.
|
|