Java Servlet & JSP Cookbook
Problem
You want to start a web application on Tomcat using an Ant file. Solution
Use the Tomcat-supplied StartTask task so that Ant can manage Tomcat. Discussion
The Tomcat servlet and JSP container includes a built-in web application called "Manager" that you can use to start, stop, deploy, and initiate other administrative tasks with web applications. Tomcat makes this application available from the /manager context path . Tomcat Version 4 (and later) includes Java classes that allow developers to use the Manager application from their Ant build files. The advantage of using the Manager application from Ant is that you do not have to configure the conf/server.xml file to make the web application dynamically reloadable (see Recipe 2.2). In addition, you can start or stop a single web application without disrupting other Tomcat applications.
Take these steps to start Tomcat from Ant:
Example 4-8 shows the taskdef element that defines the start task, followed by the target that starts the specified Tomcat application. Example 4-8. Starting Tomcat using an Ant file
<project name="My Project" default="start-tomcat" basedir="."> <taskdef name="start" classname="org.apache.catalina.ant.StartTask" /> <!-- import properties specifying username, password, url, and context-path --> <property file="global.properties" /> <target name="start-tomcat" description="Starts the Web application"> <echo message="Starting the default application ${ context-path}..."/> <start url="${url}" username="${username}" password="${password}" path="/${context-path}" /> </target> </project> The start task has four attributes that Example 4-8 sets using a global.properties file. This is a text file containing four name/value pairs, which are imported into the Ant file using the property task: <property file="global.properties" /> The global.properties file is located in the same directory as the Ant build file. Here are the contents of the global.properties file: url=http://localhost:8080/manager username=bruce password=bruce1957 context-path=home The url property specifies the Tomcat Manager URL, the username and password identify the user who is mapped in the Tomcat user database to the manager role, the context-path property specifies the context path of the web application you are starting, and the Ant file itself specifies the opening slash ( /) character for the context path.
Launch this Ant file by changing to its directory at the command line and typing ant or ant -buildfile buildfile-name . Here is the command-line output: H:\book\cookbook\code\chap4> ant -buildfile start.xml Buildfile: start.xml start-tomcat: [echo] Starting the default application home... [start] OK - Started application at context path /home BUILD SUCCESSFUL Total time: 4 seconds If an application is stopped , it is unavailable to web users (see Recipe 4.8). When the application is started again, it can receive requests normally.
See Also
The Tomcat Manager application description: http://jakarta.apache.org/tomcat/tomcat-4.1-doc/manager-howto.html; Recipe 4.1 on downloading and setting up Ant; Recipe 4.2 on writing Ant targets; Recipe 4.3 on creating a classpath for an Ant file; Recipe 4.4 on compiling a servlet with Ant; Recipe 4.5 and Recipe 4.6 on creating WAR and JAR files; Recipe 4.8 on stopping Tomcat with Ant; Recipe 2.1 and Recipe 2.6 on deploying web applications using Ant; the Ant manual section on the property task: http://ant.apache.org/manual/CoreTasks/property.html; the Ant manual segment on targets : http://ant.apache.org/manual/using.html#targets; the Apache Ant manual index page: http://ant.apache.org/manual/index.html; the Apache Ant Project: http://ant.apache.org. |