other register

Thursday, February 10, 2011

Reload External Config File Dynamically

If the grails application's Config.groovy file is changed, the war file has to be regenerated to reflect the changes, and the war has to be redeployed onto the server. This process normally involves:

1. Generate the grails application war file.
2. Go to the tomcat manager console
3. Click "Undeploy" to remove the application's context.
4. Upload the newly generated war to the ${tomcat}/webapps directory

Adding an external config file (like the code below) in the Config.groovy file does allow not to re-generate the war file. It does allow you to define sensible configuration info (e.g. username, password, access allowed users, etc.) in an external file rather than packed within the generated war file.

grails.config.locations = [ "file:${userHome}/.grails/CoursePackApp/${appName}-config.groovy"]

However the web application doesn't load the changes in the external config file automatically, thus the application has to be stopped and re-started. This process normally involves:

1. Go to the tomcat manager console
2. Click "stop" to stop the application
3. Click "start" to start the application, so that the application can pick up the changes in the external config file.

To avoid the above hassle (i.e. regenerating the war file, restarting the application) every time we change the .

In the grails-app/conf/Config.groovy file, add:

grails.config.locations = ["file:${userHome}/.grails/YourPath/${appName}-config.groovy"]

On the server, create the external file at:


In a controller which is the entry point to the application (i.e. Login Controller), add the code below. Thus every time this controller action is accessed, the external config is re-loaded.

// Courtesy IntelliGrape

// access external config file

def config = ConfigurationHolder.config
def locations = config.grails.config.locations

locations.each {
 String configFileName = it.split("file:")[1]
 config.merge(new ConfigSlurper().parse(new File(configFileName).text))

No comments: