Question

In my Config.groovy I have:

// Lots of other stuff up here...

environments {
    development {
        myapp.port = 7500
    }
    production {
        myapp.port = 7600
    }
}

fizz {
    buzz {
        foo = "Port #${myapp.port}"
    }
}

When I run my app via grails -Dgrails.env=development run-app, my web app spins up without errors, but then at runtime I see that the value of fizz.buzz.foo is "Port #[:]". I would expect it to be "Port #7500".

Why isn't Grails seeing my var?

Was it helpful?

Solution 2

Another way is to simply use variables within your config file like this:

def appPort = 7500

environments {
    production {
        appPort = 7600
        myapp.port = appPort
    }
}

fizz {
    buzz {
        foo = "Port #$appPort"
    }
}

And also, you don't need to send the -Dgrails.environment=development when you execute the run-app, it's the default one.

OTHER TIPS

You could probably get away with this if myapp.port were not in an environments block but that's a side effect of the way Config.groovy is processed rather than being intentional. And if you were to override myapp.port in an external config file then fizz.buzz.foo would still end up with the value from Config.groovy, not the override from the external.

You could make it a late-binding GString using a closure to pull the value from grails.util.Holders.config when fizz.buzz.foo is referenced rather than when it is defined:

foo = "Port #${-> Holders.config.myapp.port}"

This is different from "Port #${Holders.config.myapp.port}" which would attempt to access the config at the point where Config.groovy is being parsed.

If the value you're defining here is one that will ultimately end up defining a property of a Spring bean (for example many of the spring-security-core plugin configuration options become bean properties) then you may be able to do

foo = 'Port #${myapp.port}'

with single rather than double quotes. This causes the resulting config entry to contain the literal string ${myapp.port}, which will be resolved against the config by the Spring property placeholder mechanism when it is used as a bean property value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top