Skip to content

Commit ebfa771

Browse files
ctubbsiiJens-G
authored andcommitted
THRIFT-5274: Enforce Java 8 compatibility
Client: Java Patch: Christopher Tubbs This closes #2325 * Enforce Java 8 compatibility using the new `--release` flag introduced in JDK9, so that all generated bytecode follows Java 8 strict compatibility, even when building with newer JDK versions (9 or later) (this fixes NoSuchMethodError with ByteBuffer, and other potential incompatibilities in bytecode generation that would make the code unable to run on a Java 8 JRE) * Also strictly enforce the JDK version used to build the project by ensuring it is at least version 1.8, and will fail fast when building the Java libraries if this condition is not met.
1 parent 518163a commit ebfa771

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

lib/java/build.gradle

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919

2020
// Using the legacy plugin classpath for Clover so it can be loaded optionally
2121
buildscript {
22+
// strictly enforce the minimum version of Java required to build and fail fast
23+
if (JavaVersion.current() < JavaVersion.VERSION_1_8) {
24+
throw new GradleException("The java version used is ${JavaVersion.current()}, but must be at least ${JavaVersion.VERSION_1_8}")
25+
}
26+
2227
repositories {
2328
mavenCentral()
2429
google()

lib/java/gradle/sourceConfiguration.gradle

+7
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,20 @@ sourceSets {
4646
// ----------------------------------------------------------------------------
4747
// Compiler configuration details
4848

49+
// These two properties are still needed on JDK8, and possibly used directly by
50+
// plugins. However, the '--release' option added below makes these two
51+
// properties redundant when building with JDK9 or later.
4952
sourceCompatibility = '1.8'
5053
targetCompatibility = '1.8'
5154

5255
tasks.withType(JavaCompile) {
5356
options.encoding = 'UTF-8'
5457
options.debug = true
5558
options.deprecation = true
59+
// the following is to build with Java 8 specifications, even when building with JDK9 or later
60+
if (JavaVersion.current() > JavaVersion.VERSION_1_8) {
61+
options.compilerArgs.addAll(['--release', '8'])
62+
}
5663
// options.compilerArgs.addAll('-Xlint:unchecked')
5764
}
5865

0 commit comments

Comments
 (0)