Tuesday, December 11, 2012

Modularize/partition Grails config files

If you find that your Config.groovy is getting large, you can split it into multiple files using the same mechanism that might already be using to include an external file. So your Config.groovy may already have something like the following defined:

One might expect files in the conf directory that follow the convention of ending in Config.groovy of being automatically included but this in not the case. I imagine the reason is a question of precedence - and a lack of clarity thereof. If two files in the conf directory ending in Config.groovy defined the same property which one would win? So only the Config.groovy file is auto-read by Grails, and within this file you can define other Config files to include. It's not very convention over configuration, but you can understand why.

The files to include can be defined in one of two lists. grails.config.locations and grails.config.default.locations. Files defined in grails.config.locations are parsed after the property definitions of Config.groovy, and files defined in grails.config.default.locations are parsed before. Where the same property is defined twice, the last one parsed wins.

You might see a number of suggestions on how to modularize a config but the only one I found to work was to place your "partial configs" in the src/java directory and then define them with in the locations list with a classname prefix.

No comments:

Post a Comment