How to package your Tomcat webapp
If you are a web application developer, you should want to properly make a Debian package to help installing your application in Debian and Ubuntu. This article will detail the options you have for Tomcat 6.0 webapps. It was also posted for reference (and further improvement) at https://help.ubuntu.com/community/Tomcat/PackagingWebapps. It supposes you already know the basics of Debian packaging, and how Tomcat roughly works.
Option 1: Use the system instance
The tomcat6 package provides a system-wide instance. If your webapp is relatively simple and does not require a specific configuration, you should consider packaging your webapp for deployment there. That’s what the tomcat6-examples and tomcat6-admin packages do. To benefit from webapp autodeployment, you should:
- Depend on the tomcat6 package
- Install your webapp into /usr/share/PACKAGENAME/WEBAPP
- Install a deployment descriptor file into /etc/tomcat6/Catalina/localhost/WEBAPP.xml
For example, the tomcat6-examples package installs the webapp files into /usr/share/tomcat6-examples/examples, and then installs the following examples.xml file into /etc/tomcat6/Catalina/localhost :
<Context path="/examples" docBase="/usr/share/tomcat6-examples/examples" />
Your webapp should be automatically picked up by Tomcat !
Option 2: Use your own instance
Sometimes using the system-wide Tomcat 6.0 instance is just not the right solution. The most common case is when your webapp requires a very specific configuration. Rather than beating the system-wide instance into submission, by mangling the configuration file (or asking your users to do so), it’s better to set up your own instance. Tomcat 6.0 in Ubuntu and Debian provides the possibility to set up multiple instances of tomcat that share the same binaries, through the tomcat6-common package. You should then:
- Depend on the tomcat6-common package
- Deploy a full CATALINA_BASE directory with conf/ and webapp/ subdirectories in /usr/share/PACKNAME
- Create an init script in /etc/init.d/PACKNAME that starts catalina.sh with CATALINA_BASE=/usr/share/PACKNAME and CATALINA_HOME=/usr/share/tomcat6
- Have postinst create a specific user to run under, and use that in your init script
- Create specific directories for logs and temporary files
What should the CATALINA_BASE directory look like ? Something like this:
/usr/share/PACKNAME conf/ <-- the configuration directory with your specific config logs -> /var/log/PACKNAME temp -> /var/cache/PACKNAME/temp webapps/ <-- the directory containing your webapps work -> /var/cache/PACKNAME/work
It is slightly more complex, but that way you can fine-tune your configuration without affecting any other webapp installed, since you use your own, separate, instance. Most importantly, you still get the benefit of bugfix and security updates in the common binaries.
If you need some examples, the tomcat6-user package provides a tomcat6-create-instance command that creates CATALINA_BASE directories for user instances, you can use that as a base. For the init script, /etc/init.d/tomcat6 can be used as a base.