Use native Gradle support for --release flag (#61093) (#61151)

With Gradle 6.6 we can now use the native support for the --release compile options. 
As Gradle by default resolves the compatibility version from the release property we needed to workaround this in order to keep our current setup. An issue was raised at gradle to track this at https://github.com/gradle/gradle/issues/14141
This commit is contained in:
Rene Groeschke 2020-08-14 20:01:24 +02:00 committed by GitHub
parent 3fef26bfb0
commit 9ca052d702
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 90 additions and 27 deletions

View File

@ -0,0 +1,68 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.gradle
import org.elasticsearch.gradle.fixtures.AbstractGradleFuncTest
class ElasticsearchJavaPluginFuncTest extends AbstractGradleFuncTest {
def "compatibility options are resolved from from build params minimum runtime version"() {
when:
buildFile.text = """
plugins {
id 'elasticsearch.global-build-info'
}
import org.elasticsearch.gradle.Architecture
import org.elasticsearch.gradle.info.BuildParams
BuildParams.init { it.setMinimumRuntimeVersion(JavaVersion.VERSION_1_10) }
apply plugin:'elasticsearch.java'
assert compileJava.sourceCompatibility == JavaVersion.VERSION_1_10.toString()
assert compileJava.targetCompatibility == JavaVersion.VERSION_1_10.toString()
"""
then:
gradleRunner("help").build()
}
def "compile option --release is configured from targetCompatibility"() {
when:
buildFile.text = """
plugins {
id 'elasticsearch.java'
}
compileJava.targetCompatibility = "1.10"
afterEvaluate {
assert compileJava.options.release.get() == 10
}
"""
then:
gradleRunner("help").build()
}
private File someJavaSource() {
file("src/main/java/org/acme/SomeClass.java") << """
package org.acme;
public class SomeClass {}
"""
}
}

View File

@ -25,7 +25,6 @@ import org.elasticsearch.gradle.info.BuildParams;
import org.elasticsearch.gradle.info.GlobalBuildInfoPlugin;
import org.elasticsearch.gradle.util.Util;
import org.gradle.api.Action;
import org.gradle.api.GradleException;
import org.gradle.api.JavaVersion;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
@ -38,8 +37,10 @@ import org.gradle.api.plugins.BasePlugin;
import org.gradle.api.plugins.JavaLibraryPlugin;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.api.tasks.bundling.Jar;
import org.gradle.api.tasks.compile.AbstractCompile;
import org.gradle.api.tasks.compile.CompileOptions;
import org.gradle.api.tasks.compile.GroovyCompile;
import org.gradle.api.tasks.compile.JavaCompile;
@ -48,11 +49,9 @@ import org.gradle.external.javadoc.CoreJavadocOptions;
import org.gradle.language.base.plugins.LifecycleBasePlugin;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import static org.elasticsearch.gradle.util.Util.toStringable;
@ -140,14 +139,6 @@ public class ElasticsearchJavaPlugin implements Plugin<Project> {
java.setSourceCompatibility(BuildParams.getMinimumRuntimeVersion());
java.setTargetCompatibility(BuildParams.getMinimumRuntimeVersion());
Function<File, String> canonicalPath = file -> {
try {
return file.getCanonicalPath();
} catch (IOException e) {
throw new GradleException("Failed to get canonical path for " + file, e);
}
};
project.afterEvaluate(p -> {
project.getTasks().withType(JavaCompile.class).configureEach(compileTask -> {
CompileOptions compileOptions = compileTask.getOptions();
@ -157,13 +148,13 @@ public class ElasticsearchJavaPlugin implements Plugin<Project> {
* -serial because we don't use java serialization.
*/
// don't even think about passing args with -J-xxx, oracle will ask you to submit a bug report :)
// fail on all javac warnings
// fail on all javac warnings.
// TODO Discuss moving compileOptions.getCompilerArgs() to use provider api with Gradle team.
List<String> compilerArgs = compileOptions.getCompilerArgs();
compilerArgs.add("-Werror");
compilerArgs.add("-Xlint:all,-path,-serial,-options,-deprecation,-try");
compilerArgs.add("-Xdoclint:all");
compilerArgs.add("-Xdoclint:-missing");
// either disable annotation processor completely (default) or allow to enable them if an annotation processor is explicitly
// defined
if (compilerArgs.contains("-processor") == false) {
@ -172,23 +163,27 @@ public class ElasticsearchJavaPlugin implements Plugin<Project> {
compileOptions.setEncoding("UTF-8");
compileOptions.setIncremental(true);
// TODO: use native Gradle support for --release when available (cf. https://github.com/gradle/gradle/issues/2510)
final JavaVersion targetCompatibilityVersion = JavaVersion.toVersion(compileTask.getTargetCompatibility());
compilerArgs.add("--release");
compilerArgs.add(targetCompatibilityVersion.getMajorVersion());
// workaround for https://github.com/gradle/gradle/issues/14141
compileTask.getConventionMapping().map("sourceCompatibility", () -> java.getSourceCompatibility().toString());
compileTask.getConventionMapping().map("targetCompatibility", () -> java.getTargetCompatibility().toString());
compileOptions.getRelease().set(releaseVersionProviderFromCompileTask(project, compileTask));
});
// also apply release flag to groovy, which is used in build-tools
project.getTasks().withType(GroovyCompile.class).configureEach(compileTask -> {
project.getTasks()
.withType(GroovyCompile.class)
.configureEach(
compileTask -> {
// TODO: this probably shouldn't apply to groovy at all?
compileTask.getOptions().getRelease().set(releaseVersionProviderFromCompileTask(project, compileTask));
}
);
});
}
// TODO: this probably shouldn't apply to groovy at all?
// TODO: use native Gradle support for --release when available (cf. https://github.com/gradle/gradle/issues/2510)
final JavaVersion targetCompatibilityVersion = JavaVersion.toVersion(compileTask.getTargetCompatibility());
final List<String> compilerArgs = compileTask.getOptions().getCompilerArgs();
compilerArgs.add("--release");
compilerArgs.add(targetCompatibilityVersion.getMajorVersion());
});
private static Provider<Integer> releaseVersionProviderFromCompileTask(Project project, AbstractCompile compileTask) {
return project.provider(() -> {
JavaVersion javaVersion = JavaVersion.toVersion(compileTask.getTargetCompatibility());
return Integer.parseInt(javaVersion.getMajorVersion());
});
}