如何为 Tomcat 中的 Web 应用程序提供上下文配置? [英] How to provide a context configuration for a web application in Tomcat?

查看:19
本文介绍了如何为 Tomcat 中的 Web 应用程序提供上下文配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Web 应用程序,它依赖于一些资源和安装后要配置的参数,例如 JDBC 连接.

I have a web application that relies on some resources and parameters to be configured after it is installed, like a JDBC connection.

我想出的是提供一个 META-INF/context.xml 复制到 [engine-name]/[server-name]/[app-name].xml 在我部署应用程序时由 Tomcat 提供.这样我提供的只是一个可以复制到 appBase 文件夹 (webapps) 中的 war 文件.Tomcat 的文档 如果有这样的文件它不会被覆盖,这是真的太好了,因为部署后所做的更改不会丢失.

What I have come up with is providing a META-INF/context.xml which is copied into [engine-name]/[server-name]/[app-name].xml by Tomcat when I deploy the application. This way all I am providing is a war file that can be copied into the appBase folder (webapps). Tomcat's documentation says if there is such a file it won't be overwritten which is really great, since the changes made after deployment won't be lost.

但是这里有一个微妙的问题:由于我们通过复制到 webapps 目录来部署应用程序,因此 Tomcat 将首先卸载现有应用程序以及配置文件.这样,配置文件将丢失/覆盖,这是不可取的.据我所知,Tomcat 不会修改这种行为.

But there is a subtle issue here: Since we deploy the application by copying into webapps directory, Tomcat will first uninstall the existing application as well as the configuration file. This way the configuration file will be lost / overwritten which is not desirable. Tomcat won't modify this behaviour as far as I know.

问题是:有没有办法通过以 Tomcat 不会删除现有配置文件的方式安装应用程序来解决此问题.或者,有没有更好的方法来打包应用程序?

The question is: Is there a way to work around this issue by installing the application in a way that Tomcat won't remove the existing configuration file. Or, is there a better way of packaging the application?

请注意,我们不想将 autoDeploy 设置为 false,并且我们不能使用人工干预进行安装(排除使用 Tomcat Manager Web 应用程序).

Please note that we don't want to set autoDeploy to false and we cannot use human intervention for the installation (which rules out using Tomcat Manager web application).

如果我从.war文件中取出配置文件并单独复制为[engine-name]/[server-name]/[app-name].xml,Tomcat仍然会关联将它与我的应用程序一起使用,并在我复制新的 .war 文件后将其删除.

If I get the configuration file out of .war file and copy it separately as [engine-name]/[server-name]/[app-name].xml, Tomcat will still associate it with my application and remove it once I copy a new .war file.

另一个假设是:我们事先不知道配置的值.我们将只提供一个示例配置(一个占位符,如果您愿意),而实际配置将在稍后的某个时间执行(不一定在安装时).

Another assumption is: We don't know in advance the values to the configuration. We will only provide a sample configuration (a placeholder, if you wish) while actual configuration will be performed at some time later (not necessarily in the installation time).

谢谢

推荐答案

我设法以某种方式解决了这个问题.

I managed to resolve this issue somehow.

1- 在外部 Tomcat 的appBase 的某处安装一个分解的WAR 目录,假设它在/usr/local/MyApp 中.[如果您的应用程序从未爆炸的战争中运行,您可以在此步骤中使用 WAR 文件而不是 WAR 目录.]

1- Install an exploded WAR directory somewhere outside Tomcat's appBase, let's assume it is in /usr/local/MyApp. [You can use a WAR file for this step instead of WAR directory, if your application runs from an unexploded war.]

2- 将上下文配置文件复制到[tomcat.conf]/[engine]/[hostname] 目录中,我们称之为MyApp.xml.此文件将指向应用程序的位置:

2- Copy the context configuration file into [tomcat.conf]/[engine]/[hostname] directory, let's call it MyApp.xml. This file will point to the location of the application:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Context configuration file for my web application -->
<Context docBase="/usr/local/MyApp" privileged="true" antiResourceLocking="false" antiJARLocking="false">
        <Resource name="jdbc/myapp-ds" auth="Container" type="javax.sql.DataSource"
                maxActive="100" maxIdle="30" maxWait="10000" username="XXX" password="XXX"
                driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/mydb" />
</Context>

3- 您现在可以自由修改配置文件了.

3- You are now free to go and modify the configuration file.

4- 通过在/usr/local/MyApp 中复制新版本的应用程序来更新应用程序

4- Update the application by copying new version of your application in /usr/local/MyApp

注意事项:

a) 此解决方案也适用于未扩展的 .war 文件,但由于我们使用 Spring 的 Log4JConfigListener,它不会从未扩展的 .war 文件运行.Tomcat 不会爆炸放在 appBase (webapps) 文件夹之外的 .war 文件.

a) This solution applies to an unexpanded .war file as well, but since we use Spring's Log4JConfigListener it wouldn't run from an unexploded .war file. Tomcat doesn't explode .war files put outside appBase (webapps) folder.

b) 这种方法不会阻止您将 context.xml 放在/usr/local/MyApp/META-INF/context.xml 中,因为 Tomcat 在此配置中不会使用它.您可以在开发环境中使用它,将 .war 文件转储到 appBase (webapps) 文件夹中.

b) This approach doesn't prevent you from having context.xml in /usr/local/MyApp/META-INF/context.xml since it will not be used by Tomcat in this configuration. You can use it in your dev environment, where you dump your .war file into the appBase (webapps) folder.

这是我目前得到的,仍在寻找更好的解决方案.

This is what I've got so far, still looking out for better solutions.

这篇关于如何为 Tomcat 中的 Web 应用程序提供上下文配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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