Michael Landes Michael Landes - 4 months ago 112
Java Question

Adding external resources to class-path in Tomcat 8

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.VirtaulWebappLoader"
virtualClasspath="/PROJECT_NAME/conf"
searchVirtualFirst="true" />
</Context>


How do I achieve this same thing in Tomcat 8?

Answer

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>