Christian
Christian

Reputation: 4593

Adding task in full build definition using SBT 0.13's new task syntax to project?

I implemented a simple task to copy some files from a project's target directory to some other directory:

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

The task is added to the project's settings:

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

And it works.

Now I wanted to change the task to use the new task syntax (introduced in 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)
    }
  }

So far, so good. But I don't how to add this task to a project. If I do it like with the old version

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

I'm getting this error:

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

I looked at the documentation but did not find a solution for this issue. I guess it should be pretty easy... Im using sbt 0.13.0 (upgrade to a newer version is not possible at the moment) and my build script is a .scala build definition.

Upvotes: 1

Views: 292

Answers (1)

4lex1v
4lex1v

Reputation: 21557

What you wrote are two different cases, if you check actual types in the first case you'll see: Def.Setting[Task[Unit]] and in your second case: TaskKey[Unit], that's where an error comes from. You've actually missed this part:

lazy val publishFilesTask = publishFiles

New 0.13 syntax change has changed from applicative way of defining settings to a macro based. To fix this just do like you wrote in your first version:

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

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

Upvotes: 1

Related Questions