Skip to content

Commit 064de4e

Browse files
committed
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
1 parent 5ad4d62 commit 064de4e

File tree

1 file changed

+12
-1
lines changed
  • spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/run

1 file changed

+12
-1
lines changed

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/run/BootRun.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,10 +19,12 @@
1919
import java.lang.reflect.Method;
2020

2121
import org.gradle.api.file.SourceDirectorySet;
22+
import org.gradle.api.provider.Property;
2223
import org.gradle.api.tasks.Input;
2324
import org.gradle.api.tasks.JavaExec;
2425
import org.gradle.api.tasks.SourceSet;
2526
import org.gradle.api.tasks.SourceSetOutput;
27+
import org.gradle.jvm.toolchain.JavaLauncher;
2628

2729
/**
2830
* Custom {@link JavaExec} task for running a Spring Boot application.
@@ -84,6 +86,15 @@ public void exec() {
8486
}
8587

8688
private boolean isJava13OrLater() {
89+
try {
90+
Property<JavaLauncher> javaLauncher = this.getJavaLauncher();
91+
if (javaLauncher.isPresent()) {
92+
return javaLauncher.get().getMetadata().getLanguageVersion().asInt() >= 13;
93+
}
94+
}
95+
catch (NoSuchMethodError ex) {
96+
// Continue
97+
}
8798
for (Method method : String.class.getMethods()) {
8899
if (method.getName().equals("stripIndent")) {
89100
return true;

0 commit comments

Comments
 (0)