sbt-buildinfo 插件:如何将编译时有效的 java 版本添加到生成的 BuildInfo.scala?

sbt-buildinfo plugin: How can I add the java version in effect at compile time to the generated BuildInfo.scala?

构建信息插件对我来说工作正常,但我想知道我是否可以将生成它的 JDK 版本添加到生成的 BuildInfo.scala 源文件中。

您可以添加一个SettingKey,例如:

lazy val jdkVersion: SettingKey[String] = SettingKey[String]("jdkVersion", "JDK Version")

lazy val root = (project in file(".")).
  enablePlugins(BuildInfoPlugin)
  .settings(
    ThisBuild / jdkVersion := System.getProperty("java.version"),
    buildInfoKeys := Seq[BuildInfoKey](name, version, scalaVersion, sbtVersion, jdkVersion),
    buildInfoOptions += BuildInfoOption.ToJson,
    buildInfoPackage := "myproject.version"
  )

下面应该做的

buildInfoKeys += BuildInfoKey.action("javaVersion")(sys.props("java.version"))

您可能想要捕获的四个属性是 "java.vm.name""java.vm.version""java.version""java.runtime.version"