目前最好的方法是直接实现自定义任务。有关详细信息,请参阅输入任务。
// A custom task is required because `test` doesn't accept input.
lazy val customTest = inputKey[Unit]("custom test")
// This custom parser accepts a space separated list of arguments and then appends
// the fixed arguments to them. To do further parsing based on the user-specified
// arguments, use `flatMap` and return the next Parser.
lazy val testParser =
Def.spaceDelimited().map( (explicitArgs: Seq[String]) =>
explicitArgs ++ Seq("--bar", "--baz", "--qux", "--quux", "someDirectory")
)
customTest := {
// the result of parsing
val args = testParser.parsed
// auto-detected main class: can replace with literal string
val main = (mainClass in Compile).value getOrElse error("No main class detected.")
// main classpath, including compiled classes
val classpath = (fullClasspath in Compile).value.files
// provides Scala code execution
val scalaRun = (runner in (Compile, run)).value
val result = scalaRun.run(main, classpath, args, streams.value.log)
// handle any error
result foreach { errorMsg => sys.error(errorMsg) }
}