如何选择子项目在运行时打包(使用ScopeFilter)?

时间:2014-06-12 10:20:08

标签: scala sbt

我正在编写一个任务,将几个但不是所有子项目打包在一起,并使用排除设置和task.all(scope filter)来选择项目。然而,sbt坚持说References undefined settings at runtime

这是 project / build.scala 我使用后跟错误我需要你的建议:

import sbt._
import Keys._

object build extends Build {
  lazy val root = Project(
    id = "root",
    base = file("."),
    aggregate = Seq(a,b),
    settings = Seq(
      exclude := Seq(a),
      module := moduleImpl.value,
      modules := modulesImpl.value
    )
  )

  lazy val a = Project(
    id = "a",
    base = file("a")
  )

  lazy val b = Project(
    id = "b",
    base = file("b")
  )

  val exclude = settingKey[Seq[ProjectReference]]("excludes")
  val module = taskKey[String]("module")
  val modules = taskKey[Seq[String]]("modules")

  def moduleImpl = Def.task {
      projectID.value.organization
  }

  def modulesImpl = Def.taskDyn {
    module.all(ScopeFilter(inAggregates(ThisProject) -- inProjects(exclude.value: _*)))
  }
}

这是我真正想要摆脱的错误:

> show modules
[trace] Stack trace suppressed: run last root/*:modules for the full output.
[error] (root/*:modules) sbt.Init$RuntimeUndefined: References to undefined settings at runtime.
[error] Total time: 0 s, completed 2014-06-12 16:48:05

有什么想法解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

sbt抱怨说,因为你所使用的任务没有在子项目中定义。

解决方案不是在module,而是在moduleImpl

上调用它
lazy val moduleImpl = Def.task {
  projectID.value.organization
}

def modulesImpl = Def.taskDyn {
  moduleImpl.all(ScopeFilter(inAggregates(ThisProject) -- inProjects(exclude.value: _*)))
}

PS。当你这样做

Project(
  id = "root",
  base = file("."),
  aggregate = Seq(a,b),
  settings = Seq(
    exclude := Seq(a),
    module := moduleImpl.value,
    modules := modulesImpl.value
  )
)

您覆盖所有默认设置。您可能希望在Defaults.defaultSettings上使用settingsProject方法添加它们。