From 064de4e073eb2223ba950bc407e1e3a45578b1d9 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 11 Jan 2021 10:36:16 +0000 Subject: [PATCH] Check configured JavaLauncher when determining version of the JVM Previously, bootRun assumed that the Java version of the JVM that would run the application would be the same as the Java version of the JVM that is running the build. This assumption does not hold true when Gradle's toolchain support is used to configure tasks that fork a new JVM to use a version other than that being used by Gradle itself. This commit updates the BootRun task to query the JavaLauncher property when determining the version of Java on which the application will be run. Toolchain support and the JavaLauncher property are new in Gradle 6.7. To support earlier versions of Gradle, NoSuchMethodError is caught we continue as if no JavaLauncher has been configured and use the local JVM's Java version. Fixes gh-24512 --- .../boot/gradle/tasks/run/BootRun.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/run/BootRun.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/run/BootRun.java index 99fe3d5d35c1..fb3566f2594f 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/run/BootRun.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/run/BootRun.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,10 +19,12 @@ import java.lang.reflect.Method; import org.gradle.api.file.SourceDirectorySet; +import org.gradle.api.provider.Property; import org.gradle.api.tasks.Input; import org.gradle.api.tasks.JavaExec; import org.gradle.api.tasks.SourceSet; import org.gradle.api.tasks.SourceSetOutput; +import org.gradle.jvm.toolchain.JavaLauncher; /** * Custom {@link JavaExec} task for running a Spring Boot application. @@ -84,6 +86,15 @@ public void exec() { } private boolean isJava13OrLater() { + try { + Property javaLauncher = this.getJavaLauncher(); + if (javaLauncher.isPresent()) { + return javaLauncher.get().getMetadata().getLanguageVersion().asInt() >= 13; + } + } + catch (NoSuchMethodError ex) { + // Continue + } for (Method method : String.class.getMethods()) { if (method.getName().equals("stripIndent")) { return true;