22

With Play 1.2, I can prefix the configuration keys with the framework ID or application mode as follows:

# Production configuration
%prod.http.port=80
%prod.application.log=INFO
%prod.application.mode=prod

But it doesn't seem to work with 2.0.

Is there any way to make it work?

1

6 Answers 6

43

Play 2 doesn't force you to use any particular method to manage your environments. But it provides you with powerful and flexible tools to implement it yourself according to the needs of your project.

For instance a common pattern is keeping common environment settings in one file and having environment-specific overrides in other files. In order to do that you will need a custom Global object (you can put it right into the ./app/Global.scala). The following code is valid as of Play 2.1.1 (Scala 2.10):

import java.io.File
import play.api._
import com.typesafe.config.ConfigFactory

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

Now you can put application.dev.conf, application.test.conf, and application.prod.conf into your ./conf with environment specific overrides (while keeping common settings in application.conf).

In this sample we're relying on the Play's own mode which usually makes sense, but you can be as granular as you want and use environment variables or whatever you feel like.

See also: Typesafe Config

9
  • This does not seem to be working for me. When I use this code, anything that is not set in the mode override file seems to get set to Play defaults and not what is in application.conf. I am using play 2.2.1 built with Scala 2.10.2 (running Java 1.7.0_45). Can anyone else confirm this behavior? I think it has to do with ConfigFactory.load.
    – mushroom
    Jan 12, 2014 at 21:39
  • Looking at the documentation for ConfigFactory, I see this for ConfigFactory.load(String): Loads an application's configuration from the given classpath resource or classpath resource basename, sandwiches it between default reference config and default overrides, and then resolves it.
    – mushroom
    Jan 12, 2014 at 22:00
  • When I switched to ConfigFactory.parseFile, it started working as expected. Don't know if that is the best solution. There might be options you can give to ConfigFactory.load so that it doesn't merge your overrides with the defaults.
    – mushroom
    Jan 12, 2014 at 22:10
  • 1
    Yes, that is what I did. Please read my other comments. I said how I got it to work with my version.
    – mushroom
    Jan 13, 2014 at 11:16
  • 1
    This seems to be deprecated now, according to docs: "This method does not do anything. Instead, specify configuration in your config file or make your own ApplicationLoader (see GuiceApplicationBuilder.loadConfig)"
    – redwulf
    Jan 18, 2016 at 14:30
13

I have had this question for a long time as well and below is the best approach I've learned so far, I got a hint when asking a similar question on the Play 2 google group.

In you application.config use the following syntax to override a configuration value when a system parameter is present:

# Local machine fallback URI
mongodb.uri="mongodb://192.168.56.101:27017/application"
# Env variable override
mongodb.uri=${?MONGOLAB_URI}

The question mark means that you don't override with the env variable if it is not set. If you just use ${MONGOLAB_URI} you expect the variable to be set and, I assume, you get an exception of some kind if it is not set.

For completeness, here is an example of how you'd read the value:

lazy val mongoUri = current.configuration.getString("mongodb.uri").getOrElse("mongodb:///")

With this approach there is one caveat: make sure you keep your system param configuration in a SCM of some kind.

2
  • Hi Magnus - how would I create a new env var in the application.conf that equals mongodb.uri but with /a.xml added to it? Oct 31, 2013 at 2:42
  • @KevinMeredith: I know this is a very old comment but.. you can use something like my_new_cfg = ${mongodb.uri}"/a.xml". This is, of course, assuming you are using the HOCON format for typesafe config. Aug 30, 2017 at 9:16
6

You have to define a different configuration file with the appropriate properties

http://www.playframework.org/documentation/2.0/Configuration

There is also a inclusion mechanism that helps you define default values in application.conf and overrides only what is needed for production

3
  • 2
    I tried to do this, the only problem im facing is that when I have db.default.driver, db.default.url, db.default.user, etc in both the application.conf and prod.conf, some reason with prod.conf it never connects. But when I REMOVE the db settings from the application.conf and run prod.conf, it works. Is there a reason for this?
    – KVISH
    Jun 7, 2012 at 2:39
  • 1
    btw, i'm having prod.conf including application.conf
    – KVISH
    Jun 7, 2012 at 2:40
  • 2
    Yeah, it seems like Play 2 doesn't let you override default database parameters.
    – cdmckay
    Aug 23, 2013 at 23:51
4

If you want to be indepentent from restrited scala run modes you can use JVM properties. Put modified @coffesnake example to ./app/Global.scala :

import java.io.File

import play.api._
import com.typesafe.config.ConfigFactory

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

Next run play play

and start app with environment param run -Denvironment=prod-server1

Don't join both command, it doesn't work then

Global configuration will be overrided with environment specific properties from file:

./conf/application.prod-server1.conf

EDIT:

From time perspective I saw this workaround is unnecessary. It is better to use Play build-in config loading mechanism -Dconfig.file=/etc/play-application/foo-production.conf

1

I'm using this solution:

in application.conf I've defined the default configurations, in myUsername.conf I've included the default configuration and overridden the user specific configuration:

include "application.conf"
logger.application=DEBUG

then in Global.java I've loaded the user specific configuration (if exists):

public Configuration onLoadConfig(Configuration config, File path,
        ClassLoader classloader) {

    String username = System.getProperty("user.name");
    File confFile = new File(new File(path, "conf"), username + ".conf");

    if (confFile.exists()) {
        Logger.info("configuration file {} found", confFile.getName());
        return new Configuration(ConfigFactory.load(confFile.getName()));
    } else {
        Logger.info(
                "configuration file {} not found, using default application.conf",
                confFile.getAbsolutePath());
        return null;
    }

}
0

We just have multiple application.conf files, the default is used during development and we have a prod-applicaton.conf that we simply copy into place when we deploy.

1
  • this works fine but it requires a team to keep both of those files in sync with each other
    – Jack Chi
    Jan 27, 2014 at 20:32

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.