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

前端 未结 2 1093
花落未央
花落未央 2021-01-02 03:37

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 c

相关标签:
2条回答
  • 2021-01-02 04:30

    I think you need to write something like

    val devTaskSetting = task <<= setting in dev map { s: String =>
       println("Setting in Dev is: " + s)
    }
    

    You can also define separate task keys, like this

    val devTask   = TaskKey[Unit]("task", "a simple task experiment") in dev
    val stageTask = TaskKey[Unit]("task", "a simple task experiment") in stage
    
    0 讨论(0)
  • 2021-01-02 04:38

    As discussed in How can i make an SBT key see settings for the current configuration?, you can probably use inConfig as follows.

    Change this:

    settings = Defaults.defaultSettings ++ Seq(taskTask)
    

    to this:

    settings = Defaults.defaultSettings ++
      Seq(taskTask) ++
      inConfig(dev)(Seq(taskTask)) ++
      inConfig(stage)(Seq(taskTask)) ++
      inConfig(prod)(Seq(taskTask))
    

    and voilà:

    $ sbt
    > task
    Setting is: default setting
    > dev:task
    Setting is: default setting
    > stage:task
    Setting is: stage setting
    > prod:task
    Setting is: prod setting
    

    If you're interested in digging deeper, inConfig is defined in sbt.Project ( http://harrah.github.io/xsbt/latest/api/index.html#sbt.Project$ ) as a function to "copy a subgraph of tasks/settings into different scopes" (as @MarkHarrah describes it). Also, take a look at http://eed3si9n.com/sbt-010-guide and scroll down to "changing the scopes" where the author explains how inConfig(conf)(ss) "scopes the settings ss in to conf only when it isn't scoped yet to a configuration".

    0 讨论(0)
提交回复
热议问题