Play Framework 2.1: Use play.api.Configuration in

2019-01-13 19:07发布

问题:

In the top answer to Play Framework 2: Read the application version defined in Build.scala it's suggested that the application version number be specified in conf/application.conf and loaded in Build.scala through play.api.Configuration. I'm using Play 2.1-RC2 and getting the following error message when building:

[error] [...]/project/Build.scala:7: object Configuration is not a member of package play.api 
[error] val conf = play.api.Configuration.load(new File("."))

I think this might be caused by the fact that with Play 2.1 build dependencies have to be specified as plugins to SBT, and play.api.Configuration is not part of Play's SBT plugin. I'm guessing I have to include Play's core libraries in project/plugins.sbt, but I haven't been able to figure out how. Any ideas?

(note: Would have made this a comment in the original question if I had enough rep points)

回答1:

I works in 2.1-RC2 if you use typesafe's config library directly, without Play's Configuration wrapper. It's a Java API, so it is used slightly different than described in this answer.

In project/Build.scala import the library:

import com.typesafe.config._

and load the configuration from the file manually. Calling resolve() is needed to resolve substitutions.

val conf = ConfigFactory.parseFile(new File("conf/application.conf")).resolve()

val appName    = conf.getString("app.name")
val appVersion = conf.getString("app.version")