Play Framework 2:读取Build.scala中定义的应用版本 [英] Play Framework 2: Read the application version defined in Build.scala

查看:22
本文介绍了Play Framework 2:读取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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆