部署具有不同环境条目和资源引用的 tomcat 应用程序
我开发了一个在本地 Apache Tomcat 7.0.14 服务器(我的开发环境)上运行的 Web 应用程序。我已准备好将应用程序推送到生产主机,但在管理每个环境的不同条目值时遇到问题。
我当前的构建过程完全在我的 Netbeans 7.0.1 IDE 中执行。它使用 Ant(我承认我不熟悉它)并生成一个 .war 文件。我可以通过复制此 .war 文件来部署到我的生产主机。应用程序运行,但缺少所有正确的生产配置数据。
我已经编写了可合理配置的应用程序。与大多数应用程序一样,我的开发环境和生产环境之间的各种数据必须有所不同。我能够将所有这些数据分离到我的部署描述符 (web.xml) 或上下文配置 (context.xml) 文件中,然后在我的应用程序中读取它。这些值的详细信息并不是特别相关,但作为示例,至少有一个资源引用值充当数据源。
我无法做的是轻松地为我的不同配置维护单独的值。我希望拥有 web.xml 或 context.xml 文件的第二个副本,我可以使用它来构建 .war 文件的单独副本以部署到生产环境。我也很高兴有一个允许我将某种变量(${}?)插入到该文件中的解决方案。但是,我必须能够在文件中维护数据本身,因为会有大量条目。由于无论哪种方式数据都会在文件中,我认为自然的解决方案是第二个 web.xml 或 context.xml 文件。
理想情况下,我想要 Netbeans 中的一个简单解决方案。如果没有的话,在 Ant 中执行此操作的说明可能就足够了。任何相关术语也会有所帮助。我在 tomcat 中迷失了有关“上下文”、“环境”和“部署”的内容,它们的含义与我正在寻找的含义不同。最后,如果这两者都无法完成,是否有工具可以提供帮助?编写一个独特的 shell 脚本远非理想。
很难相信没有一个简单的解决方案可以解决这个问题。这似乎是每个部署都需要解决的问题。这不是条目和引用存在于 xml 中的原因吗?但是,我在 Tomcat 手册中找不到任何与此相关的信息。
I have developed a web application that runs on an Apache Tomcat 7.0.14 server locally (my development environment). I am ready to push my application to a production host, but I am having issues managing distinct entry values for each environment.
My current build process is performed entirely within my Netbeans 7.0.1 IDE. It uses Ant, which is something I will admit I am not familiar with, and generates a .war file. I am able to deploy to my production host by copying this .war file. The application runs, though it is missing all the correct production configuration data.
I have written my application to be reasonably configurable. Like most apps, a variety of data must vary between my development environment and the production environment. I am able to separate all this data into my deployment descriptor (web.xml) or context configuration (context.xml) file, and then read it within my application. The details of the values are not be particularly relevant, but as an example there is at least one Resource reference value that serves as a DataSource.
What I am not able to do is to easily maintain separate values for my distinct configurations. I'm hoping to have a second copy of the web.xml or context.xml file, which I could use to build a separate copy of a .war file to deploy to production. I would also be happy with a solution that permits me to insert some kind of variables (${}?) into this file. However, I must be able to maintain the data itself in a file because there will be a large number of entries. Since the data will be in files either way, I thought the natural solution was a second web.xml or context.xml file.
Ideally, I'd like a simple solution within Netbeans. Lacking that, instructions for doing this in Ant would probably be sufficient. Any relevant terminology would be helpful as well. I've gotten lost reading about "contexts", "environments", and "deployments" in tomcat, which have meanings other than what I'm looking for. Finally, if this can not be done via either, is there a tool that could help? Writing a distinct shell script would be far from ideal.
It's hard to believe there is not a trivial solution to this. It seems like the sort of thing that every deployment would need to address. Isn't that why the entries and references exist in xml? However, I could find nothing regarding this in the Tomcat manual.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你有很多选择。对于构建特定的属性,我建议为每个环境创建属性文件,并通过 Ant 中的
property
任务加载它们。例如,给定假设环境 dev、qa、prod,您可以拥有名为 build.{env}< 的属性文件/em>.properties,并像这样加载它们:build.xml
调用为:
这为您提供了一些有用的构建时属性替换。但当然,您更感兴趣的是替换项目文件中的变量。实现此目的的方法之一是使用 Ant 替换任务。用法示例:
您将调用与我给出的第一个示例类似的 Ant 构建。您可以使用附加
include
指令根据需要添加任意数量的资源类型。最后,您还可以选择用环境标识符标记一整套文件,并使用 Ant 复制任务。例如:
您可以根据需要填写
targetdir
和sourcedir
。上面的代码片段会将 web.dev.xml 或 context.dev.xml 类型的所有文件提升为 web.xml 和 context.xml。您可以组合上述全部或部分方法来获得适合您环境的高度定制的构建。我还建议将 Maven 作为构建工具,它使此类任务变得更加容易!
You have lots of options. For build specific properties I would recommend creating property files for each environment and loading them via the
property
task in Ant. So for example, given hypothetical environments dev, qa, prod, you could have property files with names build.{env}.properties, and load them up like so:build.xml
invoked as:
That gives you some useful build time property replacement. But of course, you are more interested in replacing variables in your project files. One of the ways of doing that is with the Ant Replace Task. Example usage:
You would invoke your Ant build's similar to the first example I gave. You can add as many resource types as you need with additional
include
directives.And lastly, you can also choose to have a whole set of files tagged with an environment identifier, and 'promote' them to the desired build artifact using the Ant Copy Task. For example:
You would fill in
targetdir
andsourcedir
as needed. The snippet above would promote all files of the type web.dev.xml or context.dev.xml to web.xml and context.xml.You can combine all or some of the methods above to get a highly customized build that fits your environment. I would also recommend looking at Maven as a build tool, it makes tasks like this much easier!