Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play - How to use different configuration files for Dev/Prod?

I have a project with the following configuration

  • Commons.conf
  • Application.conf for development
  • Production.conf for production

Both application.conf and production.conf include commons.conf

I have to build the application using dist task to generate the zip file, then start it using the projectname.bat file provided.

I added javaOptions ++= Seq("-Dconfig.file=conf/production.conf") in build.sbt but it seem that did not working. I cannot launch the application on command line because i did want it to be done automatically. What's the better way to do separate .conf file configuration for development and production and have dist task taking the production ones?

like image 248
emmea90 Avatar asked Dec 10 '22 13:12

emmea90


1 Answers

On startup of your application, Play is automatically configured to load application.conf from the classpath. To override this with an alternative you need to specify this on startup.

Note that whether you are deploying to Windows and starting the app with a .bat, or Linux with a .sh you are usually executing that file as follows:-

$ target/universal/stage/bin/<project-name>

The above will execute your .bat file (if you are on Microsoft that is).

Specifying an alternative config file (couple of options)

So if you want to have separate configuration files for prod/dev then use the default application.conf for dev and use a production.conf for prod.

If you put application.conf inside conf then you would specify it as follows:-

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

If application.conf is not packaged with your app (ie. you have not put it in conf then you need to specify full path using.

$ target/universal/stage/bin/<project-name> -Dconfig.file=/full/path/to/conf/production.conf

An alternative approach (inheriting and overriding application.conf)

Include application.conf in prod.conf and just override what you need to for prod Note that you can include configuration inside another, so what I usually do is specify a prod.conf like this:-

include "application.conf"

key.to.override=blah

Then specify prod.conf when you start the app and application.conf will automatically be included. You can then just specify the properties in prod.conf that override those in application.conf for your production environment.

More

Not that you can also use system properties and environment variables to override settings in application.conf. I encourage you to take some time to read the docs to understand the options more fully.

like image 182
jacks Avatar answered Dec 13 '22 04:12

jacks