Skip to content

Commit 7625a97

Browse files
committed
Configure bootRun to use project's Java toolchain by default
Previously, unlike the application plugin's run task, our bootRun task ignored the project's Java toolchain. This meant that the application was run on a JVM with the same Java version as the one being used by Gradle itself. This could result in a failure if the application required a more modern JVM. This commit updates the plugin to configure the bootRun task's JavaLauncher convention to be one derived from the project's Java toolchain. Toolchain support was introduced in Gradle 6.7 so this is only done when using Gradle 6.7 and later. Fixes gh-24517
1 parent 064de4e commit 7625a97

File tree

1 file changed

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

1 file changed

+18
-1
lines changed

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/JavaPluginAction.java

Lines changed: 18 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.
@@ -39,9 +39,13 @@
3939
import org.gradle.api.plugins.BasePlugin;
4040
import org.gradle.api.plugins.JavaPlugin;
4141
import org.gradle.api.plugins.JavaPluginConvention;
42+
import org.gradle.api.plugins.JavaPluginExtension;
4243
import org.gradle.api.tasks.SourceSet;
4344
import org.gradle.api.tasks.TaskProvider;
4445
import org.gradle.api.tasks.compile.JavaCompile;
46+
import org.gradle.jvm.toolchain.JavaToolchainService;
47+
import org.gradle.jvm.toolchain.JavaToolchainSpec;
48+
import org.gradle.util.GradleVersion;
4549

4650
import org.springframework.boot.gradle.tasks.bundling.BootBuildImage;
4751
import org.springframework.boot.gradle.tasks.bundling.BootJar;
@@ -137,9 +141,22 @@ private void configureBootRunTask(Project project) {
137141
return Collections.emptyList();
138142
});
139143
run.conventionMapping("main", new MainClassConvention(project, run::getClasspath));
144+
configureToolchainConvention(project, run);
140145
});
141146
}
142147

148+
private void configureToolchainConvention(Project project, BootRun run) {
149+
if (isGradle67OrLater()) {
150+
JavaToolchainSpec toolchain = project.getExtensions().getByType(JavaPluginExtension.class).getToolchain();
151+
JavaToolchainService toolchainService = project.getExtensions().getByType(JavaToolchainService.class);
152+
run.getJavaLauncher().convention(toolchainService.launcherFor(toolchain));
153+
}
154+
}
155+
156+
private boolean isGradle67OrLater() {
157+
return GradleVersion.current().getBaseVersion().compareTo(GradleVersion.version("6.7")) >= 0;
158+
}
159+
143160
private JavaPluginConvention javaPluginConvention(Project project) {
144161
return project.getConvention().getPlugin(JavaPluginConvention.class);
145162
}

0 commit comments

Comments
 (0)