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

后端 未结 6 1502
栀梦
栀梦 2020-12-07 18:21

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.         


        
6条回答
  •  悲&欢浪女
    2020-12-07 18:45

    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

提交回复
热议问题