Skip to content

Commit a866358

Browse files
author
Dave Syer
committed
Trap SIGINT in Gradle plugin
1 parent 1e5882b commit a866358

File tree

1 file changed

+34
-3
lines changed
  • spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/run

1 file changed

+34
-3
lines changed

spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/run/BootRunTask.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.gradle.run;
1818

1919
import java.io.File;
20+
import java.lang.reflect.Field;
2021
import java.util.ArrayList;
2122
import java.util.LinkedHashSet;
2223
import java.util.List;
@@ -25,7 +26,12 @@
2526
import org.gradle.api.internal.file.collections.SimpleFileCollection;
2627
import org.gradle.api.tasks.JavaExec;
2728
import org.gradle.api.tasks.SourceSet;
29+
import org.gradle.process.ExecResult;
30+
import org.gradle.process.internal.DefaultJavaExecAction;
31+
import org.gradle.process.internal.ExecHandle;
2832
import org.springframework.boot.loader.tools.FileUtils;
33+
import org.springframework.boot.loader.tools.SignalUtils;
34+
import org.springframework.util.ReflectionUtils;
2935

3036
/**
3137
* Extension of the standard 'run' task with additional Spring Boot features.
@@ -38,8 +44,8 @@ public class BootRunTask extends JavaExec {
3844
@Override
3945
public void exec() {
4046
SourceSet mainSourceSet = SourceSets.findMainSourceSet(getProject());
41-
final File outputDir = (mainSourceSet == null ? null : mainSourceSet.getOutput()
42-
.getResourcesDir());
47+
final File outputDir = (mainSourceSet == null ? null
48+
: mainSourceSet.getOutput().getResourcesDir());
4349
final Set<File> resources = new LinkedHashSet<File>();
4450
if (mainSourceSet != null) {
4551
resources.addAll(mainSourceSet.getResources().getSrcDirs());
@@ -53,7 +59,32 @@ public void exec() {
5359
FileUtils.removeDuplicatesFromOutputDirectory(outputDir, directory);
5460
}
5561
}
56-
super.exec();
62+
try {
63+
executeReflectively();
64+
} catch (Exception e) {
65+
getLogger().info("Cannot execute action reflectively");
66+
super.exec();
67+
}
5768
}
5869

70+
private ExecResult executeReflectively() throws Exception {
71+
Field builder = ReflectionUtils.findField(JavaExec.class, "javaExecHandleBuilder");
72+
builder.setAccessible(true);
73+
DefaultJavaExecAction action = (DefaultJavaExecAction) builder.get(this);
74+
setMain(getMain());
75+
final ExecHandle execHandle = action.build();
76+
ExecResult execResult = execHandle.start().waitForFinish();
77+
if (!isIgnoreExitValue()) {
78+
execResult.assertNormalExitValue();
79+
}
80+
SignalUtils.attachSignalHandler(new Runnable() {
81+
@Override
82+
public void run() {
83+
getLogger().info("Aborting java sub-process");
84+
execHandle.abort();
85+
}
86+
});
87+
return execResult;
88+
}
89+
5990
}

0 commit comments

Comments
 (0)