Saturday, 17 March 2012

Fixing the "DeployTask cannot be found" error when using the Tomcat 7 Ant tasks on Mac OSX.

So you've got an Ant build.xml script for your Java web application and it works just fine compiling, testing and building your WAR or EAR file. And you manage to ignore your peers who whine on about that Maven thing.

You deploy your application to Tomcat 7, but you do that manually and that's getting boring now so you want to take that last step and script your deploy as well. You kind of like the one click done feel of it, its almost continuous deployment and that is what all the cool kids are into these days so you want some of that action yourself. You're only human.

So you went and Google'd and came across the Tomcat Ant task documentation and yea verily it looked good.

So now in your build.xml file you have something like this (I left out a bunch of other Tomcat commands, dont worry if you have those)
<taskdef name="deploy" 

<taskdef name="undeploy" 

<target name="deploy" 
    description="Install web application"
    <deploy url="${url}" 

<target name="undeploy" 
    description="Remove web application">
    <undeploy url="${url}" 

But when you run it via Terminal like so

adrian$ ant -Dusername=adrian -Dpassword=pass -f build.xml deploy

you see this really annoying error (even though it worked in your IDE, or maybe it worked on the first machine but not now)

build.xml:11: taskdef class org.apache.catalina.ant.DeployTask cannot be found
using the classloader AntClassLoader[]

Here's how I fixed this (on two machines now). Hopefully it will help you too.

First I have a symlink set up to point at my Tomcat installation, like so

ln -s /path/to/where/i/put/Tomcat /Tomcat

so you might do that too, but if not, just replace /Tomcat below with whatever your path to Tomcat is.

Then in Terminal run these 4 lines, supplying your admin password when prompted
sudo cp -p /Tomcat/lib/tomcat-util.jar /usr/share/ant/lib/
sudo cp -p /Tomcat/bin/tomcat-juli.jar /usr/share/ant/lib/
sudo cp -p /Tomcat/lib/tomcat-coyote.jar /usr/share/ant/lib/
sudo cp -p /Tomcat/lib/catalina-ant.jar /usr/share/ant/lib/

So we're just copying 4 JAR files to where your Mac has it's Ant installed.
Restart Terminal and this time you see
[deploy] OK - Deployed application at context path /ReptileKingdom


Fantastic! Back to writing awesome code for you.

Oh and one last thing - if you instead see another error, much like this

build.xml:69: Server returned HTTP response code: 401 for URL: http://localhost:8080/manager/text/undeploy?path=%2YourApp

then you need to edit your /Tomcat/conf/tomcat-users.xml file and make sure you have a user in there who has at least manager-script role, but I generally just give all of them like so manager-script,manager-gui,manager-jmx,manager-status.
And of course, the username and password you specify in your deploy call should match those for the user with the roles.