Browse Source

Polish NativeImageRequirementsNotMetException

Closes gh-48307
pull/48312/head
Andy Wilkinson 3 weeks ago
parent
commit
46f22b2374
  1. 31
      core/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java
  2. 10
      core/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java

31
core/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java

@ -420,7 +420,7 @@ public class SpringApplication {
private void addAotGeneratedInitializerIfNecessary(List<ApplicationContextInitializer<?>> initializers) { private void addAotGeneratedInitializerIfNecessary(List<ApplicationContextInitializer<?>> initializers) {
if (NativeDetector.inNativeImage()) { if (NativeDetector.inNativeImage()) {
checkNativeImageVersion(); NativeImageRequirementsException.throwIfNotMet();
} }
if (AotDetector.useGeneratedArtifacts()) { if (AotDetector.useGeneratedArtifacts()) {
List<ApplicationContextInitializer<?>> aotInitializers = new ArrayList<>( List<ApplicationContextInitializer<?>> aotInitializers = new ArrayList<>(
@ -438,15 +438,6 @@ public class SpringApplication {
} }
} }
private void checkNativeImageVersion() {
JavaVersion minRequiredJavaVersion = JavaVersion.TWENTY_FIVE;
if (JavaVersion.getJavaVersion().isOlderThan(minRequiredJavaVersion)) {
throw new NativeImageRequirementsNotMetException(
"Native Image requirements not met, please upgrade it. Native Image must support at least Java %s"
.formatted(minRequiredJavaVersion));
}
}
private void refreshContext(ConfigurableApplicationContext context) { private void refreshContext(ConfigurableApplicationContext context) {
if (this.properties.isRegisterShutdownHook()) { if (this.properties.isRegisterShutdownHook()) {
shutdownHook.registerApplicationContext(context); shutdownHook.registerApplicationContext(context);
@ -1661,16 +1652,24 @@ public class SpringApplication {
/** /**
* Exception which is thrown if GraalVM's native-image requirements aren't met. * Exception which is thrown if GraalVM's native-image requirements aren't met.
*/ */
static final class NativeImageRequirementsNotMetException extends RuntimeException { static final class NativeImageRequirementsException extends RuntimeException {
/** private static final JavaVersion MINIMUM_REQUIRED_JAVA_VERSION = JavaVersion.TWENTY_FIVE;
* Creates a new {@link NativeImageRequirementsNotMetException} instance.
* @param message the message private static final JavaVersion CURRENT_JAVA_VERSION = JavaVersion.getJavaVersion();
*/
NativeImageRequirementsNotMetException(String message) { NativeImageRequirementsException(String message) {
super(message); super(message);
} }
static void throwIfNotMet() {
if (CURRENT_JAVA_VERSION.isOlderThan(MINIMUM_REQUIRED_JAVA_VERSION)) {
throw new NativeImageRequirementsException("Native Image requirements not met. "
+ "Native Image must support at least Java %s but Java %s was detected"
.formatted(MINIMUM_REQUIRED_JAVA_VERSION, CURRENT_JAVA_VERSION));
}
}
} }
/** /**

10
core/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java

@ -59,7 +59,7 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.beans.factory.support.DefaultBeanNameGenerator; import org.springframework.beans.factory.support.DefaultBeanNameGenerator;
import org.springframework.boot.Banner.Mode; import org.springframework.boot.Banner.Mode;
import org.springframework.boot.SpringApplication.NativeImageRequirementsNotMetException; import org.springframework.boot.SpringApplication.NativeImageRequirementsException;
import org.springframework.boot.availability.AvailabilityChangeEvent; import org.springframework.boot.availability.AvailabilityChangeEvent;
import org.springframework.boot.availability.AvailabilityState; import org.springframework.boot.availability.AvailabilityState;
import org.springframework.boot.availability.LivenessState; import org.springframework.boot.availability.LivenessState;
@ -77,6 +77,7 @@ import org.springframework.boot.context.event.ApplicationStartingEvent;
import org.springframework.boot.context.event.SpringApplicationEvent; import org.springframework.boot.context.event.SpringApplicationEvent;
import org.springframework.boot.convert.ApplicationConversionService; import org.springframework.boot.convert.ApplicationConversionService;
import org.springframework.boot.env.DefaultPropertiesPropertySource; import org.springframework.boot.env.DefaultPropertiesPropertySource;
import org.springframework.boot.system.JavaVersion;
import org.springframework.boot.testsupport.classpath.ForkedClassPath; import org.springframework.boot.testsupport.classpath.ForkedClassPath;
import org.springframework.boot.testsupport.classpath.resources.WithResource; import org.springframework.boot.testsupport.classpath.resources.WithResource;
import org.springframework.boot.testsupport.system.CapturedOutput; import org.springframework.boot.testsupport.system.CapturedOutput;
@ -753,9 +754,10 @@ class SpringApplicationTests {
try { try {
SpringApplication application = new SpringApplication(); SpringApplication application = new SpringApplication();
application.setWebApplicationType(WebApplicationType.NONE); application.setWebApplicationType(WebApplicationType.NONE);
assertThatExceptionOfType(NativeImageRequirementsNotMetException.class).isThrownBy(application::run) assertThatExceptionOfType(NativeImageRequirementsException.class).isThrownBy(application::run)
.withMessage( .withMessage("Native Image requirements not met. "
"Native Image requirements not met, please upgrade it. Native Image must support at least Java 25"); + "Native Image must support at least Java 25 but Java %s was detected"
.formatted(JavaVersion.getJavaVersion()));
} }
finally { finally {
System.clearProperty("org.graalvm.nativeimage.imagecode"); System.clearProperty("org.graalvm.nativeimage.imagecode");

Loading…
Cancel
Save