user1811107
user1811107

Reputation: 759

Grails - Cannot read externalized configuration values

I am developing a grails application. I want to externalize certain configuration values, so they can be specified on server startup.

In development I run the application using the following command to run the application

grails prod run-app -Dmy.property="secret"

At runtime I want to read the value in my controller using the following code

String myVal= System.getProperty("my.property")

When I check the value of myVal it is always null. I just want to be able to read the value of my.value consistently, pointers appreciated.

Note: I do not want to store values in Config.groovy. I have also tried System.getenv().get("my.property"), but that does not work either.

I have also referred to Externalizing Grails Datasource configuration.

Upvotes: 0

Views: 339

Answers (2)

Lalit Agarwal
Lalit Agarwal

Reputation: 2354

Well, the blog which you pointed out is correct. Below is what you could add in your config file.

if (new File("${userHome}/.${appName}/${appName}-config.groovy").exists()) {
  grails.config.locations = ["file:${userHome}/.${appName}/${appName}-config.groovy"]
  println("loading configuration from: :${grails.config.locations}")
} else {
    println("No external configuration file defined.....")
}

Add this to right at the top of your config.groovy file. The you could put your external config file as below.

Assuming my appname is "test"

/usr/home/myhome/.test/test-config.groovy

For dynamic reloading of config during changes in external config file you could look at this plugin: http://grails.org/plugin/external-config-reload

Upvotes: 1

mwaisgold
mwaisgold

Reputation: 106

Your problem is in the order of the parameters. All the "-D" params are java parameters so, if you check the java command that is running background does not recognize those params at the end. In short, what you should do is:

grails -Dmy.property="secret" prod run-app 

Upvotes: 0

Related Questions