Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

java - Play Framework 2.1: Use play.api.Configuration in Build.scala

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)

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

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")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...