Playframework settings depending on environment

前提是你 提交于 2019-12-03 01:08:48

I found the most robust way to specifiy this in a cross-platform compatible manner is to include it directly in the Build.scala:

val main = play.Project(appName, appVersion, appDependencies).settings(
    javaOptions in Test += "-Dconfig.file=conf/test.conf",
    ...
)

Bonus: configure once and forget ;-)

Another approach is to override method on GlobalSettings / Global named onLoadConfig and that enables you to have control where your app will look for your configuration.

So in one of our application I have this setup below for my conf/ folder.

 conf/application.conf --> configurations common for all environment
 conf/dev/application.conf --> configurations for development environment
 conf/test/application.conf --> configurations for testing environment
 conf/prod/application.conf --> configurations for production environment

With that, you are able to implement inheritance like setup for configuration, you have common and 3 others for specific environment mode.

The code inside your onLoadConfig method should just load main configuration and set correct fallback configuration specific to your environment then return the configuration instance like below:

**return new Configuration(baseConfig.withFallback(envConfig));**

Try check this blog post for complete snippet of the code.

I hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!