From b9ae61fc109f71902125230bccd162c31461ab19 Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Wed, 11 Jan 2023 14:30:37 +0100 Subject: [PATCH] Disable devtools when running in a native image See gh-32853 --- ...DevToolsPropertyDefaultsPostProcessor.java | 36 +++++++++++++------ .../system/DevToolsEnablementDeducer.java | 7 +++- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/env/DevToolsPropertyDefaultsPostProcessor.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/env/DevToolsPropertyDefaultsPostProcessor.java index 26c28ceace..01bef2240c 100755 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/env/DevToolsPropertyDefaultsPostProcessor.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/env/DevToolsPropertyDefaultsPostProcessor.java @@ -30,6 +30,7 @@ import org.springframework.boot.devtools.logger.DevToolsLogFactory; import org.springframework.boot.devtools.restart.Restarter; import org.springframework.boot.devtools.system.DevToolsEnablementDeducer; import org.springframework.boot.env.EnvironmentPostProcessor; +import org.springframework.core.NativeDetector; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.core.env.ConfigurableEnvironment; @@ -63,19 +64,12 @@ public class DevToolsPropertyDefaultsPostProcessor implements EnvironmentPostPro private static final Map PROPERTIES; static { - Properties properties = new Properties(); - try (InputStream stream = DevToolsPropertyDefaultsPostProcessor.class - .getResourceAsStream("devtools-property-defaults.properties")) { - properties.load(stream); + if (NativeDetector.inNativeImage()) { + PROPERTIES = Collections.emptyMap(); } - catch (IOException ex) { - throw new RuntimeException("Failed to load devtools-property-defaults.properties", ex); + else { + PROPERTIES = loadDefaultProperties(); } - Map map = new HashMap<>(); - for (String name : properties.stringPropertyNames()) { - map.put(name, properties.getProperty(name)); - } - PROPERTIES = Collections.unmodifiableMap(map); } @Override @@ -138,4 +132,24 @@ public class DevToolsPropertyDefaultsPostProcessor implements EnvironmentPostPro } } + private static Map loadDefaultProperties() { + Properties properties = new Properties(); + try (InputStream stream = DevToolsPropertyDefaultsPostProcessor.class + .getResourceAsStream("devtools-property-defaults.properties")) { + if (stream == null) { + throw new RuntimeException( + "Failed to load devtools-property-defaults.properties because it doesn't exist"); + } + properties.load(stream); + } + catch (IOException ex) { + throw new RuntimeException("Failed to load devtools-property-defaults.properties", ex); + } + Map map = new HashMap<>(); + for (String name : properties.stringPropertyNames()) { + map.put(name, properties.getProperty(name)); + } + return Collections.unmodifiableMap(map); + } + } diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/system/DevToolsEnablementDeducer.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/system/DevToolsEnablementDeducer.java index da76267952..0d04888d34 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/system/DevToolsEnablementDeducer.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/system/DevToolsEnablementDeducer.java @@ -21,6 +21,7 @@ import java.util.LinkedHashSet; import java.util.Set; import org.springframework.boot.SpringApplicationAotProcessor; +import org.springframework.core.NativeDetector; /** * Utility to deduce if DevTools should be enabled in the current context. @@ -47,11 +48,15 @@ public final class DevToolsEnablementDeducer { /** * Checks if a specific {@link StackTraceElement} in the current thread's stacktrace - * should cause devtools to be disabled. + * should cause devtools to be disabled. Devtools will also be disabled if running in + * a native image. * @param thread the current thread * @return {@code true} if devtools should be enabled */ public static boolean shouldEnable(Thread thread) { + if (NativeDetector.inNativeImage()) { + return false; + } for (StackTraceElement element : thread.getStackTrace()) { if (isSkippedStackElement(element)) { return false;