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

前端 未结 4 482
闹比i
闹比i 2020-12-03 01:33

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

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-03 02:11

    This is how you can get Play application version and application name defined in your build.sbt

    name := "myApp"
    version :="1.0.4" 
    

    Notice this only works in PROD mode. In dev mode SBT shares a JVM instance with the application and those calls return something different.

    Application.class.getPackage().getImplementationTitle());     // returns "myApp"
    Application.class.getPackage().getImplementationVersion());    // returns "1.0.4"
    

    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

    packageOptions += Package.ManifestAttributes(
      "Implementation-Version" -> (version in ThisBuild).value,
      "Implementation-Title" -> name.value
    )
    

    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

    addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.0.3")
    

提交回复
热议问题