3

I've had a look around but it's not very clear to me how I can configure a set of environment specific variables for my Play framework application.

As an example, I would like to use an in memory database like h2 for local development but when I move to production or my pre-production environment I would like to be connecting to a postgres database.

How do I configure my app so that it will use the variables relevant to the environment it is being deployed to? This is a Scala Play app.

3 Answers 3

7

One option (as documented in the excellent play docs), is to specify conf files during app startup.

Using -Dconfig.resource will search for an alternative configuration file in the application classpath (you usually provide these alternative configuration files into your application conf/ directory before packaging). Play will look into conf/ so you don’t have to add conf/.

$ /path/to/bin/<project-name> -Dconfig.resource=prod.conf

Using -Dconfig.file you can specify an environment specific configuration file not packaged into the application artifacts:

$ start -Dconfig.file=/opt/conf/prod.conf

Using -Dconfig.url you can also specify a configuration file to be loaded from any URL:

$ start -Dconfig.url=http://conf.mycompany.com/conf/prod.conf

Note that you can always reference the original configuration file in a new prod.conf file using the include directive, such as:

include "application.conf"

key.to.override=blah
Sign up to request clarification or add additional context in comments.

1 Comment

Pardon me for resurrecting an old thread, but I wanted to add that, in my experience, it is safer to put the production configuration in the default application and the dev configuration in the override. If, God forbid you make a deployment mistake, it can be very bad to accidentally deploy the dev configuration, whereas the worst that can happen the other way is your developers get annoyed they started the app in the wrong mode. This presumes you have good security on your database and your devs can't accidentally make data changes on production running in the wrong mode.
1

You can use different configuration files by overriding onLoadConfig method of the Global object like:

object Global extends GlobalSettings {
   override def onLoadConfig(config: Configuration, path: File, classloader: ClassLoader, mode: Mode.Mode): Configuration = {
    val fileName = s"application.${mode.toString.toLowerCase}.conf"
    config ++ Configuration(ConfigFactory.load(fileName))
  }
}

This way you have 'application.test.conf' for test mode and 'application.dev.conf' for develop mode whereas you can use another config file in production via '-Dconfig.file' parameter.

Comments

0

You can have Puppet or some similar tools to generate the needed parameters in environment.conf and place in a dedicated directory.

Then in application.conf, at the end of the file, have this:

include "file:///[your directory...]/environment.conf"

to override any testing or local values (e.g.DB parameteres) listed above

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.