Play Framework - How to maintain configuration files for different environments?
Asked Answered
A

1

5

For my Play 2.2/Scala application (built with SBT), I would like to deploy different configuration files depending on the environment I'm deploying to (e.g. to couple a deployment with a particular database server). How does one create different variants of the application's configuration file (conf/application.conf) for different deployment targets? Hopefully variants can be generated from a base version?

What I'm used to from .NET is to have a base configuration file (Web.config), which undergoes a certain transformation depending on the profile one is deploying (e.g. Production). Does one use a similar technique in the Play/Scala world?

Accusatorial answered 18/2, 2014 at 21:11 Comment(0)
S
18

Alternative configuration files are covered in Play's documentation quite well in section Specifying alternative configuration file.

In short - in application.conf you place default configuration of your app, and additionally you need to create additional files for you environment(s) ie. life.conf, dev.conf etc. In these files you first need to include application.conf (which will read whole default configuration) and next just overwrite only parts which have to be changed - ie. DB credentials, it could be dev.conf:

include "application.conf"

db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:alternative-database-for-dev-testing"
db.default.user=developer
db.default.password="developerpass"

So finally you start your application (after dist) as

./start -Dconfig.resource=dev.conf

or with the Play console

play -Dconfig.resource=dev.conf run

Several tips:

  • It's good idea to do not place your 'life' DB credentials in default application.conf file, if some dev will forget to include his dev.conf he won't damage the production DB, instead you should put it in prod.conf.
  • Also these additional configs shouldn't be placed in any VCS (ie. git) repository - creating them directly on target machine (and ignoring in repository) give you sure, that people who shouldn't know the life database credentials won't see it.
  • It's also possible to use remote alternative config file, which can be useful ie. when you deploying several instances of the same app ie. on several hosts in the cloud.
  • Each dev can has own config file ie dev_aknuds1.conf, dev_biesior.conf etc, so you can ignore them with one pattern dev_*.conf in repo.
  • Finally you can just create a shell script (unix) or bat file (Windows) to start using choosen config file like start_dev.sh, run_dev.sh etc. so you won't need to write -Dconfig.resource=... each time
Schoolman answered 18/2, 2014 at 22:30 Comment(3)
Yes, but it doesn't say how to generate configuration file variants, which is the core problem here.Accusatorial
Isn't that exactly what biesor just described to you? Include will base a config file on another only overriding the settings you want to be different.Kienan
@Kienan The answer has been expanded since I first commented. I also see now that the 'include' technique is mentioned in the documentation; it's easy to spot now that I'm aware of it, I wish it were more obvious for someone new to Play... All in all, this does look like a great solution.Accusatorial

© 2022 - 2024 — McMap. All rights reserved.