Share config between two grails apps that share a common plugin

不问归期 提交于 2019-12-02 07:41:28

The grails.config.locations definition for external configuration files can include java.lang.Class objects to load configuration from pre-compiled Groovy scripts, as well as file: or classpath: URLs to parse Groovy or .properties files at runtime. So you should be able to create a configuration file in the plugin under src/groovy

{plugin}/src/groovy/com/example/CommonConfiguration.groovy

package com.example

environments {
  production {
    ...
  }
  development {
    ...
  }
}

and then in the applications' Config.groovy files include this class in grails.config.locations

grails.config.locations = [com.example.CommonConfiguration]

However this does mean that when the plugin's CommonConfiguration and the host app's Config.groovy both specify a value for the same property, the plugin would win. To redress the balance, you'd need to put a second external in grails.config.locations (which could be another Class or a URL)

grails.config.locations = [com.example.CommonConfiguration,
                           "file:app-config.groovy"]

and put app configuration in there (as later externals override earlier ones).

Given that you want to embed the configuration within the plugin you will need to make your plugin smart enough to read it's own configuration and merge that into the containing applications config. The following is based on Grails 1.3.7. The configuration holder may have changed since then (2.0 did a lot of house cleaning) but I am sure you can figure that part out. This example assumes that there is a configuration file called grails-app/conf/MyPluginConfig.groovy inside your plugin.

Inside your /MyPlugin.groovy you will add this merge of your configuration in the doWithSpring closure.

def doWithSpring = {
    // get the current application configuration
    def currentConfig = org.codehaus.groovy.grails.commons.ConfigurationHolder.config
    GroovyClassLoader classLoader = new GroovyClassLoader(getClass().classLoader)

    // get the plugin configuration
    def pluginConfig = new ConfigSlurper(grails.util.GrailsUtil.environment).parse(classLoader.loadClass('MyPluginConfig'))

    // merge the configurations
    pluginConfig.merge(currentConfig)

    // set the application configuration to the merged configuration
    org.codehaus.groovy.grails.commons.ConfigurationHolder.config = pluginConfig
}

That's it in a nutshell. Hope this helps.

Also, take note that you can still override the values in your containing application because of the way the merge is done. The application configuration is merged into the plugin configuration. If the containing application defines something it will override the plugins value.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!