sbt : plugins.sbt 内的 scalaVersion 值

sbt : scalaVersion value inside plugins.sbt

有没有办法在 plugins.sbt 中获取 scalaVersion 设置的值?

我尝试在 plugins.sbt 中执行以下操作:

logLevel := Level.Warn

resolvers += "Typesafe repository" at "https://repo.typesafe.com/typesafe/releases/"
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.8.7")

val version = scalaVersion.value

这是我得到的错误:

error : value can only be used within a task or setting macro, such as :=, +=, ++=, Def.task, or Def.setting.

我想在 plugins.sbt 中实现的是检索 scalaVersion 设置的值并将该 val 与 addSbtPlugin 一起使用,如下所示:

addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.8.7").filter(_ => version == "2.12")

此错误表明您无法在任务或设置执行范围之外读取设置值,因此您不应仅声明字段,而应在另一个任务或设置中使用,例如:

lazy val customVersion = settingKey[String]("Custom version for sake of example")

customVersion := {
  //`.value` referenced inside `customVersion` settings declaration, so sbt can evaluate customVersion in scope of other settings/task evaluations.
  "Custom version" + scalaVersion.value
}