How to make a sbt task use a specific configuration scope?

Viewed 5032

I have a task lazy val task = TaskKey[Unit] that takes a lazy val setting = SettingKey[String] as input. I also have three different, independent config scopes (config("dev"), config("stage"), config("prod")) and a build.sbt file that specifies different values for setting for each of the config scopes (setting in stage := "foo" ... ).

I expected that calling task using the config scope prefix would make the task use the values from the respective config scope (e.g. >dev:task would make the task use the setting values from dev, using command >stage:task would make the task use the setting values from stage, ...). However, this does not seem to work.

How can I force task to use the settings from a specific config scope?

build.sbt:

setting := "default setting"

setting in stage := "stage setting"

setting in prod  := "prod setting"

Build.scala:

import sbt._
import Keys._

object TaskBuild extends Build {
  val setting = SettingKey[String]("setting", "a simple string setting")

  val task = TaskKey[Unit]("task", "a simple task experiment")

  val taskTask = task <<= setting map { s: String =>
    println("Setting is: " + s)
  }

  lazy val dev = config("dev") describedAs("dev environment settings")
  lazy val stage = config("stage") describedAs("stage environment settings")
  lazy val prod = config("prod") describedAs("prod environment settings")

  lazy val root = Project(
    "project",
     file("."),
     settings = Defaults.defaultSettings ++ Seq(taskTask)
  )
  .configs(dev, stage, prod)
}
2 Answers
Related