Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding external resources to class-path in Tomcat 8

Tags:

I have a Tomcat application which needs to reference some properties files that are external to the app. Generally these are stored on a local machine at a specific place like C:\PROJECT_NAME\conf\.

In Tomcat 7 this was achievable by placing a context.xml file inside of /META-INF/ which used a VirtualWebappLoader to essentially add this location to the application classpath as follows:

<Context>     <Loader className="org.apache.catalina.loader.VirtualWebappLoader"         virtualClasspath="/PROJECT_NAME/conf"         searchVirtualFirst="true" /> </Context> 

How do I achieve this same thing in Tomcat 8?

like image 633
Michael Landes Avatar asked Apr 17 '14 21:04

Michael Landes


People also ask

What is classpath in Tomcat server?

A classpath is an argument that tells the JVM where to find the classes and packages necessary to run a program. The classpath is always set from a source outside the program itself.

How Tomcat ClassLoader works?

Like many server applications, Tomcat installs a variety of class loaders (that is, classes that implement java. lang. ClassLoader ) to allow different portions of the container, and the web applications running on the container, to have access to different repositories of available classes and resources.

Where do I put jar files in Tomcat?

you can upload the . jar's to /home/tomcat/lib, which is mentioned in this doc. /home/tomcat/lib is added to Tomcat's class loader, so the . jar will be added just like when you put it under CATALINA_HOME/lib."


2 Answers

There is a section about this in the Tomcat 8 migration guide which will direct you to use a resources configuration

In particular, you will be creating a WebResourceRoot object which contains the following text in its description.

VirtualWebappLoader - Replaced by Pre- and Post-Resources mapped to WEB-INF/lib and WEB-INF/classes

Your new context.xml will look something like the following:

<Context>     <Resources className="org.apache.catalina.webresources.StandardRoot">         <PreResources className="org.apache.catalina.webresources.DirResourceSet"             base="C:\\PROJECT_NAME\\conf"             internalPath="/"             webAppMount="/WEB-INF/classes" />     </Resources> </Context> 
like image 132
Michael Landes Avatar answered Sep 19 '22 10:09

Michael Landes


Just another example:

Please note the comments inside and note that I used PostResources and not PreResources so that I can override classes in my current project if I'm not happy with my "util" implementation. I'm not actually sure if JarResource is treated as a "PostResource" or "PreResource" but overriding static content and classes works.

    <!--          https://tomcat.apache.org/tomcat-8.0-doc/config/resources.html          http://stackoverflow.com/questions/23143697/adding-external-resources-to-class-path-in-tomcat-8          http://stackoverflow.com/questions/34515852/tomcat-7-application-migration-to-tomcat-8          http://mikusa.blogspot.co.za/2014/07/tips-on-migrating-to-tomat-8-resources.html     -->     <Context path="/MY_PROJECT" docBase="/MY_PROJECT">         <Resources className="org.apache.catalina.webresources.StandardRoot">             <!-- Post-load the static content from my util project -->             <PostResources className="org.apache.catalina.webresources.DirResourceSet"                     base="/workspace/MY_UTIL_PROJECT/WebContent"                     webAppMount="/">             </PostResources>             <!-- Post-load the classes from my util project -->             <PostResources className="org.apache.catalina.webresources.DirResourceSet"                     base="/workspace/MY_UTIL_PROJECT/WebContent/WEB-INF/classes"                     webAppMount="/WEB-INF/classes">             </PostResources>             <!-- Load the JARs contained within my util project -->             <JarResources className="org.apache.catalina.webresources.DirResourceSet"                     base="/workspace/MY_UTIL_PROJECT/WebContent/WEB-INF/lib"                     webAppMount="/WEB-INF/lib">             </JarResources>         </Resources>     </Context> 
like image 43
dutoitns Avatar answered Sep 18 '22 10:09

dutoitns