Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

playframework 2.0 - How to manage application.conf in several environments with play 2.0?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...