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.
- 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 inTOMCAT_HOME/conf/tomcat-users.xml
. For example:<tomcat-users>
<user name="admin" password="s3cr£t" roles="manager-gui,manager-script"/>
</tomcat-users> - 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:- Copy the JARs into the
ANT_HOME/lib
folder. Then Ant will just find them. - 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:
Where<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>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. - Access the JARs directly from your Tomcat 7 installation. Ant then needs a path id to find them:
Where<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>appserver.lib
is the path to Tomcat 7’s lib directory andappserver.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. - Copy the JARs into the
- 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"/> - 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
andTomcat.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.
Thestop-webapp
task has thefailonerror
attribute set tofalse
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.
Thank you very much ! That was exactly what I needed.
ReplyDeleteThanks, very helpful when updating my tomcat to 7.0.26 today.
ReplyDeletePerfect! You might consider linking to stackoverflow:
ReplyDeletehttp://stackoverflow.com/questions/7251098/ant-tomcat-build-error-java-lang-noclassdeffounderror-org-apache-tomcat-util
Thanks a ton.
ReplyDeletecheers
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.
ReplyDeleteThanks very much for posting this. But, i am getting an error during undeploy task:
ReplyDelete*********************************************************************************************
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.
*********************************************************************************************
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.
ReplyDeleteThank you! That was most helpful.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteGreat post. I had suffered a long time before reading your article.
ReplyDeleteI'm very thankful, it was so painful until I found this.
ReplyDeleteThanks a lot really helpful blog
ReplyDeleteGood one, Helped me to resolve tomcat 7 issue with Ant.
ReplyDeleteThank you, Paul. This is just what I needed to know. You are awesome!
ReplyDeleteThank you very much...! helpful blog...!
ReplyDeleteThank you! Saved me hours.
ReplyDeleteThank you! Very helpful post...
ReplyDeleteThanks a lot!!! You saved my time :)
ReplyDeleteHi, Thanks for the post. Linked your answer to this solution on Stackoverflow site. http://stackoverflow.com/a/12122495/1793718
ReplyDeleteWhen i am diploying it shows an error like this.. "java.io.IOException: insufficient data written" what should i do..?
ReplyDeletevery thanks for your GREAT solution!
ReplyDeleteThanks a LOT!!
ReplyDelete