views:

69

answers:

3

If I have an dependency Jar for my application is it better to place it in the war files lib directory or to place it in the global application server (like Tomcat) lib directory? What do I gain by using one approach over another?

Diskspace springs to mind, but we live in a time when diskspace is cheap. Is there a memory usage difference? Can someone with more experience then me list the pros and cons of both options?

+1  A: 

If you want to use the container's resource management capabilities -- e.g. connecting to a SQL database and providing a JNDI lookup and connection pool for it -- then the container software itself will need access to the libraries and drivers to manage the resources.

Otherwise you probably don't want to install them in the server /lib directory and assume they are there and will work, as different web applications might have subtly different version requirements.

Steven Huwig
+1  A: 

For a in-depth description of the class loader hierarchy implemented by Catalina, you should check Tomcat's Class Loaders HOW-TO. This will help you to understand when to make jars available to the container, to all webapps, to a single webapp only... and where to put them.

Pascal Thivent
+2  A: 

Generally speaking, it's much better to have WAR self-contained so you don't have to rely on the container configuration. It makes deployment much easier also. So try to put library in the WAR if you can.

However, I ran into cases when installing libraries to container makes sense. For example,

  1. We have some internal libraries used by every webapp and they are huge. We install them to container so all the webapps use the same version and it saves on memory and diskspace too.

  2. Libraries installed in WEB-INF/lib is not available to container. If you need to reference these in context.xml (like JDBC driver defined in Resources), you have to put them in server/lib.

  3. If you want send log4j logs from all the webapps to the same file, you have to put log4j jar in the server/lib. Otherwise, each webapp uses its own logger.

ZZ Coder
On point 3, is the converse true? I'm putting log4j in the servers/lib because everything is using it, but I don't want everything to go to the same file. Is there a way to get around that?
James McMahon
Yes. See my answer to this question: http://stackoverflow.com/questions/1332691/how-to-configure-logs-catalina-out-of-tomcat-6-for-per-app-configure-web-app-sp
ZZ Coder