从 .war 文件外部化 Tomcat webapp 配置 [英] Externalizing Tomcat webapp config from .war file

查看:39
本文介绍了从 .war 文件外部化 Tomcat webapp 配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Tomcat 7 中配置 webapp 时遇到问题.在我的 WAR 文件中,有一个属性文件 myApp/WEB-INF/classes/myProps.props,它包含特定于环境的属性.我正在尝试覆盖服务器上的该配置文件,以便将同一个 WAR 文件部署到多个环境中.

I am having trouble with configuring a webapp in Tomcat 7. In my WAR file, there is a properties file myApp/WEB-INF/classes/myProps.props, and it contains environment-specific properites. I am trying to override that configuration file on the server, so that the same WAR file will deploy to multiple environments.

我听说有一种方法可以使用 tomcat/conf/Catalina/myApp 中的替换配置文件来做到这一点.这是我无法弄清楚的方法.

I heard there was a way to do this using replacement config files in tomcat/conf/Catalina/myApp. This is the method I am having trouble figuring out.

此外,myApp.war 是在同一台 Tomcat 服务器上运行的众多程序之一,它不作为 localhost 运行.我希望能够为几个 web 应用程序解决这个问题.

Also, myApp.war is one of many running on the same Tomcat server, and it does not run as localhost. I want to be able to solve this problem for several of the webapps.

Server version: Apache Tomcat/7.0.23
Server built:   Nov 20 2011 07:36:25
Server number:  7.0.23.0
OS Name:        Linux

推荐答案

你的 tomcat/conf/Catalina/ 可以包含上下文描述符,让你配置很多东西,包括定义环境条目",可通过 JNDI 从 Java 访问.有很多方法可以使用它.就个人而言,我设置了一个环境条目,它是我的属性文件的文件系统路径.我的应用程序旨在检查此条目,如果它不存在,请在类路径中查找该文件.这样,在 dev 中,我们在类路径上有 dev 属性,但是当我们构建和部署时,我们将它指向一个外部文件.

Your tomcat/conf/Catalina/<host> can contain context descriptors that let you configure lots of things including defining "environment entries", which are accessible from Java via JNDI. There are lots of ways to go about using it. Personally, I set an environment entry which is the file system path to my properties file. My app is built to check for this entry, and if it doesn't exist, look for the file on the classpath instead. That way, in dev, we have the dev properties right there on the classpath, but when we build and deploy, we point it to an external file.

在 Tomcat 上配置上下文有很好的文档网站.请参阅定义上下文部分 关于如何创建文件和放置文件的详细信息.

There's good documentation for configuring a context on the Tomcat website. See the Defining a Context section on details of how to create the file and where to put it.

例如,如果您的主机名为 myHost 并且您的应用程序是 webapps 目录中名为 myApp.war 的 war 文件,然后你可以用这个内容创建 tomcat/conf/Catalina/myHost/myApp.xml:

As an example, if your host is named myHost and your app is a war file named myApp.war in the webapps directory, then you could create tomcat/conf/Catalina/myHost/myApp.xml with this content:

<Context>
    <Environment name="configurationPath" value="/home/tomcat/myApp.properties" type="java.lang.String"/>
</Context>

然后从您的代码中,您将在 java:comp/env/configurationPath(此处为 95% 确定性)上执行 JNDI 查找以获取该字符串值.

Then from your code, you'd do a JNDI lookup on java:comp/env/configurationPath (95% certainty here) to get that string value.

这篇关于从 .war 文件外部化 Tomcat webapp 配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆