如何依赖其他任务并在 SBT 0.10 中执行您的代码? [英] How to depend on other tasks and do your code in SBT 0.10?

查看:28
本文介绍了如何依赖其他任务并在 SBT 0.10 中执行您的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想定义一个任务,它调用 compile 和 packageBin 任务,然后做它的事情.我怎么做?目前这只做第二部分并跳过编译&packageBin 任务.

I want to define a task, that invokes compile and packageBin tasks, and then does its stuff. How do I do that? Currently this only does the second part and skips on compile & packageBin tasks.

lazy val dist = TaskKey[Unit](
  "dist", "Creates a project distribution in dist/ folder."
)
def distTask = {
  dist <<= dist.dependsOn(compile, packageBin)
  dist <<= (update, crossTarget).map { case (updateReport, out) =>
    updateReport.allFiles.foreach { srcPath =>
      val destPath = out / "lib" / srcPath.getName
      IO.copyFile(srcPath, destPath, preserveLastModified=true)
    }
  }
}

推荐答案

<<= 是 TaskKey 上的一个返回值的方法.它不会在任何地方更新可变状态,因此在示例代码中,第一次调用的结果将被丢弃.要解决此问题,请将 packageBin 也声明为输入,但忽略结果值.注意packageBin依赖于compile,所以依赖compile是没有必要的.

<<= is a method on TaskKey that returns a value. It does not update mutable state anywhere, so in the example code, the result of the first call is being discarded. To fix this, declare packageBin as an input as well, but ignore the resulting value. Note that packageBin depends on compile, so depending on compile is unnecessary.

dist <<= (update, crossTarget, packageBin in Compile) map { (updateReport, out, _) =>

您不太可能仅根据文件名将 UpdateReport 中的所有文件复制到目录中.不同的依赖项可能具有相同的文件名.此外,这将包括来自所有配置的依赖项,包括测试依赖项.

It is unlikely that you want to copy all of the files in an UpdateReport to a directory based solely on the file's name. It is possible for different dependencies to have the same file name. Also, this will include dependencies from all configurations, including test dependencies.

对于第一个问题,使用关联的ModuleID在目标目录中构造路径,就像在retrieveManaged := true时在lib_managed目录中所做的一样.对于第二个问题,只选择你想要的配置文件.

For the first problem, use the associated ModuleID to construct the path in the target directory, like is done in the lib_managed directory when retrieveManaged := true. For the second problem, only select the files for the configuration you want.

updateReport.matching(configurationFilter(Runtime.name)).foreach...

请参阅sbt.UpdateReportsbt.RichUpdateReport 其他有用方法的 API 文档.

See the sbt.UpdateReport and sbt.RichUpdateReport API docs for other useful methods.

如果你不关心文件名冲突,你可以从dependencyClasspath获取依赖文件.例如:

If you aren't concerned about file name collisions, you can get the dependency files from dependencyClasspath. For example:

dist <<= (crossTarget, packageBin in Compile, dependencyClasspath in Runtime) map { (out, _, cp) =>

并从 cp.files 中获取 Seq[File].

这篇关于如何依赖其他任务并在 SBT 0.10 中执行您的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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