Skip to main content

Catalina-Ant for Tomcat 7

I recently upgraded from Tomcat 6 to Tomcat 7 and all of my Ant deployment scripts stopped working. I eventually worked out why and made the necessary changes, but there doesn’t seem to be a complete description of how to use Catalina-Ant for Tomcat 7 on the web so I thought I'd write one.
  1. To start with, make sure Tomcat manager is configured for use by Catalina-Ant. Make sure that manager-script is included in the roles for one of the users in TOMCAT_HOME/conf/tomcat-users.xml. For example:
    <tomcat-users>
    <user name="admin" password="s3cr£t" roles="manager-gui,manager-script"/>
    </tomcat-users>
  2. Catalina-Ant for Tomcat 6 was encapsulated within a single JAR file. Catalina-Ant for Tomcat 7 requires four JAR files. One from TOMCAT_HOME/bin:

    tomcat-juli.jar

    and three from TOMCAT_HOME/lib:

    catalina-ant.jar
    tomcat-coyote.jar
    tomcat-util.jar


    There are at least three ways of making the JARs available to Ant:

    1. Copy the JARs into the ANT_HOME/lib folder. Then Ant will just find them.
    2. Copy the JARs to a folder within your project that you check into your source control system. Ant then needs a path id to find them:
      <path id="catalina-ant-classpath">
      <fileset dir="${catalina-ant-dir}">
      <include name="catalina-ant.jar"/>
      <include name="tomcat-coyote.jar"/>
      <include name="tomcat-util.jar"/>
      <include name="tomcat-juli.jar"/>
      </fileset>
      </path>
      Where catalina-ant-dir is the directory with the JARs in. This way you don’t need to modify the Ant installation on every machine you build on.
    3. Access the JARs directly from your Tomcat 7 installation. Ant then needs a path id to find them:
      <path id="catalina-ant-classpath">
      <fileset dir="${appserver.lib}">
      <include name="catalina-ant.jar"/>
      <include name="tomcat-coyote.jar"/>
      <include name="tomcat-util.jar"/>
      </fileset>
      <fileset dir="${appserver.home}/bin">
      <include name="tomcat-juli.jar"/>
      </fileset>
      </path>
      Where appserver.lib is the path to Tomcat 7’s lib directory and appserver.home is the path to Tomcat’s top level installed directory. This way Tomcat 7 is required on every box you build on.

    My personal preference is for 2 above.

  3. Now that your Ant script can see the Catalina-Ant JARs you need to tell it what tasks are available. These are most if not all of the tasks that are available to Ant.
    <taskdef name="catalina-deploy" classname="org.apache.catalina.ant.DeployTask" classpathref="catalina-ant-classpath"/>
    <taskdef name="catalina-list" classname="org.apache.catalina.ant.ListTask" classpathref="catalina-ant-classpath"/>
    <taskdef name="catalina-reload" classname="org.apache.catalina.ant.ReloadTask" classpathref="catalina-ant-classpath"/>
    <taskdef name="catalina-findleaks" classname="org.apache.catalina.ant.FindLeaksTask" classpathref="catalina-ant-classpath"/>
    <taskdef name="catalina-resources" classname="org.apache.catalina.ant.ResourcesTask" classpathref="catalina-ant-classpath"/>
    <taskdef name="catalina-start" classname="org.apache.catalina.ant.StartTask" classpathref="catalina-ant-classpath"/>
    <taskdef name="catalina-stop" classname="org.apache.catalina.ant.StopTask" classpathref="catalina-ant-classpath"/>
    <taskdef name="catalina-undeploy" classname="org.apache.catalina.ant.UndeployTask" classpathref="catalina-ant-classpath"/>
  4. Finally you need a set of tasks that actually do the work. Although, as you can see above, there are a few tasks I only tend to use the following ones:
    <target name = "stop-webapp">
    <catalina-stop url="${tomcat.manager.url}"
    username="${tomcat.username}"
    password="${tomcat.password}"
    path="/${webapp.name}"
    failonerror="false"/>
    </target>

    <target name = "start-webapp">
    <catalina-start url="${tomcat.manager.url}"
    username="${tomcat.username}"
    password="${tomcat.password}"
    path="/${webapp.name}"/>
    </target>

    <target name = "undeploy-webapp">
    <catalina-undeploy url="${tomcat.manager.url}"
    username="${tomcat.username}"
    password="${tomcat.password}"
    path="/${webapp.name}"
    failonerror="false"/>
    </target>

    <target name = "deploy-webapp">
    <catalina-deploy url="${tomcat.manager.url}"
    username="${tomcat.username}"
    password="${tomcat.password}"
    path="/${webapp.name}"
    war="file:${war.file}"/>
    </target>
    tomcat.manager.url is the URL where Tomcat manager lives. This is another of the changes from Tomcat 6 to Tomcat 7. Usually this will be: http://:8080/manager/text.

    Tomcat.username and Tomcat.password are the user name and password for Tomcat manager.

    webapp.name is the name of the Tomcat application that you are deploying.

    war.file is the path the Tomcat application you are deploying’s WAR file.

    The stop-webapp task has the failonerror attribute set to false as on most occasions you don’t want the Ant build to stop if a Tomcat application you’re trying to stop isn’t running.

    I usually define all of these properties in a properties file that Ant can read. That way local settings can be picked up more easily if builds are run on different machines.

Comments

  1. Thank you very much ! That was exactly what I needed.

    ReplyDelete
  2. Thanks, very helpful when updating my tomcat to 7.0.26 today.

    ReplyDelete
  3. Perfect! You might consider linking to stackoverflow:

    http://stackoverflow.com/questions/7251098/ant-tomcat-build-error-java-lang-noclassdeffounderror-org-apache-tomcat-util

    ReplyDelete
  4. Thanks a ton.

    cheers

    ReplyDelete
  5. Thanks very much for posting. I'd been struggling with the Tomcat documentation trying to get this to work. Then I found this article and 5 mins later I was up and running.

    ReplyDelete
  6. Thanks very much for posting this. But, i am getting an error during undeploy task:

    *********************************************************************************************
    undeploy-war:
    [undeploy] FAIL - Unable to delete [C:\apache-tomcat-7.0.29\webapps\abc-webapp]. The
    continued presence of this file may cause problems.
    *********************************************************************************************

    ReplyDelete
  7. That's a common error. Often tomcat holds on to a JAR. You can google for ways to encourage it not to or put the offending JARs in the common Tomcat lib folder and not in your application.

    ReplyDelete
  8. Thank you! That was most helpful.

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. Great post. I had suffered a long time before reading your article.

    ReplyDelete
  11. I'm very thankful, it was so painful until I found this.

    ReplyDelete
  12. Thanks a lot really helpful blog

    ReplyDelete
  13. Good one, Helped me to resolve tomcat 7 issue with Ant.

    ReplyDelete
  14. Thank you, Paul. This is just what I needed to know. You are awesome!

    ReplyDelete
  15. Thank you very much...! helpful blog...!

    ReplyDelete
  16. Thank you! Saved me hours.

    ReplyDelete
  17. Thank you! Very helpful post...

    ReplyDelete
  18. Thanks a lot!!! You saved my time :)

    ReplyDelete
  19. Hi, Thanks for the post. Linked your answer to this solution on Stackoverflow site. http://stackoverflow.com/a/12122495/1793718

    ReplyDelete
  20. When i am diploying it shows an error like this.. "java.io.IOException: insufficient data written" what should i do..?

    ReplyDelete
  21. very thanks for your GREAT solution!

    ReplyDelete

Post a Comment

Popular posts from this blog

Write Your Own Load Balancer: A worked Example

I was out walking with a techie friend of mine I’d not seen for a while and he asked me if I’d written anything recently. I hadn’t, other than an article on data sharing a few months before and I realised I was missing it. Well, not the writing itself, but the end result. In the last few weeks, another friend of mine, John Cricket , has been setting weekly code challenges via linkedin and his new website, https://codingchallenges.fyi/ . They were all quite interesting, but one in particular on writing load balancers appealed, so I thought I’d kill two birds with one stone and write up a worked example. You’ll find my worked example below. The challenge itself is italics and voice is that of John Crickets. The Coding Challenge https://codingchallenges.fyi/challenges/challenge-load-balancer/ Write Your Own Load Balancer This challenge is to build your own application layer load balancer. A load balancer sits in front of a group of servers and routes client requests across all of the serv...

RESTful Behaviour Guide

I’ve used a lot of existing Representational State Transfer (REST) APIs and have created several of my own. I see a lot of inconsistency, not just between REST APIs but often within a single REST API. I think most developers understand, at a high level, what a REST API is for and how it should work, but lack a detailed understanding. I think the first thing they forget to consider is that REST APIs allow you to identify and manipulate resources on the web. Here I want to look briefly at what a REST API is and offer some advice on how to structure one, how it should behave and what should be considered when building it. I know this isn’t emacs vs vi, but it can be quite contentious. So, as  Barbossa from Pirates of the Caribbean said, this “...is more what you’d call ‘guidelines’ than actual rules.” Resources & Identifiers In their book, Rest in Practice - Hypermedia and Systems Architecture (‎ISBN: 978-0596805821), Jim Webber, Savas Parastatidis and Ian Robinson describe resour...