Search code examples
grailsgrails3

grails 3 - Update grails config during Plugin "doWithApplicationContext"


I've got a plugin that, during startup, reads some properties from the applications Config file, creates some domain objects and then needs to update the configuration with some additional information. However, it seems that the config object available during doWithApplicationContext is not the actual grailsApplication.config object.

For instance, attempting to do something straightforward in the MyPluginGrailsPlugin.groovy file like:

void doWithApplicationContext() {
  grailsApplication.config.put('test', 'testValue')
}

does not update the config.

If this plugin is incldued in an application, at any point after startup, grailsApplication.config.getProperty('test') will return null.

How does one go about updating the config map during plugin startup?

NOTE: In grails 2, this used to work.


Solution

  • With this code snippet in MyPluginGrailsPlugin.groovy's doWithApplicationContext, new properties were successfully added into the application's config object.

     ConfigObject myConfigObject = new ConfigSlurper().parse(props)
     PropertySource propertySource = new MapPropertySource('grails.plugins.myPlugin', [:] << myConfigObject)
     def propertySources = grailsApplication.mainContext.environment.propertySources
     propertySources.addFirst propertySource
    

    As an additional note: in doWithApplicationContext in my plugin, changing the config object like this worked in Grails 2 and no longer works in Grails 3.

    grailsApplication.config.merge(myConfigObject)
    grailsApplication.configChanged()