Play2 “include” directive override configuration w

2019-08-04 01:12发布

I have following conf files in my play2.1.0 application

  • application.conf
  • override.dev.conf
  • override.qa.conf
  • override.prod.conf

And there is a application.mode property in the application.conf file which will have either one of dev/qa/prod values.

application.conf also has a line to include env/mode specific conf files as override. This is what is not working with substitution.

Reason: To have the override properties in the env/mode specific conf files.

Referred: http://www.playframework.com/documentation/2.0/Configuration

If an unquoted include at the start of a key is followed by anything other than a single quoted string, it is invalid and an error should be generated.

No substitutions are allowed, and the argument may not be an unquoted string or any other kind of value.

Tried:

  1. Able to get the substitution done for another property but not for include like this

    my.prop="override."${?application.mode}".conf"

    The above outputs to override.dev.conf if application.mode=dev

  2. If I have something like below its not working and i suppose its what is expected as per the documentation reference.

    include "override."${?application.mode}".conf"

    Expected the above to include/override props in a file named override.dev.conf

Question:

  • Should this be a future enhancement or this is what is expected out of it?
  • What are the other ways to implement what I wanted?

Any help would be really appreciated.

2条回答
时光不老,我们不散
2楼-- · 2019-08-04 01:30

I prefer to override the GlobalSettings.onLoadConfig as described in PlayFramework 2 load different config according to current mode. It is done in Scala but it should be possible to do in Java as well.

It lets you overload configurations in a very nice way without the need to start the application with command line arguments, you still start the app with play run, play start, etc.

You should be able to use this method if you change your override.qa.conf to override.test.conf since qa is not a known mode in Play.

All shared settings in the application.conf and then override in the other ones.

查看更多
何必那么认真
3楼-- · 2019-08-04 01:34

We wanted to do something similar and the only way we got it to work was to reverse it.

In each environment we have a main-config.conf that has all of the configuration specific for that environment. Basically, what you are calling your override.[env].conf. The first line in each of those files is includes "application.conf" to merge in the default configuration for the application. So, application.conf has the general project configuration and the other files have the stuff specific to the environment.

To start your app you just tell it to use the environment-specific config file.

play -Dconfig.file=/path/to/main-config.conf start

The application will load main-config.conf which, in turn, includes the default application.conf from the project.

We actually also modify the build shell script (in the /framework directory, I believe) so it always specifies that config file parameter. That way we don't have to type that in when we're developing.

查看更多
登录 后发表回答