【问题标题】:Adding task in full build definition using SBT 0.13's new task syntax to project?使用 SBT 0.13 的新任务语法在完整构建定义中添加任务到项目?
【发布时间】:2014-03-12 18:57:11
【问题描述】:

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

  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 构建定义。

【问题讨论】:

    标签: scala sbt


    【解决方案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 := { ... }
    

    【讨论】:

    • 谢谢!我经常感到困惑,因为在文档中大部分时间使用 build.sbt 而我使用的是完整的 scala 构建脚本:-)
    • @Christian 看看 Josh Suereth 的 this 书,会更容易理解 sbt 的工作原理
    • 我推荐一个更简单的publishFiles := { ... }lazy val publishFilesTask = publishFiles := { ... }
    • 但这对我不起作用。还是我理解错了?
    猜你喜欢
    • 2014-03-16
    • 2016-08-04
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 2016-07-20
    • 2012-05-15
    • 1970-01-01
    • 2014-06-18
    相关资源
    最近更新 更多