Can I create a custom classpath on a per application basis in Tomcat

sparkyspider picture sparkyspider · Jun 14, 2011 · Viewed 32.8k times · Source

For some applications I use ZK, others Hibernate, other Apache Commons, etc.

I don't want to deploy a 75MB war file, just because it uses lots of libraries.

I don't want to add the libraries to my tomcat lib folder, or nor the classpath to it's configuration as I may have an old application using library x.1 and another application using library x.2

For this reason, it would be great to have something in the web.xml or context.xml where I say something like:

<classpath>/usr/local/tomcat/custom-libs/zk-5.0.4</classpath>

Note: The above is pseudo-code

Answer

sparkyspider picture sparkyspider · Jun 20, 2011

From Tomcat 7 there is no mention of not being able to use the VirtualWebappLoader in production. I tried it and it works like a dream. Simply add the following to META-INF/context.xml:

<?xml version="1.0" encoding="UTF-8"?>

<Context antiJARLocking="true" path="/websandbox">
    <Loader className="org.apache.catalina.loader.VirtualWebappLoader"
          virtualClasspath="/usr/.../*.jar;/usr/.../*.jar"/>
</Context>

In Netbeans, under packaging, I just untick all the packages, taking the .war size down to nothing, make sure the dependencies are in the correct folders on the server and upload. Yey! No more 100 MB WAR file.