Overriding configuration with environment variable

2019-04-23 09:27发布

Using typesafe config, how do I override the reference configuration with an environment variable? For example, lets say I have the following configuration:

foo: "bar"

I want it to be overriden with the environment variable FOO if one exists.

2条回答
再贱就再见
2楼-- · 2019-04-23 09:48

If I correctly understood your question, the answer is here. You can do

foo: "bar"
foo: ${?FOO}
查看更多
闹够了就滚
3楼-- · 2019-04-23 09:49

If you have to use environment variables and if their naming is consistent with config names you can use a bash script like this to automatically convert from your environment vars to JVM cmd args. These -D JVM args will override Typesafe Config values. Example:

# export my_PROP1=1
# export my_PROP2=2
#
# props=$(env | grep my_ | awk '{print "-D"$_}' ORS=' ')
#
# echo "JVM executable command is: java $props some.jar"
JVM executable command is: java -Dmy_PROP2=2 -Dmy_PROP1=1  some.jar

Convert upper to lower case, do substring operations on env vars as you please if they don't directly map to your config values.

查看更多
登录 后发表回答