使用SBT 0.13的新任务语法添加完整构建定义中的任务进行投影?

时间:2014-03-11 16:26:35

标签: scala sbt

我实现了一个简单的任务,将一些文件从项目的目标目录复制到其他目录:

  lazy val publishFiles = taskKey[Unit]("publishes the files")

  lazy val publishFilesTask = publishFiles <<= (projectID, target, streams) map { (id, targetDir, streams) =>
    val Sprint = "99"
    val DestBaseDir = "/some/folder"
    val DestDir = new File(s"$DestBaseDir/Sprint$Sprint/${id.name}")
    val log = streams.log
    val ScoverageReportDir = "scoverage-report"
    val CoberturaFileName = "cobertura.xml"

    if (DestDir.exists)
      log.error(s"Destination Directory $DestDir exists, exiting ...")
    else {
      log.info(s"Copying test coverage report to $DestDir ...")
      sbt.IO.createDirectory(DestDir)
      sbt.IO.copyDirectory(targetDir / ScoverageReportDir, DestDir / ScoverageReportDir, overwrite = false)
      sbt.IO.copyFile(targetDir / "coverage-report" / CoberturaFileName, DestDir / CoberturaFileName)
    }
  }

任务已添加到项目的设置中:

  lazy val settings = ... ++ publishFilesTask ++ ..

它有效。

现在我想更改任务以使用新的任务语法(在sbt 0.13.0中引入):

  lazy val publishFilesTask = taskKey[Unit]("publishes the files")

  publishFilesTask := {
    val Sprint = "99"
    val DestBaseDir = "/some/folder"
    val DestDir = new File(s"$DestBaseDir/Sprint$Sprint/${projectID.value.name}")
    val log = streams.value.log
    val ScoverageReportDir = "scoverage-report"
    val CoberturaFileName = "cobertura.xml"

    if (DestDir.exists)
      log.error(s"Destination Directory $DestDir exists, exiting ...")
    else {
      log.info(s"Copying test coverage report to $DestDir ...")
      sbt.IO.createDirectory(DestDir)
      sbt.IO.copyDirectory(target.value / ScoverageReportDir, DestDir / ScoverageReportDir, overwrite = false)
      sbt.IO.copyFile(target.value / "coverage-report" / CoberturaFileName, DestDir / CoberturaFileName)
    }
  }

到目前为止,这么好。但我不知道如何将此任务添加到项目中。如果我喜欢旧版本

  lazy val settings = ... ++ publishFilesTask ++ ..

我收到了这个错误:

  [error]  found   : sbt.TaskKey[Unit]
  [error]  required: scala.collection.GenTraversableOnce[?]

我查看了文档,但没有找到解决此问题的方法。我想这应该很容易......   我使用sbt 0.13.0(目前无法升级到更新的版本),我的构建脚本是.scala构建定义。

1 个答案:

答案 0 :(得分:1)

你写的是两种不同的情况,如果你在第一种情况下检查实际类型,你会看到:Def.Setting[Task[Unit]],在你的第二种情况下:TaskKey[Unit],那就是错误来自。你实际上错过了这部分:

lazy val publishFilesTask = publishFiles

新的0.13语法更改已从定义设置的应用方式更改为基于宏。要解决这个问题就像你在第一个版本中写的一样:

lazy val publishFiles = taskKey[Unit]("publishes the files")

lazy val publishFilesTask = publishFiles := { ... }