Ant & Tomcat Manager on virtual hosts

I’m kicking out a couple freelance projects using Struts and Hibernate in the next couple weeks, this weekend I was fiddling with the Ant build script and I remembered Tomcat Manager App, which allows you to reload contexts while Tomcat is running (as well as start, stop, install etc..). Unfortunately, the /manager application only sees applications within the virtual host it’s running in and because I’m working with a couple different applications, I have multiple virtual hosts and I need to be able to utilize the /manager application from each one.

Turns out it’s not all that hard to get the /manager application working within a different virtual host… as long as you read the documentation very thoroughly. It’s as simple as adding this:

<Context path="/manager" debug="5" docBase="${CATALINA_BASE}\server\webapps\manager" privileged="true" />

(where ${CATALINA_BASE} is the absolute path to your tomcat install) to the virtual host element you have setup in server.xml (for those not experienced with Tomcat, adding a ‘context’ is similar to adding a virtual directory in IIS, make any more sense?). The one gotcha (in bold) is the privileged attribute. It *must* be set to true to run the /manager or /admin applications in virtual host besides localhost.

Reloading the application using Ant is a breeze, the Tomcat documenation includes an example build file, in short all you need is this:

<property name="path" value="/myapp"/>
<property name="url" value="http://localhost:8080/manager"/>
<property name="username" value="myusername"/>
<property name="password" value="mypassword"/>
<taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask"/>
<target name="reload" >
<reload
  url="${url}"
  username="${username}" password="${password}"
  path="${path}"/>
</target>

Make sure that you have the catalina-ant.jar file coped from {Tomcat_install}\server\lib\ to your {Ant_install}\lib. If you are using Eclipse, go to Windows –> Preferences –> Ant –> –> Runtime –> Classpath and add catalina-ant.jar to the Runtime classpath.

This entry was posted in J2EE, Systems Administration. Bookmark the permalink.

3 Responses to Ant & Tomcat Manager on virtual hosts

  1. JustinMaccarthy says:

    Nice article Aaron.

    Just thought I’d point out some cool tools for Tomcat/ Hibernate / Ant I’ve come across.

    MiddleGen can take an existing DB schema and generate all the XML for mapping java to the DB and even the java itself.
    Get the middlegen plugin here http://boss.bekk.no/boss/middlegen/

    You will need to download the hibernate-tools.jar too.

    and see an example project with ANT build files etc here http://www.ociweb.com/jnb/jnbNov2003.html

    The O’Reilly Book – Java Extreme Programming Cookbook has some code to start / stop Tomcat from ANT using a custom task. Also one to check if its started in the first instance.

    The Code can be downloaded from the O’Reilly site
    http://www.oreilly.com/catalog/jextprockbk/ chapter 10

    Have you done any CFMX / Hibernate projects Aaron?

  2. Jon says:

    Any way to specify a different user/pass for each virtual host?

  3. Chris says:

    Thanks heaps! This is just what I was looking for.

    Just a hint to others who use it (which might be obvious ;) – you have to change the line:

    to

    in your ant file to tell ant to communicate with the virtual host’s manager and not just your plain old localhost’s manager.

    After I worked this out it was fine.

    While we’re on the subject of Hibernate and persistence Javelin also generates and manages Java code and ORM persistence meta-data – all from visual class diagrams. Improves productivity by about 75% if you’re into that sort of thing ;)

    http://stepaheadsoftware.com/products/javelin/javelin.htm

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>