THRIFT-5584: use gradle toolchain to specify Java 11 with --release 8 (#2606)

* use gradle toolchain to specify Java 11 with --release 8
* use newer syntax
* fix spotless apply
* remove legacy apply and bump version of spot bugs
* migrate pmd to new plugin syntax
diff --git a/lib/java/gradle/codeQualityChecks.gradle b/lib/java/gradle/codeQualityChecks.gradle
index b8d13f9..0ce47f7 100644
--- a/lib/java/gradle/codeQualityChecks.gradle
+++ b/lib/java/gradle/codeQualityChecks.gradle
@@ -5,11 +5,9 @@
 
 dependencies {
     spotbugs configurations.spotbugsPlugins.dependencies
-    spotbugsPlugins 'com.h3xstream.findsecbugs:findsecbugs-plugin:1.11.0'
+    spotbugsPlugins 'com.h3xstream.findsecbugs:findsecbugs-plugin:1.12.0'
 }
 
-apply plugin: 'com.github.spotbugs'
-
 // see https://spotbugs-gradle-plugin.netlify.app/com/github/spotbugs/snom/spotbugsextension
 spotbugs {
     ignoreFailures = true
@@ -28,8 +26,6 @@
     }
 }
 
-apply plugin: 'pmd'
-
 pmd {
     ignoreFailures = true
     toolVersion = '6.0.0'
@@ -46,9 +42,7 @@
 
 spotless {
     java {
-        target project.fileTree(project.rootDir) {
-          include 'src/**/*.java'
-        }
+        target 'src/**/*.java'
         googleJavaFormat()
     }
 }
diff --git a/lib/java/gradle/environment.gradle b/lib/java/gradle/environment.gradle
index 224f10f..f25f3e6 100644
--- a/lib/java/gradle/environment.gradle
+++ b/lib/java/gradle/environment.gradle
@@ -29,13 +29,8 @@
 ext.installPath = property('install.path')
 ext.installJavadocPath = property('install.javadoc.path')
 
-ext.thriftRoot = file('../..')
-
-if (hasProperty('thrift.compiler')) {
-    ext.thriftCompiler = property('thrift.compiler')
-} else {
-    ext.thriftCompiler = "$thriftRoot/compiler/cpp/thrift"
-}
+ext.thriftRoot = rootProject.file('../..')
+ext.thriftCompiler = findProperty('thrift.compiler') ?: "$thriftRoot/compiler/cpp/thrift"
 
 ext.mvnRepo = property('mvn.repo')
 ext.apacheRepo = property('apache.repo')
@@ -50,6 +45,7 @@
 ext.junitVersion = property('junit.version')
 ext.mockitoVersion = property('mockito.version')
 ext.javaxAnnotationVersion = property('javax.annotation.version')
+ext.commonsLang3Version = property('commons-lang3.version')
 
 // In this section you declare where to find the dependencies of your project
 repositories {
@@ -69,7 +65,7 @@
     implementation "org.apache.httpcomponents:httpcore:${httpcoreVersion}"
     implementation "javax.servlet:javax.servlet-api:${servletVersion}"
     implementation "javax.annotation:javax.annotation-api:${javaxAnnotationVersion}"
-    implementation "org.apache.commons:commons-lang3:3.12.0"
+    implementation "org.apache.commons:commons-lang3:${commonsLang3Version}"
 
     testImplementation "org.junit.jupiter:junit-jupiter:${junitVersion}"
     testImplementation "org.mockito:mockito-all:${mockitoVersion}"
diff --git a/lib/java/gradle/sourceConfiguration.gradle b/lib/java/gradle/sourceConfiguration.gradle
index 97ce1b9..044a114 100644
--- a/lib/java/gradle/sourceConfiguration.gradle
+++ b/lib/java/gradle/sourceConfiguration.gradle
@@ -21,21 +21,27 @@
 // ----------------------------------------------------------------------------
 // Compiler configuration details
 
-// These two properties are still needed on JDK8, and possibly used directly by
-// plugins. However, the '--release' option added below makes these two
-// properties redundant when building with JDK9 or later.
-sourceCompatibility = '1.8'
-targetCompatibility = '1.8'
+// We are using Java 11 toolchain to compile.
+// This enables decoupling from the Java version that gradle runs, from
+// the actual JDK version for the project. For more details, see
+// https://docs.gradle.org/current/userguide/toolchains.html
+//
+// The '--release' option added below makes sure that even if we are using
+// the toolchain version > 8, the final artifact is at version 8. There is
+// also a runtime CI that's based on Java 8 to ensure that.
+java {
+    toolchain {
+        languageVersion = JavaLanguageVersion.of(11)
+    }
+}
 
-tasks.withType(JavaCompile) {
+tasks.withType(JavaCompile).configureEach {
     options.encoding = 'UTF-8'
     options.debug = true
     options.deprecation = true
     // the following is to build with Java 8 specifications, even when building with JDK9 or later
-    if (JavaVersion.current() > JavaVersion.VERSION_1_8) {
-        options.compilerArgs.addAll(['--release', '8'])
-    }
-    options.compilerArgs.addAll([
+    options.release = 8
+    options.compilerArgs += [
             '-Werror',
             '-Xlint:deprecation',
             '-Xlint:cast',
@@ -44,7 +50,7 @@
             '-Xlint:finally',
             '-Xlint:overrides',
             // we can't enable -Xlint:unchecked just yet
-    ])
+    ]
 }
 
 // ----------------------------------------------------------------------------