【问题标题】:How to pass arguments to InputTask without entering sbt interactive mode?如何在不进入 sbt 交互模式的情况下将参数传递给 InputTask?
【发布时间】:2016-07-09 17:33:15
【问题描述】:

使用以下示例 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]  ^

【问题讨论】:

标签: sbt


【解决方案1】:

sbt "sampleDoSomething a b c"

见文档:http://www.scala-sbt.org/0.13/docs/Running.html#Batch+mode

干杯

【讨论】:

  • 啊,很好。我之前尝试过sbt sampleDoSomething "a b c",但没想到还要在引号中包含该任务。
猜你喜欢
  • 1970-01-01
  • 2014-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-01
  • 1970-01-01
相关资源
最近更新 更多