6
votes

I use SBT 0.11.

I've the following build.sbt file in a sbt project:

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

It works fine, but how could I access settings like all Java/Scala source file paths, e.g. src/main/scala/*.scala, and the target class directory, e.g. target/scala-2.9.1/class?

1

1 Answers

7
votes

define a taskKey, say

yourActionTask

then

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
}

BTW. you can find more predefined task/keys in Keys.scala source code or scaladoc of sbt.

here is an example you can refer to which is extracted from one of my build file(just combine TaskKey and Task definition together, since I don't bother I will reuse the TaskKey in the future):

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