简体   繁体   中英

Define environment variable for app in Tomcat, outside of deployment

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 , like PATH or JAVA_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 , eg -DmyApp.config=/some/path.ini
  • Tomcat context parameter in <CATALINA_BASE>\conf\server.xml . This way, each Tomcat instance (one CATALINA_HOME , multiple CATALINA_BASE ) could define a different value for the config path property. (Do I have to include a reference to the server.xml context parameter in the web.xml ? And can I look it up from ServletContext ?)
  • 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 from ServletContext ?)
  • 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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