如何获取 sbt 项目的所有项目依赖项的源目录? [英] How to get source directories of all project dependencies of an sbt project?

查看:26
本文介绍了如何获取 sbt 项目的所有项目依赖项的源目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个 sbt 任务,它收集使用项目的 dependsOn 方法指定的项目的所有源目录.我最终得到了这段代码:

I'm trying to implement an sbt task which collects all the source directories of projects specified using dependsOn method of a project. I end up with this piece of code:

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

对我来说它应该可以工作但它无法编译:

As for me it should work but it fails to compile:

[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)

有人可以建议如何应用建议的解决方案吗?或者也许有更简单的方法来实现我的目标?

Could anyone advise on how to apply the proposed solution? Or maybe there is a simpler way to achieve my goal?

推荐答案

我一直在努力解决类似的问题,但最终找到了一个 回答.

I've been struggling with a similar problem but finally found an answer.

所以,你可以做的是定义一个动态任务来执行你的复杂计算,比如

So, what you may do is to define a dynamic task performing your complex computation, like

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 允许您获取项目引用到其源目录的映射.

So, this buildMapping allows you to get a map of project references to their source directories.

然后以这种方式调用您的任务:

then invoke your task this way:

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
}

这篇关于如何获取 sbt 项目的所有项目依赖项的源目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆