开发者

How to depend on other tasks and do your code in SBT 0.10?

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


<<= 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, _) =>

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.

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...

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

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

and get the Seq[File] from cp.files.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜