From 3e69c26f3a29ad4fd1abc9aadb980a9a4a3eb93d Mon Sep 17 00:00:00 2001 From: Eric Anderson Date: Mon, 31 Jul 2023 17:04:44 -0700 Subject: [PATCH] Avoid accidental Java 9+ dependency with --release In d654707 we swapped compiling the uploaded artifacts to Java 11. This caused ABI issues with ByteBuffer, like clear() returning ByteBuffer instead of Buffer. There are source-level approaches to avoid the accidental ABI dependency on Java 11, but we have no tool able to detect such breakages. We use Animalsniffer for similar cases, but it fails to detect these[1]. Since we have no tool, source-level approaches can't gain the necessary confidence that all incompatibility fixes have been resolved. Java has had javac-level ways to address this, but they used to require setting bootclasspath. Since Java 9, though, they made it easier and we can use --release, which does exactly what we need. Fixes #10432 1. https://github.com/mojohaus/animal-sniffer/issues/77 --- api/build.gradle | 10 +++++++--- build.gradle | 15 ++++++++------- gae-interop-testing/gae-jdk8/build.gradle | 3 --- servlet/build.gradle | 4 ---- servlet/jakarta/build.gradle | 2 -- 5 files changed, 15 insertions(+), 19 deletions(-) diff --git a/api/build.gradle b/api/build.gradle index 6906a2ae0d9..bc2301cee35 100644 --- a/api/build.gradle +++ b/api/build.gradle @@ -16,9 +16,13 @@ sourceSets { } } -compileContextJava { - sourceCompatibility = 1.7 - targetCompatibility = 1.7 +tasks.named("compileContextJava").configure { + if (JavaVersion.current().isJava9Compatible()) { + options.release = 7 + } else { + sourceCompatibility = 1.7 + targetCompatibility = 1.7 + } } diff --git a/build.gradle b/build.gradle index 31801ef291b..13d8d819ee4 100644 --- a/build.gradle +++ b/build.gradle @@ -181,9 +181,6 @@ subprojects { } plugins.withId("java") { - sourceCompatibility = 1.8 - targetCompatibility = 1.8 - dependencies { testImplementation libraries.junit, libraries.mockito.core, @@ -239,6 +236,14 @@ subprojects { } } + tasks.withType(JavaCompile).configureEach { + if (JavaVersion.current().isJava9Compatible()) { + options.release = 8 + } else { + sourceCompatibility = 1.8 + targetCompatibility = 1.8 + } + } tasks.named("compileJava").configure { // This project targets Java 7 (no time.Duration class) options.errorprone.check("PreferJavaTimeOverload", CheckSeverity.OFF) @@ -279,10 +284,6 @@ subprojects { plugins.withId("me.champeau.jmh") { // invoke jmh on a single benchmark class like so: // ./gradlew -PjmhIncludeSingleClass=StatsTraceContextBenchmark clean :grpc-core:jmh - tasks.named("compileJmhJava").configure { - sourceCompatibility = 1.8 - targetCompatibility = 1.8 - } tasks.named("jmh").configure { warmupIterations = 10 iterations = 10 diff --git a/gae-interop-testing/gae-jdk8/build.gradle b/gae-interop-testing/gae-jdk8/build.gradle index 78cadc60582..f3ff765ddfb 100644 --- a/gae-interop-testing/gae-jdk8/build.gradle +++ b/gae-interop-testing/gae-jdk8/build.gradle @@ -90,9 +90,6 @@ appengine { group = 'io.grpc' // Generated output GroupId version = '1.0-SNAPSHOT' // Version in generated output -sourceCompatibility = 1.8 -targetCompatibility = 1.8 - /** Returns the service name. */ String getGaeProject() { def stream = new ByteArrayOutputStream() diff --git a/servlet/build.gradle b/servlet/build.gradle index f5ef32ae11e..50f06f60ae7 100644 --- a/servlet/build.gradle +++ b/servlet/build.gradle @@ -5,10 +5,6 @@ plugins { description = "gRPC: Servlet" -// javax.servlet-api 4.0 requires a minimum of Java 8, so we might as well use that source level -sourceCompatibility = 1.8 -targetCompatibility = 1.8 - def jettyVersion = '10.0.7' configurations { diff --git a/servlet/jakarta/build.gradle b/servlet/jakarta/build.gradle index 59f5ac78d80..7b784bce9ac 100644 --- a/servlet/jakarta/build.gradle +++ b/servlet/jakarta/build.gradle @@ -4,8 +4,6 @@ plugins { } description = "gRPC: Jakarta Servlet" -sourceCompatibility = 1.8 -targetCompatibility = 1.8 // Set up classpaths and source directories for different servlet tests configurations {