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

3

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.

Feigned answered 26/11, 2012 at 8:30 Comment(1)
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.Concupiscence
G
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

Germainegerman answered 26/11, 2012 at 15:1 Comment(0)
E
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.

Enslave answered 26/11, 2012 at 19:45 Comment(0)
P
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))
 }
Perlman answered 9/10, 2013 at 11:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.