Helix112
Helix112

Reputation: 306

sbt : scalaVersion value inside plugins.sbt

Is there a way to get the value of scalaVersion setting inside plugins.sbt ?

I tried to do the following inside 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

And this is the error I get :

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

What I want to achieve inside plugins.sbt is to retrieve the value of scalaVersion setting and use that val with addSbtPlugin like the following :

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

Upvotes: 0

Views: 317

Answers (1)

Ivan Kurchenko
Ivan Kurchenko

Reputation: 4063

This error says that you can't read settings value outside of task or setting execution scope, so instead of just declaring field, you should use inside another task or settings, e.g. :

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
}

Upvotes: 1

Related Questions