How to override SettingKey to calculate another SetKey parameter in sbt?

I want to override the value of SettingKey b only when calculating SetKey a1 .

 import sbt._ import sbt.Keys._ object Build extends Build { val a1Key = SettingKey[String]("a1", "") val a2Key = SettingKey[String]("a2", "") val bKey = SettingKey[String]("b", "") lazy val rootProject = Project("P", file(".")).settings( bKey := "XXX", a1Key <<= bKey((x) => ">>>"+x+"<<<"), a2Key <<= bKey((x) => ">>>"+x+"<<<") ) .settings( bKey in a1Key := "YYY" //providing custom value in setting scope ) } 

Current result

 > a1 [info] >>>XXX<<< > a2 [info] >>>XXX<<< > b [info] XXX 

... but I try to see YYY as the value of a1 :

 > a1 [info] >>>YYY<<< > a2 [info] >>>XXX<<< > b [info] XXX 

An example of a better real world than the above is when you want to add some resources to your assembly only in the runtime configuration and some other resources when the application is packed. For example, creating shared resources for a GWT application served by a server during development and during production is different. It would be nice, for example, to configure resource-directories for run and package tasks.

+4
source share
1 answer

You need to set a1Key and a2Key so that bKey canceled bKey :

 lazy val rootProject = Project("P", file(".")).settings( bKey := "Fnord", a1Key <<= (bKey in a1Key)(x => ">>>" + x + "<<<"), a2Key <<= (bKey in a2Key)(x => ">>>" + x + "<<<") ).settings( bKey in a1Key := "Meep" ) 

Thus, calculating a1Key will use the more specific Meep value, and when calculating a2Key sbt will "look" for the definition of bKey in a2Key , and then, since it does not "find" it, it returns to the more general bKey (in the default area), which defined and therefore used.

Edit: this, unfortunately, means that if someone providing definitions for the a1Key and a2Key parameters also explicitly provides the required extension points (in the form of dependencies with a configuration binding), you cannot override the dependencies. That, at least, is how I understand it.

+2
source

All Articles