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

查看:251
本文介绍了如何在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]。部署应用程序时由Tomcat创建的xml 。这样我所提供的是一个war文件,可以复制到appBase文件夹(webapps)。
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-在某处外部安装一个爆炸的WAR目录 Tomcat的appBase,我们假设它位于 / 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不会将.war文件放在appBase(webapps)文件夹之外。

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)这种方法不会阻止你在/ usr / local中使用context.xml /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天全站免登陆