我希望有一个命令publish-snapshot
,它将运行publish
任务并修改version
设置(该设置将在执行命令时计算)。 / p>
我想出了如何获取version
内部命令的当前值,而Project.runTask("task", "scope", ...)
似乎是调用publish
任务的正确调用。
我唯一感到困惑的是如何使用新版本值修改State
实例。我的所有尝试似乎都对原始版本设置无效。
我的最后一次尝试:
val printVers = TaskKey[Unit]("printvers")
val printVersTask = TaskKey[Unit]("printvers") <<= {version map println}
def publishSnapshot = Command.command("publish-snapshot") { state =>
val newState = SessionSettings.reapply(state.get(sessionSettings).get.appendRaw(version := "???"), state)
Project.runTask(printVers in Compile, newState, true)
state
}
lazy val root = Project("main", file("."),
settings =
Defaults.defaultSettings ++
Seq(printVersTask)).settings(commands += publishSnapshot)
有没有办法解决这个问题?
答案 0 :(得分:20)
在sbt
邮件列表的帮助下,我能够创建如下解决方案:
def publishSnapshot = Command.command("publish-snapshot") { state =>
val extracted = Project extract state
import extracted._
val eVersion = getOpt(version).get // getting current version
runTask(publish in Compile,
append(Seq(version := "newVersion"), state),
true
)
state
}
答案 1 :(得分:12)
这实际上对我不起作用。我正在使用SBT 0.13.7
根据上面的例子调整我必须做的事情,我必须做类似的事情:
def publishSnapshot = Command.command("publish-snapshot") { state =>
val extracted = Project extract state
val newState = extracted.append(Seq(version := "newVersion"), state)
val (s, _) = Project.extract(newState).runTask(publish in Compile, newState)
s
}
或者做:
def publishSnapshot = Command.command("publish-snapshot") { state =>
val newState =
Command.process("""set version := "newVersion" """, state)
val (s, _) = Project.extract(newState).runTask(publish in Compile, newState)
s
}
答案 2 :(得分:3)
要从命令更新任意设置,请执行以下操作:
def updateFoo = Command.command("updateFoo") { state =>
val extracted = Project extract state
import extracted._
println("'foo' set to true")
//append returns state with updated Foo
append(Seq(foo := true), state)
}