scala SBT 停止运行而不退出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5137460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 02:51:33  来源:igfitidea点击:

SBT stop run without exiting

scalasbtterminate

提问by dsg

How do you terminate a run in SBT without exiting?

如何在不退出的情况下终止 SBT 中的运行?

I'm trying CTRL+C but it exits SBT. Is there a way to only exit the running application while keeping SBT open?

我正在尝试 CTRL+C 但它退出了 SBT。有没有办法只退出正在运行的应用程序,同时保持 SBT 打开?

采纳答案by Seth Tisue

In the default configuration, your runs happen in the same JVM that sbt is running, so you can't easily kill them separately.

在默认配置中,您的运行发生在 sbt 正在运行的同一个 JVM 中,因此您不能轻易地单独杀死它们。

If you do your run in a separate, forked JVM, as described at Forking, then you can kill that JVM (by any means your operating system offers) without affecting sbt's JVM:

如果您在一个单独的分叉 JVM 中运行,如Forking 中所述,那么您可以终止该 JVM(通过您的操作系统提供的任何方式)而不会影响 sbt 的 JVM:

fork in run := true

回答by Jonas Anso

From sbt version 0.13.5 you can add to your build.sbt

从 sbt 0.13.5 版开始,您可以添加到 build.sbt

cancelable in Global := true

It is defined as "Enables (true) or disables (false) the ability to interrupt task execution with CTRL+C." in the Keys definition

它被定义为“启用 (true) 或禁用 (false) 使用 CTRL+C 中断任务执行的能力”。在Keys 定义中

If you are using Scala 2.12.7+ you can also cancel the compilation with CTRL+C. Reference https://github.com/scala/scala/pull/6479

如果您使用的是 Scala 2.12.7+,您还可以使用 CTRL+C 取消编译。参考https://github.com/scala/scala/pull/6479

There are some bugs reported:

报告了一些错误:

回答by kilo

I've found the following useful when I have control over the main loop of the application being run from sbt.

当我可以控制从 sbt 运行的应用程序的主循环时,我发现以下内容很有用。

I tell sbt to fork when running the application (in build.sbt):

我在运行应用程序时告诉 sbt fork(在 build.sbt 中):

fork in run := true

I also tell sbt to forward stdin from the sbt shell to the application (in build.sbt):

我还告诉 sbt 将 stdin 从 sbt shell 转发到应用程序(在 build.sbt 中):

connectInput in run := true

Finally, in the main thread of the application, I wait for end-of-file on stdin and then shutdown the JVM:

最后,在应用程序的主线程中,我在 stdin 上等待文件结束,然后关闭 JVM:

while (System.in.read() != -1) {}
logger.warn("Received end-of-file on stdin. Exiting")
// optional shutdown code here
System.exit(0)

Of course, you can use any thread to read stdin and shutdown, not just the main thread.

当然,你可以使用任何线程来读取stdin和shutdown,而不仅仅是主线程。

Finally, start sbt, optionally switch to the subproject you want to run, run.

最后启动sbt,可选择切换到你要运行的子项目,运行。

Now, when you want to stop the process, close its stdin by typing CTRL-D in the sbt shell.

现在,当您想停止该进程时,请通过在 sbt shell 中键入 CTRL-D 来关闭其标准输入。

回答by mtsokol

Consider using sbt-revolver. We use it in our company and it's really handy. For what you're asking can be done with:

考虑使用sbt-revolver。我们在我们公司使用它,它真的很方便。对于您的要求,可以通过以下方式完成:

reStart

reStop

Without need to configure build.sbt file.

无需配置 build.sbt 文件。

Your can use this plugin by adding:

您可以通过添加以下内容来使用此插件:

addSbtPlugin("io.spray" % "sbt-revolver" % "0.9.1")

To your project/plugins.sbt

到您的项目/plugins.sbt