在后台运行 JavaExec 任务,然后在构建完成时终止
我试图找出如何启动一个 JavaExec 任务来生成 Jetty 服务器而不阻塞后续任务。另外,我需要在构建完成后终止该服务器。知道我该怎么做吗?
I'm trying to figure out how to launch a JavaExec task that spawns a Jetty server without blocking subsequent tasks. Also, I will need to terminate this server after the build completes. Any idea how I can do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我知道该线程是 2011 年的,但我仍然偶然发现了这个问题。这是使用 Gradle 2.14 的解决方案:
I know the thread is from 2011, but I still stumbled across the problem. So here's a solution working with Gradle 2.14:
我更新了 @chrishuen 的解决方案,因为您无法再对任务调用执行。这是我的工作
build.gradle
I updated solution from @chrishuen because you cannot call execute on task anymore. Here is my working
build.gradle
希望这段代码能让您了解如何完成它。
您可以使用构建侦听器闭包在构建开始/结束时运行代码。然而,由于某种原因, gradle.buildStarted 闭包在 Milestone-3 中不起作用,因此我将其替换为 gradle.taskGraph.whenReady ,这可以解决问题。
然后,您可以使用
Task#execute()
调用runJetty
任务(请注意,此 API 不是官方的,可能会消失),此外,还可以从运行它ExecutorService
来获取一些异步行为。Hope this snippet will give you some insight on how it can be done.
You can used build listener closures to run code on build start/finish. However, for some reason,
gradle.buildStarted
closure does not work in milestone-3, so I have replaced it withgradle.taskGraph.whenReady
which does the trick.Then you can call the
runJetty
task usingTask#execute()
(Note, this API is not official and may disappear), and additionally, run it from anExecutorService
to get some asynchronous behaviour.你不能用 JavaExec 来做到这一点;你必须编写自己的任务。
You can't do it with
JavaExec
; you'll have to write your own task.根据之前的回答,我的看法是:
Based on previous answers, here is my take: