4

I have a PlayFramework application, and there is a name and version in the build.sbt.

If I run the play console, I can access this information by typing name or version.

How can I get this information from Java code inside the application? I can't seem to find any useful methods in Play.application() and I have not been able to find this in the documentation.

Eugene Yokota
  • 94,654
  • 45
  • 215
  • 319
Eric Wilson
  • 57,719
  • 77
  • 200
  • 270

1 Answers1

4

For this you can use the SBT plugin BuildInfo:

Add to project/plugins.sbt:

addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.2.5")

And to build.sbt:

buildInfoSettings

sourceGenerators in Compile <+= buildInfo

buildInfoKeys := Seq[BuildInfoKey](name, version, scalaVersion, sbtVersion)

buildInfoPackage := "hello"

Now you can access the build information with static methods:

package controllers;

import play.*;
import play.mvc.*;

import views.html.*;

public class Application extends Controller {

    public static Result index() {
        return ok(hello.BuildInfo.name());//the name of the application as output
    }

}
Schleichardt
  • 7,502
  • 1
  • 27
  • 37
  • 1
    I see that this works, but what in the world is `hello`? Is it a static member of `Controller`? What makes it available in the `index` method? – Eric Wilson Nov 05 '13 at 13:36
  • You can set the package of the genarated class in build.sbt with buildInfoPackage := "hello" – Schleichardt Nov 18 '13 at 08:43