Grails Config Values Per Tomcat Host

Sometimes in a Grails application it is necessary to have configuration values available at the Tomcat Host level. An example of such a situation would be needing multiple Hosts in a single Tomcat instance all using the same version of a war. Each war is essentially a copy that contains generic application code that relies on external configuration values to configure its own database, Host-specific labels, integration configuration, etc. For example, you could have 3 Hosts – check here for details on specific configuration of Tomcat Hosts:

Each Host’s application will need its own external configuration files, which is a problem since the wars are identical copies. The traditional use of external configuration in bootstrap.groovy would end in each Host using the same files and being configured for the same Host:

We need a way to provide the application with variables pointing to the correct configuration files on a per Host basis. We can solve this with JNDI by configuring a Tomcat Context for each Host. Each Context will contain an Environment variable containing the location of the correct configuration file. In the {Tomcat}/conf/Catalina/{Host} directory, create an xml file for each Host (I’ll call it context.xml) containing:

The “grailsExtConfFile” Environment variable will be available to your application, so you just have to look it up and use it in Config.groovy:

Your application is now ready to deploy and run as multiple Hosts on one Tomcat instance. But how do you run locally while developing and testing? The easiest way is by detecting the environment and providing external configuration files more traditionally if Environment.DEVELOPMENT or Environment.TEST:

About the Author

Object Partners profile.

One thought on “Grails Config Values Per Tomcat Host

  1. Naresha says:

    I see that JNDI lookup code is executed when I run ‘grails war’. Is there anyway it can be executed during runtime?

    Thanks.

    1. Naresha says:

      Thanks. It works though I get the error in JNDI lookup when I run ‘grails war’.

  2. When I used this mechanism I just wrapped the lookup in a try/catch and ate the exception so it wouldn’t affect the packaging process. However, I believe you could use BuildScope to see if the current scope is “war”, and if so don’t do the lookup. Expanding on the code from above it would be something like :


    if (Environment.current in [Environment.DEVELOPMENT, Environment.TEST] || BuildScope.current == BuildScope.WAR) {
    exConfig = “file:/path/to/local/applicationConfig.groovy”
    } else {
    try {
    exConfig = ((Context)(new InitialContext().lookup(“java:comp/env”))).lookup(“grailsExtConfFile”)

  3. Sebastian says:

    You might need to add additional import statments into the Config.groovy file:

    import javax.naming.Context
    import javax.naming.InitialContext

  4. blacar says:

    It works perfect. Thanks for this!

    However i see you are using println … would it be possible to use log.warn or something like that? … i’ve tried but it does not works out of the box.

Leave a Reply to Naresha Cancel reply

Your email address will not be published. Required fields are marked *

Related Blog Posts
Seamlessly Integrating Micro Apps with iFrame
A recent client wanted to upgrade a small portion of their legacy application with a more modern UI and extra functionality, like fuzzy text search. There are a few approaches to incremental upgrades of legacy […]
Consul on Docker EE
[markdown]”Consul is a service networking solution to connect and secure services across any runtime platform and public or private cloud” – [https://consul.io](https://consul.io) This post details a way to run a Consul cluster on Docker EE […]
Passing the AWS Machine Learning Speciality Exam
The Amazon Machine Learning Specialty Exam is a 3-hour, 65 question test. It is designed to test your skills in AWS specific Data Engineering and Machine Learning Practices along with Machine Learning in general. I […]
Consistent Python environments with Poetry and pre-commit hooks
Clean and Consistent Environments Regardless of the programming language you are working in, it can sometimes be a struggle to maintain a clean codebase and a consistent development environment for all members of your team, […]