0

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.

Community
  • 1
  • 1
user1811107
  • 729
  • 3
  • 18
  • 39

2 Answers2

1

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

Lalit Agarwal
  • 2,354
  • 1
  • 14
  • 18
  • I need to be able to deploy a single war file and not recompile every time values change..with your suggestion - it seems I may have to recompile again? Let me know if my assumptions are incorrect. Thx – user1811107 Jun 18 '14 at 16:28
  • Added a link in my answer for dynamic loading of config. – Lalit Agarwal Jun 19 '14 at 05:34
0

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 
mwaisgold
  • 106
  • 2
  • That did not work,usually command line parameters are mostly order agnostic. Can you point me to a resource where you saw the ordering etc. matters? Thx – user1811107 Jun 18 '14 at 16:19