THRIFT-5581: use gradle 7 for java/kotlin (#2601)

* upgrade gradle version to 7.4
* migrate from `maven` plugin to `maven-publish` plugin as required for Gradle 7
* add guard to ktfmt, since it can't run with JDK 8

Co-authored-by: Christopher Tubbs <ctubbsii@apache.org>
diff --git a/lib/kotlin/build.gradle.kts b/lib/kotlin/build.gradle.kts
index 1f7711a..90b9e80 100644
--- a/lib/kotlin/build.gradle.kts
+++ b/lib/kotlin/build.gradle.kts
@@ -41,8 +41,10 @@
 }
 
 tasks {
-    ktfmt {
-        kotlinLangStyle()
+    if (JavaVersion.current().isJava11Compatible) {
+        ktfmt {
+            kotlinLangStyle()
+        }
     }
 
     test {
diff --git a/lib/kotlin/cross-test-client/build.gradle.kts b/lib/kotlin/cross-test-client/build.gradle.kts
index deda467..ff48eed 100644
--- a/lib/kotlin/cross-test-client/build.gradle.kts
+++ b/lib/kotlin/cross-test-client/build.gradle.kts
@@ -19,9 +19,9 @@
 
 plugins {
     kotlin("jvm")
-    id("com.ncorti.ktfmt.gradle")
     java
     application
+    id("com.ncorti.ktfmt.gradle")
 }
 
 repositories {
@@ -62,8 +62,10 @@
         mainClass.set("org.apache.thrift.test.TestClientKt")
     }
 
-    ktfmt {
-        kotlinLangStyle()
+    if (JavaVersion.current().isJava11Compatible) {
+        ktfmt {
+            kotlinLangStyle()
+        }
     }
 
     task<Exec>("compileThrift") {
diff --git a/lib/kotlin/cross-test-client/src/main/kotlin/org/apache/thrift/test/TestClient.kt b/lib/kotlin/cross-test-client/src/main/kotlin/org/apache/thrift/test/TestClient.kt
index 28f9ffd..39500df 100644
--- a/lib/kotlin/cross-test-client/src/main/kotlin/org/apache/thrift/test/TestClient.kt
+++ b/lib/kotlin/cross-test-client/src/main/kotlin/org/apache/thrift/test/TestClient.kt
@@ -77,30 +77,27 @@
 }
 
 class TestClient : CliktCommand() {
-    private val host: String by option(help = "The cross test host to connect to")
-        .default("localhost")
+    private val host: String by
+        option(help = "The cross test host to connect to").default("localhost")
     private val port: Int by option(help = "The cross test port to connect to").int().default(9090)
-    private val numTests: Int by option("--testloops", "--n", help = "Number of runs in this test")
-        .int()
-        .default(1)
-    private val protocolType: ProtocolType by option("--protocol", help = "Protocol type")
-        .enum<ProtocolType> { it.key }
-        .default(ProtocolType.Binary)
-    private val transportType: TransportType by option("--transport", help = "Transport type")
-        .enum<TransportType> { it.key }
-        .default(TransportType.Buffered)
-    private val useHttpClient: Boolean by option("--client", help = "Use http client")
-        .flag(default = false)
-    private val useSSL: Boolean by option("--ssl", help = "Use SSL for encrypted transport")
-        .flag(default = false)
-    private val useZlib: Boolean by option(
-            "--zlib",
-            help = "Use zlib wrapper for compressed transport"
-        )
-        .flag(default = false)
-    private val socketTimeout: Int by option("--timeout", help = "Socket timeout")
-        .int()
-        .default(1000)
+    private val numTests: Int by
+        option("--testloops", "--n", help = "Number of runs in this test").int().default(1)
+    private val protocolType: ProtocolType by
+        option("--protocol", help = "Protocol type")
+            .enum<ProtocolType> { it.key }
+            .default(ProtocolType.Binary)
+    private val transportType: TransportType by
+        option("--transport", help = "Transport type")
+            .enum<TransportType> { it.key }
+            .default(TransportType.Buffered)
+    private val useHttpClient: Boolean by
+        option("--client", help = "Use http client").flag(default = false)
+    private val useSSL: Boolean by
+        option("--ssl", help = "Use SSL for encrypted transport").flag(default = false)
+    private val useZlib: Boolean by
+        option("--zlib", help = "Use zlib wrapper for compressed transport").flag(default = false)
+    private val socketTimeout: Int by
+        option("--timeout", help = "Socket timeout").int().default(1000)
 
     private fun createProtocol(transport: TTransport): TProtocol =
         when (protocolType) {
diff --git a/lib/kotlin/cross-test-server/build.gradle.kts b/lib/kotlin/cross-test-server/build.gradle.kts
index 508b29d..eda1ebd 100644
--- a/lib/kotlin/cross-test-server/build.gradle.kts
+++ b/lib/kotlin/cross-test-server/build.gradle.kts
@@ -19,9 +19,9 @@
 
 plugins {
     kotlin("jvm")
-    id("com.ncorti.ktfmt.gradle")
     java
     application
+    id("com.ncorti.ktfmt.gradle")
 }
 
 repositories {
@@ -60,8 +60,10 @@
         mainClass.set("org.apache.thrift.test.TestServerKt")
     }
 
-    ktfmt {
-        kotlinLangStyle()
+    if (JavaVersion.current().isJava11Compatible) {
+        ktfmt {
+            kotlinLangStyle()
+        }
     }
 
     task<Exec>("compileThrift") {
diff --git a/lib/kotlin/cross-test-server/src/main/kotlin/org/apache/thrift/test/TestServer.kt b/lib/kotlin/cross-test-server/src/main/kotlin/org/apache/thrift/test/TestServer.kt
index d9c0c86..566e53d 100644
--- a/lib/kotlin/cross-test-server/src/main/kotlin/org/apache/thrift/test/TestServer.kt
+++ b/lib/kotlin/cross-test-server/src/main/kotlin/org/apache/thrift/test/TestServer.kt
@@ -167,17 +167,18 @@
 
 class TestServerCommand : CliktCommand() {
     private val port: Int by option(help = "The cross test port to connect to").int().default(9090)
-    private val protocolType: ProtocolType by option("--protocol", help = "Protocol type")
-        .enum<ProtocolType> { it.key }
-        .default(ProtocolType.Binary)
-    private val transportType: TransportType by option("--transport", help = "Transport type")
-        .enum<TransportType> { it.key }
-        .default(TransportType.Buffered)
-    private val serverType: ServerType by option("--server-type")
-        .enum<ServerType> { it.key }
-        .default(ServerType.NonBlocking)
-    private val useSSL: Boolean by option("--ssl", help = "Use SSL for encrypted transport")
-        .flag(default = false)
+    private val protocolType: ProtocolType by
+        option("--protocol", help = "Protocol type")
+            .enum<ProtocolType> { it.key }
+            .default(ProtocolType.Binary)
+    private val transportType: TransportType by
+        option("--transport", help = "Transport type")
+            .enum<TransportType> { it.key }
+            .default(TransportType.Buffered)
+    private val serverType: ServerType by
+        option("--server-type").enum<ServerType> { it.key }.default(ServerType.NonBlocking)
+    private val useSSL: Boolean by
+        option("--ssl", help = "Use SSL for encrypted transport").flag(default = false)
     private val stringLimit: Long by option("--string-limit").long().default(-1)
     private val containerLimit: Long by option("--container-limit").long().default(-1)
 
diff --git a/lib/kotlin/settings.gradle.kts b/lib/kotlin/settings.gradle.kts
index a6bdbbc..2eff402 100644
--- a/lib/kotlin/settings.gradle.kts
+++ b/lib/kotlin/settings.gradle.kts
@@ -18,8 +18,15 @@
  */
 pluginManagement {
     plugins {
-        kotlin("jvm") version "1.5.31"
-        id("com.ncorti.ktfmt.gradle") version "0.4.0"
+        kotlin("jvm") version "1.6.10"
+        // per https://github.com/cortinico/ktfmt-gradle/tags the latest version requires Gradle 7.4 which in turn
+        // requires Java 11, failing the xenial build which is still on Java 8. Here we both configure the plugin
+        // but with different version, and only enabling ktfmt configure for Java 11+, to make the legacy build happy
+        if (JavaVersion.current().isJava11Compatible) {
+            id("com.ncorti.ktfmt.gradle") version "0.8.0"
+        } else {
+            id("com.ncorti.ktfmt.gradle") version "0.5.0"
+        }
     }
 }