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

前端 未结 4 483
闹比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:00

    I use the SBT BuildInfo plugin for this purpose:

    import sbtbuildinfo.Plugin._
    
    val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA, settings = Defaults.defaultSettings ++ buildInfoSettings).settings(
    
      buildInfoKeys := Seq[Scoped](name, appVersion, scalaVersion, sbtVersion),
      buildInfoPackage := "org.foo.bar",
    
      ...
    
    )
    

    This generates an org.foo.bar.BuildInfo object which you can then call from the source code:

    org.foo.bar.BuildInfo.version
    

    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.

提交回复
热议问题