5

How to update external config files (e.g.: config-ex.groovy, config-ex.properties) without rebuilding the war file in Grails?

Restarting the application server will apply the new updates from external config files.

Tung
  • 1,579
  • 4
  • 15
  • 32
diep
  • 101
  • 1
  • 2
  • 4
  • I don't understand the question - a grails app _will_ see changes to its external configuration files on restart, there's no need to rebuild the war. – Ian Roberts Nov 26 '12 at 09:05

3 Answers3

5

If I understand well you want to externalized Grails config outside the war. You can define an external config in your config.groovy like this

grails.config.locations = ["file:path/to/your/Configfile.groovy"]

See the Grails doc 4.4 Externalized Configuration

Benoit Wickramarachi
  • 6,096
  • 5
  • 36
  • 46
3

Define your external Grails config with:

grails.config.locations = ["file:some/path/to/Config.groovy"]

Then to reload them at runtime, you can use code like this:

def config = grailsApplication.config
def locations = config.grails.config.locations

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

I have the above code in an admin protected Controller.

Gregg
  • 34,973
  • 19
  • 109
  • 214
1

Went around the houses for this one, thanks Gregg

For services or groovy src files you could use:

import org.springframework.context.ApplicationContext
ApplicationContext ctx = (ApplicationContext) org.codehaus.groovy.grails.web.context.ServletContextHolder.getServletContext().getAttribute(org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes.APPLICATION_CONTEXT);
def grailsApplication = ctx.getBean("grailsApplication")
ConfigObject config = ctx.getBean(GrailsApplication).config
def locations = config.grails.config.locations
locations.each {
   String configFileName = it.split("file:")[1]
   config.merge(new ConfigSlurper().parse(new File(configFileName).text))
}

And for abstract classes that are typically extended from controllers:

import grails.util.Holders
def config = Holders.config
def locations = config.grails.config.locations
locations.each {
  String configFileName = it.split("file:")[1]
  config.merge(new ConfigSlurper().parse(new File(configFileName).text))
 }
V H
  • 8,382
  • 2
  • 28
  • 48