Java 将外部资源添加到 Tomcat 8 中的类路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23143697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding external resources to class-path in Tomcat 8
提问by Michael Landes
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\
.
我有一个 Tomcat 应用程序,它需要引用应用程序外部的一些属性文件。通常,这些存储在本地计算机上的特定位置,例如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:
在 Tomcat 7 中,这可以通过context.xml
在/META-INF/
其中放置一个文件来实现,该文件使用 aVirtualWebappLoader
基本上将此位置添加到应用程序类路径中,如下所示:
<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?
我如何在 Tomcat 8 中实现同样的功能?
采纳答案by Michael Landes
There is a section about this in the Tomcat 8 migration guidewhich will direct you to use a resources configuration
在Tomcat 8 迁移指南中有一个关于此的部分,它将指导您使用资源配置
In particular, you will be creating a WebResourceRootobject which contains the following text in its description.
特别是,您将创建一个WebResourceRoot对象,该对象在其描述中包含以下文本。
VirtualWebappLoader - Replaced by Pre- and Post-Resources mapped to WEB-INF/lib and WEB-INF/classes
VirtualWebappLoader - 替换为映射到 WEB-INF/lib 和 WEB-INF/classes 的前置和后置资源
Your new context.xml will look something like the following:
您的新 context.xml 将类似于以下内容:
<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>
回答by NS du Toit
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.
请注意里面的注释,并注意我使用了PostResources
而不是PreResources
为了如果我对我的“util”实现不满意,我可以覆盖当前项目中的类。我实际上不确定是否JarResource
被视为“PostResource”或“PreResource”,但覆盖静态内容和类有效。
<!--
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>
回答by weberjn
Just copy the property files into the Tomcat lib folder.
只需将属性文件复制到 Tomcat lib 文件夹中即可。
Or enhance conf/catalina.properties:common.loader
或者增强 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?