从 .war 文件外部化 Tomcat webapp 配置

编程入门 行业动态 更新时间:2024-10-15 00:27:20
本文介绍了从 .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 配置

本文发布于:2023-11-25 20:42:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631279.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   war   webapp   Tomcat

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!