ProblemYou want to use Ant to stop a specific Tomcat web application. SolutionDefine a task in the Ant file using a taskdef element and the Java class org.apache.catalina.ant.StopTask . DiscussionDuring development, you might need to stop a Tomcat web application so that you can add new servlets or deployment-descriptor entries, and then restart the application, allowing the changes to take effect. In the absence of a conf/server.xml configuration to make the application dynamically reloadable (see Recipe 2.2), you can use an Ant target to stop a particular web application without disrupting the other running web applications. This is the opposite of starting an application (Recipe 4.7); the application is taken out of service until you start it again. The org.apache.catalina.ant.StopTask class provides a connection between Ant and the Tomcat Manager application. Manager is a built-in web application (at context path /manager ) that you can use to administer other Tomcat web applications. Implement the same four steps discussed in Recipe 4.7 to use this stop task:
Example 4-9. Using Ant to stop a web application <project name="My Project" default="stop-tomcat" basedir="."> <taskdef name="stop" classname="org.apache.catalina.ant.StopTask" /> <!-- import properties specifying username, password, url, and context-path --> <property file="global.properties" /> <target name="stop-tomcat" description="Stops the Web application"> <echo message="Stopping the application ${context-path}..."/> <stop url="${url}" username="${username}" password="${password}" path="/${context-path}" /> </target> </project> The taskdef defines a task for this build file called stop . The defined task is then used in the build file: <stop url="${url}" username="${username}" password="${password}" path="/${context-path}" /> Example 4-9 gets its property values from a property task that imports global.properties (the property file is located in the same directory as the Ant build file). The properties represent:
See AlsoThe 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.7 on starting 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. |