简体   繁体   English

如何在 WAR 存档中包含应用程序配置文件?

[英]How to include app config file in WAR archive?

I'm having trouble deploying a Java web app.我在部署 Java web 应用程序时遇到问题。 It centers on our application config file (myapp.properties).它以我们的应用程序配置文件 (myapp.properties) 为中心。 In certain situations, having this file external to the WAR seems to work fine, but whenever I try to deploy it properly (Docker, proper JBoss deployment, etc.) the deployments inevitably fail due to being unable to find/read from myapp.properties.在某些情况下,将这个文件放在 WAR 外部似乎可以正常工作,但是每当我尝试正确部署它(Docker、正确的 JBoss 部署等)时,由于无法从 myapp.properties 中找到/读取,部署不可避免地会失败.

I've seen a few references here and elsewhere to packaging these files inside the WAR archive, but I can't find any information on how to actually do that.我在这里和其他地方看到了一些将这些文件打包WAR 存档中的参考资料,但我找不到任何有关如何实际执行此操作的信息。 So, my questions is:所以,我的问题是:

What is the proper way to include an application config file in a WAR archive and/or deployment?在 WAR 存档和/或部署中包含应用程序配置文件的正确方法是什么?

It depends in how you want to access the file.这取决于您要如何访问该文件。

If you want to load it via classloader resources, you need it to be in the class-path, so the best location is inside WEB-INF/classes .如果你想通过类加载器资源加载它,你需要它在类路径中,所以最好的位置是在WEB-INF/classes里面。

That's by the way, where resources in src/main/resources will end up if you are using Maven顺便说一句, 如果您使用的是 Maven ,则src/main/resources中的资源将结束

You can also put it anywhere inside your WAR, for example, WEB-INF/config and access it via ServletContext.getResourceAsStream("/WEB-INF/config/.... . Beware but, that it's not inside the WEB-INF directory, you will have to explicitly forbid the access to the file in the web.xml deployement descriptor, otherwise it would be accessible to client requests.您也可以将它放在 WAR 中的任何位置,例如WEB-INF/config并通过ServletContext.getResourceAsStream("/WEB-INF/config/....访问它。但请注意,它不在WEB-INF目录,您必须明确禁止访问web.xml部署描述符中的文件,否则客户端请求可以访问该文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM