From 7625a979db9d8fd8233b675a3051393b4ac620f8 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 11 Jan 2021 11:17:03 +0000 Subject: [PATCH] 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 --- .../boot/gradle/plugin/JavaPluginAction.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/JavaPluginAction.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/JavaPluginAction.java index 164f6a02ee..6583e87557 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/JavaPluginAction.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/JavaPluginAction.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. @@ -39,9 +39,13 @@ import org.gradle.api.plugins.ApplicationPlugin; import org.gradle.api.plugins.BasePlugin; import org.gradle.api.plugins.JavaPlugin; import org.gradle.api.plugins.JavaPluginConvention; +import org.gradle.api.plugins.JavaPluginExtension; import org.gradle.api.tasks.SourceSet; import org.gradle.api.tasks.TaskProvider; import org.gradle.api.tasks.compile.JavaCompile; +import org.gradle.jvm.toolchain.JavaToolchainService; +import org.gradle.jvm.toolchain.JavaToolchainSpec; +import org.gradle.util.GradleVersion; import org.springframework.boot.gradle.tasks.bundling.BootBuildImage; import org.springframework.boot.gradle.tasks.bundling.BootJar; @@ -137,9 +141,22 @@ final class JavaPluginAction implements PluginApplicationAction { return Collections.emptyList(); }); run.conventionMapping("main", new MainClassConvention(project, run::getClasspath)); + configureToolchainConvention(project, run); }); } + private void configureToolchainConvention(Project project, BootRun run) { + if (isGradle67OrLater()) { + JavaToolchainSpec toolchain = project.getExtensions().getByType(JavaPluginExtension.class).getToolchain(); + JavaToolchainService toolchainService = project.getExtensions().getByType(JavaToolchainService.class); + run.getJavaLauncher().convention(toolchainService.launcherFor(toolchain)); + } + } + + private boolean isGradle67OrLater() { + return GradleVersion.current().getBaseVersion().compareTo(GradleVersion.version("6.7")) >= 0; + } + private JavaPluginConvention javaPluginConvention(Project project) { return project.getConvention().getPlugin(JavaPluginConvention.class); }