如何在 sbt 0.13 中动态获取子项目列表
How to get the list of subprojects dynamically in sbt 0.13
我如何以编程方式(在build.sbt
中)在sbt 0.13中找到当前根项目的所有子项目?
(我还没有尝试 Project.componentProjects,因为它是 sbt 1.0 中的新功能)。
lazy val root = (project in file(".") ... )
val myTask = taskKey[Unit]("some description")
myTask := {
val masterRoot = baseDirectory.value
// This does not work
// val subProjects: Seq[ProjectReference] = root.aggregate
// So I tried to specify the subproject list explicitly; still does not work
val subProjects = Seq[Project](subPrj1)
subProjects.foreach { subproject =>
// All of this works if the "subproject" is hard-coded to "subPrj1"
val subprojectTarget = target.in(subproject).value / "classes"
val cp = (dependencyClasspath in(subproject, Compile, compile)).value
}
}
遇到这些错误:
build.sbt: error: Illegal dynamic reference: subproject
val subprojectTarget = target.in(subproject).value / "classes"
^
build.sbt: error: Illegal dynamic reference: subproject
val cp = (dependencyClasspath in(subproject, Compile, compile)).value
您可以通过 buildStructure.value.allProjectRefs
.
访问所有子项目的列表
另一部分是你的问题,这是一个我也经常遇到的可怕问题。我能够通过首先创建 List[Task[A]
然后使用递归函数将其提升为 Task[List[A]]
.
来解决此类问题
def flattenTasks[A](tasks: Seq[Def.Initialize[Task[A]]]): Def.Initialize[Task[List[A]]] =
tasks.toList match {
case Nil => Def.task { Nil }
case x :: xs => Def.taskDyn {
flattenTasks(xs) map (x.value :: _)
}
}
myTask := {
val classDirectories: List[File] = Def.taskDyn {
flattenTasks {
for (project ← buildStructure.value.allProjectRefs)
yield Def.task { (target in project).value / "classes" }
}
}.value
}
我用过这种方法,例如这里:utility methods actual usage
我如何以编程方式(在build.sbt
中)在sbt 0.13中找到当前根项目的所有子项目?
(我还没有尝试 Project.componentProjects,因为它是 sbt 1.0 中的新功能)。
lazy val root = (project in file(".") ... )
val myTask = taskKey[Unit]("some description")
myTask := {
val masterRoot = baseDirectory.value
// This does not work
// val subProjects: Seq[ProjectReference] = root.aggregate
// So I tried to specify the subproject list explicitly; still does not work
val subProjects = Seq[Project](subPrj1)
subProjects.foreach { subproject =>
// All of this works if the "subproject" is hard-coded to "subPrj1"
val subprojectTarget = target.in(subproject).value / "classes"
val cp = (dependencyClasspath in(subproject, Compile, compile)).value
}
}
遇到这些错误:
build.sbt: error: Illegal dynamic reference: subproject
val subprojectTarget = target.in(subproject).value / "classes"
^
build.sbt: error: Illegal dynamic reference: subproject
val cp = (dependencyClasspath in(subproject, Compile, compile)).value
您可以通过 buildStructure.value.allProjectRefs
.
另一部分是你的问题,这是一个我也经常遇到的可怕问题。我能够通过首先创建 List[Task[A]
然后使用递归函数将其提升为 Task[List[A]]
.
def flattenTasks[A](tasks: Seq[Def.Initialize[Task[A]]]): Def.Initialize[Task[List[A]]] =
tasks.toList match {
case Nil => Def.task { Nil }
case x :: xs => Def.taskDyn {
flattenTasks(xs) map (x.value :: _)
}
}
myTask := {
val classDirectories: List[File] = Def.taskDyn {
flattenTasks {
for (project ← buildStructure.value.allProjectRefs)
yield Def.task { (target in project).value / "classes" }
}
}.value
}
我用过这种方法,例如这里:utility methods actual usage