'Changing the Grails project configuration from within a plugin' post illustration

Changing the Grails project configuration from within a plugin

avatar

This code snippet shows how to update project configuration settings from a Grails plugin executable script. The script changes the configuration by copying the settings from a default config file, placed in the plugin's folder, to the Config.groovy. This is usually very useful during a plugin installation when you need to provide some default configuration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
includeTargets << grailsScript("_GrailsInit")

target(quickstart: 'Configures project to use [your plugin name] Plugin') {
    def configFile = new File("${basedir}/grails-app/conf/", 'Config.groovy')
    def defaultConfigFile = new File("${[your plugin name]PluginDir}/src/samples/",
        'conf/DefaultConfig.groovy')
    if (configFile.exists() && defaultConfigFile.exists()) {
       defaultConfigFile.eachLine { line ->
           configFile.append("\n${line}")
       }
       println '* Your grails-app/conf/Config.groovy has been updated.'
    } else {
       println '* Cannot update grails-app/conf/Config.groovy.'
    }
}

setDefaultTarget(quickstart)

If you're looking for a developer or considering starting a new project,
we are always ready to help!