使用 SBT 0.13 的新任务语法在完整构建定义中添加任务到项目中?



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

  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 build build定义。

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

lazy val publishFilesTask = publishFiles

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

lazy val publishFiles = taskKey[Unit]("publishes the files")
lazy val publishFilesTask = publishFiles := { ... }

最新更新