在sbt 0.11中的自定义任务中获取项目的源文件

时间:2012-06-28 08:00:48

标签: scala sbt

我使用SBT 0.11。

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

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

它工作正常,但我如何访问所有Java / Scala源文件路径的设置,例如src/main/scala/*.scala和目标类目录,例如目标/阶-2.9.1 /类?

1 个答案:

答案 0 :(得分:7)

定义一个taskKey,比如说

  

yourActionTask

然后

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