Play Framework 2.1:在 Build.scala 中使用 play.api.Configuration [英] Play Framework 2.1: Use play.api.Configuration in Build.scala
问题描述
在 Play Framework 的最佳答案中2:读取Build.scala中定义的应用版本号,建议在conf/application.conf
中指定应用版本号,在Build.scala
中加载通过play.api.Configuration
.我正在使用 Play 2.1-RC2
并在构建时收到以下错误消息:
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("."))
我认为这可能是由于 Play 2.1 构建依赖项必须指定为 SBT 的插件,并且 play.api.Configuration
不是 Play 的 SBT 插件的一部分.我猜我必须在 project/plugins.sbt
中包含 Play 的核心库,但我一直无法弄清楚如何.有什么想法吗?
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)
推荐答案
我在 2.1-RC2
中工作,如果你直接使用 typesafe 的配置库,没有 Play 的 Configuration
包装器.它是一个 Java API,所以它的使用与这个答案中描述的略有不同.
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.
在project/Build.scala
中导入库:
import com.typesafe.config._
并手动从文件加载配置.需要调用 resolve()
来解析替换.
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")
这篇关于Play Framework 2.1:在 Build.scala 中使用 play.api.Configuration的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!