将设置提取到顶级val

时间:2018-02-27 19:42:29

标签: sbt

假设:

resourceGenerators in Compile += Def.task {
  val jar = (update in Compile).value
    .matching((_: ModuleID) == nemesisProto)
    .head
  IO.unzip(jar, (resourceManaged in Compile).value / "protobuf").toSeq
}.taskValue

PB.protoSources in Compile := Seq((resourceManaged in Compile).value / "protobuf")

是否可以将(resourceManaged in Compile).value / "protobuf"重构为公共场所?

我尝试将它分配给val:

val protobufResourceFile = (resourceManaged in Compile).value / "protobuf"

resourceGenerators in Compile += Def.task {
  val jar = (update in Compile).value
    .matching((_: ModuleID) == nemesisProto)
    .head
  IO.unzip(jar, protobufResourceFile).toSeq
}.taskValue

PB.protoSources in Compile := Seq(protobufResourceFile)

仅出现以下错误:

 error: `value` can only be used within a task or setting macro, such as :=, +=, ++=, Def.task, or Def.setting.
val protobufResourceFolder = (resourceManaged in Compile).value / "protobuf"
                                                          ^

1 个答案:

答案 0 :(得分:1)

几乎。正如错误消息所述,您无法在sbt dsl之外解包值。所以,这样的事情通常是使用SettingKey

完成的
val protobufResourceFile = settingKey[File]("Protobuf resource file ...")

protobufResourceFile := (resourceManaged in Compile).value / "protobuf"

resourceGenerators in Compile += Def.task {
  val jar = (update in Compile).value
    .matching((_: ModuleID) == nemesisProto)
    .head
  IO.unzip(jar, protobufResourceFile.value).toSeq
}.taskValue

PB.protoSources in Compile := Seq(protobufResourceFile.value)

虽然,在这种特殊情况下,它可能有点过分。