From e361c21e55c63ae88aef972db9b63eadffe9372f 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 384ab9be1c3..2d94ed0eb0b 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 + } } tasks.named("jar").configure { diff --git a/build.gradle b/build.gradle index deaa6d82ef6..a0d20cafebd 100644 --- a/build.gradle +++ b/build.gradle @@ -182,9 +182,6 @@ subprojects { } plugins.withId("java") { - sourceCompatibility = 1.8 - targetCompatibility = 1.8 - dependencies { testImplementation libraries.junit, libraries.mockito.core, @@ -240,6 +237,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) @@ -285,10 +290,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 3ed315b388f..71f3dfb38d4 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 8df1b93d83a..40f61c0a407 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 {