Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play Framework 2: Read the application version defined in Build.scala

Tags:

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.

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.

like image 565
peter Avatar asked Sep 11 '12 12:09

peter


1 Answers

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.

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

In conf/application.conf define the properties:

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}
}
like image 155
kapex Avatar answered Sep 23 '22 08:09

kapex