Skip to content

Commit

Permalink
Configure bootRun to use project's Java toolchain by default
Browse files Browse the repository at this point in the history
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
  • Loading branch information
wilkinsona committed Jan 11, 2021
1 parent 064de4e commit 7625a97
Showing 1 changed file with 18 additions and 1 deletion.
@@ -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.
Expand Down Expand Up @@ -39,9 +39,13 @@
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;
Expand Down Expand Up @@ -137,9 +141,22 @@ private void configureBootRunTask(Project project) {
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);
}
Expand Down

0 comments on commit 7625a97

Please sign in to comment.