Skip to content

Commit

Permalink
Provision min runtime version jdk for compilation (#105152)
Browse files Browse the repository at this point in the history
This commit adjusts compile tasks to explicitly provision a Java
toolchain for the Java minimum runtime version. By doing so the Java
used by Gradle may be upgraded without the possibility of causing
spurious warnings from javac which could fail the build, such as when
new warnings are added in later JDK versions.
  • Loading branch information
rjernst authored Feb 6, 2024
1 parent ac09d75 commit e8c2f4f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,26 @@
import org.gradle.api.tasks.compile.CompileOptions;
import org.gradle.api.tasks.compile.GroovyCompile;
import org.gradle.api.tasks.compile.JavaCompile;
import org.gradle.jvm.toolchain.JavaLanguageVersion;
import org.gradle.jvm.toolchain.JavaToolchainService;

import java.util.List;

import javax.inject.Inject;

/**
* A wrapper around Gradle's Java Base plugin that applies our
* common configuration for production code.
*/
public class ElasticsearchJavaBasePlugin implements Plugin<Project> {

private final JavaToolchainService javaToolchains;

@Inject
ElasticsearchJavaBasePlugin(JavaToolchainService javaToolchains) {
this.javaToolchains = javaToolchains;
}

@Override
public void apply(Project project) {
// make sure the global build info plugin is applied to the root project
Expand Down Expand Up @@ -103,7 +115,7 @@ private static void disableTransitiveDependenciesForSourceSet(Project project, S
/**
* Adds compiler settings to the project
*/
public static void configureCompile(Project project) {
public void configureCompile(Project project) {
project.getExtensions().getExtraProperties().set("compactProfile", "full");
JavaPluginExtension java = project.getExtensions().getByType(JavaPluginExtension.class);
if (BuildParams.getJavaToolChainSpec().isPresent()) {
Expand All @@ -112,6 +124,10 @@ public static void configureCompile(Project project) {
java.setSourceCompatibility(BuildParams.getMinimumRuntimeVersion());
java.setTargetCompatibility(BuildParams.getMinimumRuntimeVersion());
project.getTasks().withType(JavaCompile.class).configureEach(compileTask -> {
compileTask.getJavaCompiler().set(javaToolchains.compilerFor(spec -> {
spec.getLanguageVersion().set(JavaLanguageVersion.of(BuildParams.getMinimumRuntimeVersion().getMajorVersion()));
}));

CompileOptions compileOptions = compileTask.getOptions();
/*
* -path because gradle will send in paths that don't always exist.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.elasticsearch.gradle.util.GradleUtils;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.plugins.JavaLibraryPlugin;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.tasks.SourceSet;
Expand Down Expand Up @@ -50,7 +49,7 @@ public class MrjarPlugin implements Plugin<Project> {

@Override
public void apply(Project project) {
project.getPluginManager().apply(JavaLibraryPlugin.class);
project.getPluginManager().apply(ElasticsearchJavaBasePlugin.class);
var javaExtension = project.getExtensions().getByType(JavaPluginExtension.class);

var srcDir = project.getProjectDir().toPath().resolve("src");
Expand Down

0 comments on commit e8c2f4f

Please sign in to comment.