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.
This is how you can get Play application version and application name defined in your build.sbt
Notice this only works in PROD mode. In dev mode SBT shares a JVM instance with the application and those calls return something different.
In this case Application class is a class defined in your project. It can be any class from your project.
UPDATE
I noticed that this method doesn't work out of the box for Play >=2.4.x
To fix the problem add this to your build.sbt
The two properties will be appended to MANIFEST.FM file in your build so the package title and version can be read from the code.
fyi: I use SBT native packager
You can get the current version of Play by using:
You can define the version in
application.conf
and letBuild.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:In
conf/application.conf
define the properties:Finally in your application it will be accessible with
The configuration syntax has quite some features, so you can even go a little more crazy with your version or application names:
I use the SBT BuildInfo plugin for this purpose:
This generates an
org.foo.bar.BuildInfo
object which you can then call from the source code:You can also define custom keys in the build and add them to the buildInfoKeys, which is quite useful if your build gets more complex.