6

我使用 SBT 0.11。

build.sbt在 sbt 项目中有以下文件:

myAction := {
  // val srcFiles = ?
  // How can I make srcFiles a List[File] of all sources files?
  println("This is my action")
}

它工作得很好,但是我如何访问所有 J​​ava/Scala 源文件路径(例如src/main/scala/*.scala)和目标类目录(例如 target/scala-2.9.1/class)之类的设置?

4

1 回答 1

7

定义一个taskKey,比如说

你的行动任务

然后

yourActionTask <<= (baseDirectory, target, packageBin in Compile, resources in Compile...) map { (basedir, targetDir, bin, res...)=>  
     // use these resources to complete your task as per your needs
}

顺便提一句。您可以在 Keys.scala 源代码或 sbt 的 scaladoc 中找到更多预定义的任务/键。

这是一个您可以参考的示例,它是从我的一个构建文件中提取的(只需将 TaskKey 和 Task 定义结合在一起,因为我不打扰我将来会重用 TaskKey):

  val distTask = TaskKey[Unit]("dist", "distribute the deployment package of eromanga") <<= (baseDirectory, target, fullClasspath in Compile, packageBin in Compile, resources in Compile, streams) map {
(baseDir, targetDir, cp, jar, res, s) =>
  s.log.info("[dist] prepare distribution folders...")
  val assemblyDir = targetDir / "dist"
  val confDir = assemblyDir / "conf"
  val libDir = assemblyDir / "lib"
  val binDir = assemblyDir / "bin"
  Array(assemblyDir, confDir, libDir, binDir).foreach(IO.createDirectory)

  s.log.info("[dist] copy jar artifact to lib...")
  IO.copyFile(jar, libDir / jar.name)

  s.log.info("[dist] copy 3rd party dependencies to lib...")
  cp.files.foreach(f => if (f.isFile) IO.copyFile(f, libDir / f.name))

  s.log.info("[dist] copy shell scripts to bin...")
  ((baseDir / "bin") ** "*.sh").get.foreach(f => IO.copyFile(f, binDir / f.name))

  s.log.info("[dist] copy configuration templates to conf...")
  ((baseDir / "conf") * "*.xml").get.foreach(f => IO.copyFile(f, confDir / f.name))

  s.log.info("[dist] copy examples chanenl deployment...")
  IO.copyDirectory(baseDir / "examples", assemblyDir / "examples")

  res.filter(_.name.startsWith("logback")).foreach(f => IO.copyFile(f, assemblyDir / f.name))
  }
于 2012-06-28T10:43:07.490 回答