3
votes

I'm looking at: https://github.com/typesafehub/config

Let's say I want to have a default configuration, e.g. reference.conf, and then I want to have dev/prod overrides (two different application.conf's), and then I also wanted to have host-specific overrides that inherited from both the application.conf and ultimately the default reference.conf. How would I do this?

e.g., I'm imagining a directory structure something like:

resources/reference.conf
resources/prod/application.conf
resources/prod/master.conf
resources/prod/slave.conf
resources/dev/application.conf
resources/dev/master.conf
resources/dev/slave.conf

Or maybe it would be resources/dev/master/application.conf?

Somewhere I would specify an environment, i.e. maybe extracted from the hostname the application was started on.

If the application was master.dev.example.com, I'm expecting I should be able to do something like:

getConfigurations("dev/master.conf").withDefaultsFrom(
    getConfigurations("dev/application.conf").withDefaultsFrom(
        getConfigurations("resource.conf"))

But I'm having a hard time understanding what exactly that would look like using the given library.

I see I could set a config.resource system property, but it looks like that would only allow for one level of overrides, dev-application.conf -> resources.conf, not something like master-node.conf -> dev-application.conf -> resources.conf.

I see a .withFallback method, but that seems to be if I wanted to mix two kinds of configuration in a single file, not to chain resources/files together.

1

1 Answers

6
votes

Use multiple withFallback with the configs that have the highest priority first. For example:

Config finalConfig = 
  ConfigFactory.systemProperties().
    withFallback(masterConfig).
    withFallback(applicationConfig).
    withFallback(referenceConfig)

Each of the configs like masterConfig would have been loaded with Config.parseFile. You can also use ConfigFactor.load as a convenience, but the parseXXX methods give you more control over your hierarchy.