Managing Applications with Ant

You can also use Ant to run the previous administration commands. This is convenient for development purposes, because an Ant build file could be used to compile, deploy, and even start a Web application. The steps for doing this once Ant is installed are as follows:

  1. Copy the CATALINA_HOME/server/lib/catalina-ant.jar file into Ant’s library directory (ANT_HOME/lib). This JAR file contains the Tomcat management task definitions for Ant.

  2. Add ANT_HOME/bin to your path.

  3. Add a user with the manager role to Tomcat’s user database if such a user doesn’t exist.

  4. Now add <taskdef> elements to your custom build.xml script that call the Tomcat manager commands.

Listing 6-5 shows a sample build.xml. You could use it to build and deploy a Web application. As it stands, it simply allows you to use all the previous commands with Ant. A developer would typically give you the Ant script for building the application.

Listing 6-5: A Sample build.xml File for Using the Manager Application

image from book
 <project name="ManagerApplication" default="list" basedir=".">    <!-- Configure the context path for this application -->    <property name="path"     value="/Catalog"/>    <property name="build"     value="C:/dev"/>    <property name="file"     value="Catalog.war"/> 
image from book

The <project> tag has attributes for the name of the project and the default target. The default target in this case is called list. Running Ant with no options will invoke the tasks associated with this default target. The basedir attribute is the base directory for all path calculations in the Ant build script. This is set to . (the current directory), and therefore all the paths are taken to be relative to the directory from which you run Ant. You then define properties for the build (in this case the path of the Web application and the location of your Web application files).

The properties in Listing 6-6 specify the access URL and username/password for the manager application. At the end of this section you’ll see how you can also pass the password from the command line.

Listing 6-6: Defining the Properties for the Manager Application

image from book
 <!-- Configure properties to access the Manager application -->  <property name="url"      value="http://localhost:8080/manager"/>  <property name="username" value="tomcat"/>  <property name="password" value="tomcat"/> 
image from book

Listing 6-7 specifies the task definitions for the manager application. Ant allows for custom tasks that extend its functionality. Tomcat implements the custom tasks shown in Listing 6-7 for executing the manager application commands. For example, org.apache.catalina.ant.DeployTask executes the deploy command against the manager application.

Listing 6-7: Ant Task Definitions for Using the Manager Application

image from book
 <!-- Configure the custom Ant tasks for the Manager application -->  <taskdef name="deploy"           classname="org.apache.catalina.ant.DeployTask"/>  <taskdef name="list"           classname="org.apache.catalina.ant.ListTask"/>  <taskdef name="reload"           classname="org.apache.catalina.ant.ReloadTask"/>  <taskdef name="resources"           classname="org.apache.catalina.ant.ResourcesTask"/>  <taskdef name="roles"           classname="org.apache.catalina.ant.RolesTask"/>  <taskdef name="start"           classname="org.apache.catalina.ant.StartTask"/>  <taskdef name="stop"           classname="org.apache.catalina.ant.StopTask"/>  <taskdef name="undeploy"           classname="org.apache.catalina.ant.UndeployTask"/> 
image from book

Next, Listing 6-8 shows the manager tasks for listing all Web applications and deploying/ undeploying Web applications.

Listing 6-8: The Manager Command Tasks

image from book
   <target name="deploy" description="Deploy web application">      <deploy url="${url}" username="${username}" password="${password}"              path="${path}" war="file:${build}/${file}"/>    </target>    <target name="list" description="List all web applications">      <list url="${url}" username="${username}" password="${password}"/>    </target>    <target name="reload" description="Reload web application">      <reload url="${url}" username="${username}" password="${password}"              path="${path}"/>    </target>    <target name="resources" description="List all JNDI resources">      <resources url="${url}" username="${username}" password="${password}"/>    </target>    <target name="roles" description="List all roles">      <roles url="${url}" username="${username}" password="${password}"/>    </target>    <target name="start" description="Start web application">      <start url="${url}" username="${username}" password="${password}"             path="${path}"/>    </target>    <target name="stop" description="Stop web application">      <stop url="${url}" username="${username}" password="${password}"            path="${path}"/>    </target>    <target name="undeploy" description="Undeploy web application">      <undeploy url="${url}" username="${username}" password="${password}"                path="${path}"/>    </target>  </project> 
image from book

The password property in the previous Ant script contains the password for the user with manager privileges. This is useful for development environments where you don’t want to specify the password each time you build and deploy.

You can override this value from the command line, or even omit it from the build file altogether and pass it only from the command line. This avoids the security risk of putting the password in a text file. The following will stop the tomcatBook Web application using a username and password at the command line:

 ant -Dpassword=tomcat -Dusername=tomcat -Dpath=/tomcatBook stop 



Pro Jakarta Tomcat 5
Pro Apache Tomcat 5/5.5 (Experts Voice in Java)
ISBN: 1590593316
EAN: 2147483647
Year: 2004
Pages: 94

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net