如何在不进入sbt交互模式的情况下将参数传递给InputTask?

时间:2016-03-22 17:16:11

标签: sbt

使用以下示例SBT构建文件,我可以从SBT交互模式中向我的InputTask传递参数,但不能从没有传递参数。有办法吗?

示例build.sbt:

import complete.DefaultParsers._

lazy val sampleDoSomething = inputKey[Unit]("Will print arguments.")

lazy val commonSettings = Seq(
  organization := "com.example",
  version := "0.1.0-SNAPSHOT"
)

lazy val taskInputTaskProject = (project in file(".")).
  settings(commonSettings: _*).
  settings(
    sampleDoSomething := {
      println("Arguments: ")
      val args = spaceDelimited("<arg>").parsed
      args foreach println
    }
  )

从SBT交互模式中成功调用任务:

$ sbt
[info] Set current project to taskInputTaskProject (in build file:/study/sbt/input-tasks/)
> sampleDoSomething a b c
Arguments: 
a
b
c
[success] Total time: 0 s, completed Mar 22, 2016 1:06:58 PM

从命令行成功调用任务而不带参数:

$ sbt sampleDoSomething
[info] Set current project to taskInputTaskProject (in build file:/study/sbt/input-tasks/)
Arguments: 
[success] Total time: 0 s, completed Mar 22, 2016 1:06:18 PM

无法使用参数从命令行调用任务:

$ sbt sampleDoSomething a b c
[info] Set current project to taskInputTaskProject (in build file:/study/sbt/input-tasks/)
Arguments: 
[success] Total time: 0 s, completed Mar 22, 2016 1:06:44 PM
[error] Not a valid command: a
[error] Expected 'all'
[error] Not a valid project ID: a
[error] Expected ':' (if selecting a configuration)
[error] Not a valid key: a
[error] a
[error]  ^

1 个答案:

答案 0 :(得分:2)

sbt "sampleDoSomething a b c"

请参阅doc:http://www.scala-sbt.org/0.13/docs/Running.html#Batch+mode

干杯