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?
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>
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>
Just copy the property files into the Tomcat lib folder.
Or enhance conf/catalina.properties:common.loader
with a folder for the property files as described here:
How to add external resources (properties file) on to the classpath so that war can read?