问题描述
在 Play框架的最佳答案中2:阅读Build.scala 中定义的应用程序版本,建议在conf/application.conf
中指定应用程序版本号,并通过play.api.Configuration
加载到Build.scala
中.我正在使用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)
推荐答案
如果您直接使用typesafe的配置库而不使用Play的Configuration
包装器,则可以在2.1-RC2
中工作.这是一个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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!