java Play Framework 2:读取Build.scala中定义的应用版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12369967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:36:47  来源:igfitidea点击:

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

javaplayframeworkplayframework-2.0version

提问by peter

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.

我使用 Play Framework 2.0 (2.0.3)。我有一个 Java 项目,想读取appVersionBuild.scala 中定义的应用程序版本 ( )。

What I already saw is that it's possible to read certain configuration details from the Applicationobject provided to Global.java, but didn't find a key called appVersion or similar.

我已经看到的是,可以从Application提供给 Global.java的对象中读取某些配置详细信息,但没有找到名为 appVersion 或类似的键。

回答by kapex

You can define the version in application.confand let Build.scalaread 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.

您可以定义版本application.conf并让Build.scala读取值。我用版本号和应用程序名称来做到这一点。以下适用于 Play 2.0,Play 2.1有更新的解决方案

In project/Build.scala, load the configuration and get the properties:

在 中project/Build.scala,加载配置并获取属性:

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.confdefine the properties:

conf/application.conf定义属性:

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}
}

回答by Manuel Bernhardt

I use the SBT BuildInfo pluginfor this purpose:

为此,我使用了SBT BuildInfo 插件

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.BuildInfoobject which you can then call from the source code:

这将生成一个org.foo.bar.BuildInfo对象,然后您可以从源代码调用该对象:

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.

您还可以在构建中定义自定义键并将它们添加到 buildInfoKeys,如果您的构建变得更复杂,这将非常有用。

回答by ndeverge

You can get the current version of Play by using:

您可以使用以下命令获取当前版本的 Play:

play.core.PlayVersion.current();

回答by maestr0

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

这是您如何获得在build.sbt 中定义的 Play 应用程序版本和应用程序名称

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.

请注意,这仅适用于 PROD 模式。在开发模式下,SBT 与应用程序共享一个 JVM 实例,这些调用返回不同的东西。

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.

在这种情况下,Application 类是在您的项目中定义的类。它可以是您项目中的任何类。

UPDATE

更新

I noticed that this method doesn't work out of the box for Play >=2.4.x

我注意到这个方法在 Play >=2.4.x 时不能开箱即用

To fix the problem add this to your build.sbt

要解决此问题,请将其添加到您的 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.

这两个属性将附加到构建中的 MANIFEST.FM 文件,以便可以从代码中读取包标题和版本。

fyi: I use SBT native packager

仅供参考:我使用 SBT 原生打包器

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