Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update external config files without rebuilding war file in Grails

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.

like image 270
diep Avatar asked Nov 26 '12 08:11

diep


3 Answers

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

like image 194
Benoit Wickramarachi Avatar answered Nov 12 '22 21:11

Benoit Wickramarachi


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.

like image 23
Gregg Avatar answered Nov 12 '22 20:11

Gregg


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))
 }
like image 1
V H Avatar answered Nov 12 '22 21:11

V H