Grails 3 - External config

2,701 views
Skip to first unread message

Benoit Hediard

unread,
Mar 8, 2015, 6:30:52 PM3/8/15
to grails-de...@googlegroups.com
Hi,

In the new Grails 3 documentation, there is no "Externalized configuration" info.
I tried to create an application.groovy and use grails.config.locations without success ;).

Is grails.config.locations not supported anymore?
What would be the equivalent?

Thanks!

Benoit

Graeme Rocher

unread,
Mar 9, 2015, 3:54:36 AM3/9/15
to grails-de...@googlegroups.com
Grails 3 uses Spring's property sources concept, so it will resolve
properties from the system, the environment and finally the
application.yml/application.groovy

So if you need to externalise configuration you can do so simply be
settings system or environment variables. Alternatively you can define
a different property source in your 'Application' class as follows
(untested pseudo code):


class Application extends GrailsAutoConfiguration implements EnvironmentAware {
@Override
void Environment(Environment env) {
def res =
getClass().classLoader.getResource('myConfig.groovy') // file in
grails-app/conf or loaded from where ever
if(res) {
def config = new ConfigSlurper().parse( res )
env.propertySources.addFirst( new MapPropertySource(config) )
> --
> You received this message because you are subscribed to the Google Groups
> "Grails Dev Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to grails-dev-disc...@googlegroups.com.
> To post to this group, send email to grails-de...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/grails-dev-discuss/5113725b-7302-4988-b908-d031dbc23cb5%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Graeme Rocher

Benoit Hediard

unread,
Mar 9, 2015, 4:17:12 AM3/9/15
to grails-de...@googlegroups.com
That’s great!
I’ll give it a try.

Thanks

You received this message because you are subscribed to a topic in the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/grails-dev-discuss/_5VtFz4SpDY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to grails-dev-disc...@googlegroups.com.

To post to this group, send email to grails-de...@googlegroups.com.

Ronny Løvtangen

unread,
Mar 9, 2015, 12:28:53 PM3/9/15
to grails-de...@googlegroups.com
BTW; is there a list of features that won't make it from Grails 2.x to 3.x?
Would be handy to figure out if an application could be easily upgraded or not.

The external config support is one issue that will affect all our applications, hopefully the pseudo code provided will work.

Ronny Løvtangen
> To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/CAO_wNodw9bFZ92aKm6rEtaxDup-tj%2BUZoKHHMvBhyTCqrSCMPA%40mail.gmail.com.

Clyde Balneaves

unread,
Mar 27, 2015, 9:39:22 AM3/27/15
to grails-de...@googlegroups.com
Pseudo to working code:

class Application extends GrailsAutoConfiguration implements EnvironmentAware {
    static void main(String[] args) {
        GrailsApp.run(Application)
    }

    @Override
    void setEnvironment(Environment environment) {
        //Set up Configuration directory
        def krakenHome = System.getenv('KRAKEN_HOME') ?: System.getProperty('KRAKEN_HOME') ?: "/opt/kraken"

        println ""
        println "Loading configuration from ${krakenHome}"
        def appConfigured = new File(krakenHome, 'KrakenConfig.groovy').exists()
        println "Loading configuration file ${new File(krakenHome, 'KrakenConfig.groovy')}"
        println "Config file found : " + appConfigured

        if (appConfigured) {
            def config = new ConfigSlurper().parse(new File(krakenHome, 'KrakenConfig.groovy').toURL())
            environment.propertySources.addFirst(new MapPropertySource("KrakenConfig", config))
        }
    }
}

Michael Baehr

unread,
Apr 2, 2015, 8:10:27 PM4/2/15
to grails-de...@googlegroups.com
Thanks Clyde, I was running into the same problem, and your code works.

It seems though that the environment{} logic is not supported - I don't see any configuration parameters that are defined inside the environment block. Do you have any idea how this can be enabled?

Thanks

Michael

Clyde Balneaves

unread,
Apr 6, 2015, 9:13:23 AM4/6/15
to grails-de...@googlegroups.com
Do you mean different logic depending on whether you are running in development or production mode?  I havent tried it (just started my port to Grails 3) but this is all I could find to do it from within the Application class.  Sorry for the slow reply, I should check gmail more often!

import grails.util.Environment

...

switch (Environment.current) { case Environment.DEVELOPMENT: configureForDevelopment() break case Environment.PRODUCTION: configureForProduction() break }

Clyde Balneaves

unread,
Apr 6, 2015, 9:15:13 AM4/6/15
to grails-de...@googlegroups.com
Of course if you meant the external config has environment specific properties, then I have no idea!  I've been loading different files based on the logic, or getting Puppet to modify the file based on the environment.


On Thursday, April 2, 2015 at 7:10:27 PM UTC-5, Michael Baehr wrote:

Michael Baehr

unread,
Apr 6, 2015, 1:15:38 PM4/6/15
to grails-de...@googlegroups.com
Yeah, environment specific properties is what I'm after. I'm also converting a production application to Grails 3, and I'm not sure if there are customers out there who have environment blocks in their configs. Guess I will have to find out the hard way ... ;)

Thanks for your support, your code was already very helpful!

Peyman P

unread,
May 21, 2015, 6:29:09 AM5/21/15
to grails-de...@googlegroups.com
here a yml version. it works for me:

class Application extends GrailsAutoConfiguration implements EnvironmentAware {
static void main(String[] args) {
GrailsApp.run(Application)
}

@Override
void setEnvironment(Environment environment) {
        String configPath = System.properties["appname.config.location"]
Resource resourceConfig = new FileSystemResource(configPath);
YamlPropertiesFactoryBean ypfb = new YamlPropertiesFactoryBean();
ypfb.setResources(resourceConfig);
ypfb.afterPropertiesSet();
Properties properties = ypfb.getObject();
environment.propertySources.addFirst(new PropertiesPropertySource("appname.config.location", properties))
}
}

build.gradle

bootRun {
jvmArgs = ['-Dappname.config.location=/path/to/config.yml']
}

Michael Jess

unread,
Sep 21, 2015, 5:33:53 AM9/21/15
to Grails Dev Discuss
Any idea on how to implement this as a plugin? I think this would be really useful to the community and I'd even consider maintaining it; the thing is just that even doWithSpring() appears to be run after the configuration has been loaded into the grailsApplication, and therefore changes are not visible.

Is there a lifecycle method for plugins where I could do this? This should happen before the app bootstrap files are executed.

Thanks,
Michael

Sami Mäkelä

unread,
Jan 21, 2016, 8:02:32 AM1/21/16
to Grails Dev Discuss
I did a quick hack to support grails.config.locations

import org.springframework.core.env.Environment
import grails.boot.config.GrailsAutoConfiguration
import org.springframework.core.env.MapPropertySource

class ApplicationConfigurationLoader {

private ApplicationConfigurationLoader() {}

public static load(GrailsAutoConfiguration application, Environment environment) {
if (application && environment) {
DefaultResourceLocator resourceLocator = new DefaultResourceLocator()
def applicationGroovy = application.getClass().classLoader.getResource('application.groovy')
if (applicationGroovy) {
def applicationConfiguration = new ConfigSlurper(grails.util.Environment.current.name).parse(applicationGroovy)
for (String configLocation in applicationConfiguration.grails.config.locations) {
def configurationResource = resourceLocator.findResourceForURI(configLocation)
if (configurationResource) {
def config = new ConfigSlurper(grails.util.Environment.current.name).parse(configurationResource.getURL())
environment.propertySources.addFirst(new MapPropertySource(configLocation, config))
}
}
}
}
}
}

@Override
void setEnvironment(Environment environment) {
ApplicationConfigurationLoader.load(this, environment)
}
Reply all
Reply to author
Forward
0 new messages