From 10db07e07501cde8db3f9db0bd2d878997c6ff4c Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 12 Apr 2021 10:02:17 +0200 Subject: [PATCH] Fix serverStart for canceling build to stop server --- build.gradle | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/build.gradle b/build.gradle index 7f9d982..cb42345 100644 --- a/build.gradle +++ b/build.gradle @@ -130,25 +130,35 @@ def serverStart(String serverName) { Thread outputThread = proc.consumeProcessOutputStream(System.out) outputThread.setName("${serverName} - OutputThread") - Thread inputThread = new Thread(new Runnable() { - @Override - void run() { - Reader reader = System.in.newReader() - Writer writer = proc.getOutputStream().newWriter() - try { - while (true) { - String s = reader.readLine() - writer.write(s + "\n") - writer.flush() - } - } catch (InterruptedIOException e) { - + Thread inputThread = new Thread({ + Reader reader = System.in.newReader() + Writer writer = proc.getOutputStream().newWriter() + try { + while (proc.alive) { + String s = reader.readLine() + writer.write(s + "\n") + writer.flush() } + } catch (IOException e) { + // Ignored } }) inputThread.setName("${serverName} - InputThread") inputThread.start() + gradle.buildFinished { buildResult -> + if (!proc.alive) { + return + } + Writer writer = proc.getOutputStream().newWriter() + writer.write("stop\n") + writer.flush() + + proc.closeStreams() + outputThread.interrupt() + inputThread.interrupt() + } + while (proc.alive) { Thread.sleep(10) }