Play Framework 2:阅读Build.scala中定义的应用程序版本 [英] Play Framework 2: Read the application version defined in Build.scala
问题描述
我使用Play Framework 2.0(2.0.3)。
我有一个Java项目,想要阅读Build.scala中定义的应用程序版本( appVersion
)。
I use the Play Framework 2.0 (2.0.3).
I have a Java project and want to read the application version (appVersion
) defined in Build.scala.
我已经看到的是,可以从提供给Global.java的 Application
对象中读取某些配置详细信息,但没有找到名为appVersion或类似的键。 。
What I already saw is that it's possible to read certain configuration details from the Application
object provided to Global.java, but didn't find a key called appVersion or similar.
推荐答案
您可以在 application.conf
中定义版本并让 Build.scala
读取值。我用版本号和应用程序名称做了这个。以下适用于Play 2.0,有一个更新的 Play 2.1的解决方案。
You can define the version in application.conf
and let Build.scala
read the value. I did this with the version number and application name. The following works in Play 2.0, there is an updated solution for Play 2.1.
在 project / Build.scala
中,加载配置并获取属性:
In project/Build.scala
, load the configuration and get the properties:
val conf = play.api.Configuration.load(new File("."))
val appName = conf.getString("app.name").getOrElse("unnamed application")
val appVersion = conf.getString("app.version").getOrElse("0.0.0")
在 conf / application.conf
中定义属性:
app.version = 1.0
app.name = My Application
最后在您的应用程序中,它可以通过
Finally in your application it will be accessible with
Play.application().configuration().getString("app.version")
配置语法有很多功能,所以你甚至可以去您的版本或应用程序名称更加疯狂:
The configuration syntax has quite some features, so you can even go a little more crazy with your version or application names:
app {
major = 1
minor = 2
revision = 3
version = ${app.major}.${app.minor}.${app.revision}
name = My Application ${app.major}.${app.minor}
}
这篇关于Play Framework 2:阅读Build.scala中定义的应用程序版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!