我正在尝试实现一个sbt任务,该任务收集使用项目的dependsOn
方法指定的项目的所有源目录。我最终得到了这段代码:
def sourcesOfDependencies(p: Project): Def.Initialize[Task[Seq[File]]] =
Def.taskDyn {
p.dependencies
.map(dep => (sourceDirectory in dep.project).toTask.map(Seq(_)))
.reduce((t1, t2) =>
t1.flatMap(s1 => t2.map(s2 => s1 ++ s2).taskValue)
)
}
sources := Def.taskDyn { sourcesOfDependencies(myProject) }.value
至于我它应该可以工作但是无法编译:
[error] /home/visa/src/Playtech-BIT/project/SparkDeployment.scala:57:32: The evaluation of `map` inside an anonymous function is prohibited.
[error]
[error] Problem: Task invocations inside anonymous functions are evaluated independently of whether the anonymous function is invoked or not.
[error]
[error] Solution:
[error] 1. Make `map` evaluation explicit outside of the function body if you don't care about its evaluation.
[error] 2. Use a dynamic task to evaluate `map` and pass that value as a parameter to an anonymous function.
[error]
[error] t1.flatMap(s1 => t2.map(s2 => s1 ++ s2).taskValue)
[error] ^
[error] /home/visa/src/Playtech-BIT/project/SparkDeployment.scala:57:26: Illegal dynamic reference: t2
[error] t1.flatMap(s1 => t2.map(s2 => s1 ++ s2).taskValue)
[error] ^
[error] /home/visa/src/Playtech-BIT/project/SparkDeployment.scala:57:39: Illegal dynamic reference: s1
[error] t1.flatMap(s1 => t2.map(s2 => s1 ++ s2).taskValue)
有人可以就如何应用建议的解决方案提出建议吗?或者也许有一种更简单的方法来实现我的目标?
答案 0 :(得分:1)
我一直在努力解决类似的问题但终于找到了answer。
因此,您可能要做的是定义执行复杂计算的动态任务,例如
val buildMaping: Def.Initialize[Task[Seq[Def.Initialize[(ProjectRef, Seq[Seq[File]])]]]] = {
Def.taskDyn {
val refs = loadedBuild.value.allProjectRefs
val tt = refs.map(_._1).map {
ref =>
sourceDirectories.all(ScopeFilter(inProjects(ref)))
.zipWith(Def.setting(ref)) { case (a, b) => b -> a }
}
Def.task {
tt
}
}
}
因此,这个buildMapping
允许您获取项目引用到其源目录的映射。
以这种方式调用你的任务:
Def.task {
val sd = settingsData.in(Global).value
val mapping = buildMaping.value.map(_.evaluate(sd)).toMap
val allProjectRefs = loadedBuild.value.allProjectRefs.map(_._1)
//... now you may iterate over each project,
// get deps and use the mapping to resolve your values
}