我该如何让自定义任务中的外部进程输出到SBT的信息日志,而不是输出到控制台?
我有一个SBT项目,其中有一个自定义任务,会执行一些操作,然后把结果回显到控制台。
build.sbt:
lazy val myTask = taskKey[Unit]("my custom task")
myTask := {
// Calls a separate shell script
s"${baseDirectory.value.getAbsolutePath}/my_task.sh" !
}
my_task.sh:
#!/usr/bin/env bash
# Do some stuff
echo "Successfully did the thing"
目前,这会把 “Successfully did the thing” 直接输出到控制台。
有没有办法让脚本输出到SBT的信息日志,而不是输出到控制台?这看起来是查看这类信息的更合适的地方,而且它还可以通过SBT的 --warn 标志按调用来改变本次运行的日志级别,从而实现逐次静默。
Current output:
$ sbt myTask
[info] welcome to sbt 1.9.8 (Eclipse Adoptium Java 17.0.12)
[info] loading global plugins from /Users/Me/.sbt/1.0/plugins
[info] loading settings for project my-project from meta-build.sbt,plugins.sbt ...
[info] loading project definition from /Users/Me/my-project
[info] resolving key references (14866 settings) ...
[info] set current project to my-project (in build file:/Users/Me/my-project/)
Successfully did the thing
Desired output:
$ sbt myTask
[info] welcome to sbt 1.9.8 (Eclipse Adoptium Java 17.0.12)
[info] loading global plugins from /Users/Me/.sbt/1.0/plugins
[info] loading settings for project my-project from meta-build.sbt,plugins.sbt ...
[info] loading project definition from /Users/Me/my-project
[info] resolving key references (14866 settings) ...
[info] set current project to my-project (in build file:/Users/Me/my-project/)
[info] Successfully did the thing
解决方案
This can be done using the streams.value.log 作为 ! 的日志记录器来实现:
myTask := {
s"${baseDirectory.value.getAbsolutePath}/my_task.sh" ! streams.value.log
}
This is documented in External Processes in the SBT documentation:
没有参数时,
!方法会把输出发送到标准输出和标准错误。你可以向!方法传入一个Logger,将输出发送到Logger:
shell "find project -name *.jar" ! log你可以通过以下方式获得一个
Logger:
scala val log = streams.value.log
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。