Tomcat 7 With Serlvet 3 Annotations – Note To Self


January 11, 2013 | Posted in Uncategorized | By

I made a quick web servlet application that uses the new annotations available (like @WebServlet) and had everything working just fine until I did something rather bone headed. I am using Maven for my dependency management and from the command line I was using the following command to create a WAR file for deployment:

$ mvn clean war:war

This works absolutely fine and creates the WAR file as expected but when I would deploy the file on Tomcat 7 the application would start successfully but none of the servlets that I had created with annotations would show as being registered with the application and I would get a 404 when I tried to access them. So, after researching and looking at adding things into the web.xml I finally figured out what I was doing wrong. If you know Maven better than I do you probably already know the problem.

When I ran the above Maven command it would delete the ‘target’ directory and then recreate the WAR file from the resources needed for the web application. One critical thing was missing from this sequence of Maven events – and that would be the compilation the application’s source files. The ‘clean’ command was deleting the ‘.class’ files and the war:war command does not auto-magically know that they are not there and compile them for you. So, the Maven command that should I should run from now on for this application is:

$ mvn clean compile war:war

Hopefully this helps me remember how to do things correctly and might save someone else out there some headache as well.

Read More →