I need to provide a config file path to my Java web application that runs in Apache Tomcat 10.1.
The config path must be set outside of the deployment. It's a property of the machine / stage on which the application is deployed. And not an attribute of the application. So it must not be set in the web.xml.
For example /srv/myapp/prod/config.ini or C:\EclipseDeveloperOnWindows\config.ini.
I've thought about multiple options:
- Operating system environment variable
MYAPPCONFIG, likePATHorJAVA_HOME: Independent of my application and even independent of the Tomcat container. Downside: May become error-prone if I want to run multiple distributions / Tomcats with different configurations as the same system user. - Java system property as a command line option for Tomcat in
CATALINA_OPTS, e.g.-DmyApp.config=/some/path.ini - Tomcat context parameter in
<CATALINA_BASE>\conf\server.xml. This way, each Tomcat instance (oneCATALINA_HOME, multipleCATALINA_BASE) could define a different value for the config path property. (Do I have to include a reference to theserver.xmlcontext parameter in theweb.xml? And can I look it up fromServletContext?) - Tomcat environment entry in
<CATALINA_BASE>\conf\server.xml- what are the differences / pros and cons of Tomcat context parameters vs. Tomcat environment entries? (Can I look it up fromServletContext?) - Something else, that maybe can be looked up using JNDI?
Did I miss some options? And which one is recommended? Which approach do you use for what reason?
Putting the configuration into the Context is probably your best option. Note that Contexts can be defined in various ways, not just in
server.xml.See the Defining a context in the Tomcat 10.1 docs for details.
For our product, the same war file is deployed to the staging environment and the production environment. The differences between the environments are exhaustively described by the two Context descriptors (one for each environment).