From 7f68ff2ce430e348d5c538d6f9f3ac7826e7f01f Mon Sep 17 00:00:00 2001 From: Alexandre Dutra Date: Thu, 12 Jun 2025 17:13:08 +0200 Subject: [PATCH 1/3] feat(build): Add Checkstyle plugin and an IllegalImport rule --- .../src/main/kotlin/polaris-java.gradle.kts | 22 ++++++-- .../main/kotlin/polaris-spotless.gradle.kts | 2 - codestyle/checkstyle.xml | 52 +++++++++++++++++++ gradle/libs.versions.toml | 1 + plugins/spark/v3.5/spark/build.gradle.kts | 38 -------------- quarkus/test-commons/build.gradle.kts | 8 +-- 6 files changed, 77 insertions(+), 46 deletions(-) create mode 100644 codestyle/checkstyle.xml diff --git a/build-logic/src/main/kotlin/polaris-java.gradle.kts b/build-logic/src/main/kotlin/polaris-java.gradle.kts index 2c45da15f6..2e76e0095f 100644 --- a/build-logic/src/main/kotlin/polaris-java.gradle.kts +++ b/build-logic/src/main/kotlin/polaris-java.gradle.kts @@ -30,6 +30,7 @@ plugins { `java-library` `java-test-fixtures` `jvm-test-suite` + checkstyle id("polaris-spotless") id("jacoco-report-aggregation") id("net.ltgt.errorprone") @@ -37,11 +38,26 @@ plugins { apply() -if (project.extra.has("duplicated-project-sources")) { - // skip the style check for duplicated projects - tasks.withType().configureEach { enabled = false } +val checkstyleVersion = + versionCatalogs + .named("libs") + .findVersion("checkstyle") + .orElseThrow { GradleException("checkstyle version not found in libs.versions.toml") } + .requiredVersion + +checkstyle { + toolVersion = checkstyleVersion + configFile = rootProject.file("codestyle/checkstyle.xml") + isIgnoreFailures = false + maxErrors = 0 + maxWarnings = 0 } +// Ensure Checkstyle runs after jandex to avoid task dependency issues +tasks + .matching { it.name.startsWith("checkstyle") } + .configureEach { mustRunAfter(tasks.matching { it.name == "jandex" }) } + tasks.withType(JavaCompile::class.java).configureEach { options.compilerArgs.addAll(listOf("-Xlint:unchecked", "-Xlint:deprecation")) options.errorprone.disableAllWarnings = true diff --git a/build-logic/src/main/kotlin/polaris-spotless.gradle.kts b/build-logic/src/main/kotlin/polaris-spotless.gradle.kts index 7e878290e8..78119b007f 100644 --- a/build-logic/src/main/kotlin/polaris-spotless.gradle.kts +++ b/build-logic/src/main/kotlin/polaris-spotless.gradle.kts @@ -18,8 +18,6 @@ */ import com.diffplug.spotless.FormatterFunc -import gradle.kotlin.dsl.accessors._fa00c0b20184971a79f32516372275b9.java -import gradle.kotlin.dsl.accessors._fa00c0b20184971a79f32516372275b9.spotless import java.io.Serializable import org.gradle.api.GradleException diff --git a/codestyle/checkstyle.xml b/codestyle/checkstyle.xml new file mode 100644 index 0000000000..508557746f --- /dev/null +++ b/codestyle/checkstyle.xml @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 7733e9c9f5..82dfa160b5 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -18,6 +18,7 @@ # [versions] +checkstyle = "10.25.0" hadoop = "3.4.1" iceberg = "1.9.0" # Ensure to update the iceberg version in regtests to keep regtests up-to-date quarkus = "3.21.4" diff --git a/plugins/spark/v3.5/spark/build.gradle.kts b/plugins/spark/v3.5/spark/build.gradle.kts index 8e2f3a354f..a2a54e26be 100644 --- a/plugins/spark/v3.5/spark/build.gradle.kts +++ b/plugins/spark/v3.5/spark/build.gradle.kts @@ -112,44 +112,6 @@ dependencies { } } -// TODO: replace the check using gradlew checkstyle plugin -tasks.register("checkNoDisallowedImports") { - doLast { - // List of disallowed imports. Right now, we disallow usage of shaded or - // relocated libraries in the iceberg spark runtime jar. - val disallowedImports = - listOf("import org.apache.iceberg.shaded.", "org.apache.iceberg.relocated.") - - // Directory to scan for Java files - val sourceDirs = listOf(file("src/main/java"), file("src/test/java")) - - val violations = mutableListOf() - // Scan Java files in each directory - sourceDirs.forEach { sourceDir -> - fileTree(sourceDir) - .matching { - include("**/*.java") // Only include Java files - } - .forEach { file -> - val content = file.readText() - disallowedImports.forEach { importStatement -> - if (content.contains(importStatement)) { - violations.add( - "Disallowed import found in ${file.relativeTo(projectDir)}: $importStatement" - ) - } - } - } - } - - if (violations.isNotEmpty()) { - throw GradleException("Disallowed imports found! $violations") - } - } -} - -tasks.named("check") { dependsOn("checkNoDisallowedImports") } - tasks.register("createPolarisSparkJar") { archiveClassifier = "bundle" isZip64 = true diff --git a/quarkus/test-commons/build.gradle.kts b/quarkus/test-commons/build.gradle.kts index 61d26d54cd..cb6a40d45f 100644 --- a/quarkus/test-commons/build.gradle.kts +++ b/quarkus/test-commons/build.gradle.kts @@ -24,9 +24,11 @@ plugins { } configurations.all { - exclude(group = "org.antlr", module = "antlr4-runtime") - exclude(group = "org.scala-lang", module = "scala-library") - exclude(group = "org.scala-lang", module = "scala-reflect") + if (name != "checkstyle") { + exclude(group = "org.antlr", module = "antlr4-runtime") + exclude(group = "org.scala-lang", module = "scala-library") + exclude(group = "org.scala-lang", module = "scala-reflect") + } } dependencies { From 52ffa1612f5483ceb9768f1952b8c64ee055f79b Mon Sep 17 00:00:00 2001 From: Alexandre Dutra Date: Fri, 13 Jun 2025 15:20:43 +0200 Subject: [PATCH 2/3] review --- .../src/main/kotlin/polaris-java.gradle.kts | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/build-logic/src/main/kotlin/polaris-java.gradle.kts b/build-logic/src/main/kotlin/polaris-java.gradle.kts index 2e76e0095f..4370b5518c 100644 --- a/build-logic/src/main/kotlin/polaris-java.gradle.kts +++ b/build-logic/src/main/kotlin/polaris-java.gradle.kts @@ -38,14 +38,13 @@ plugins { apply() -val checkstyleVersion = - versionCatalogs - .named("libs") - .findVersion("checkstyle") - .orElseThrow { GradleException("checkstyle version not found in libs.versions.toml") } - .requiredVersion - checkstyle { + val checkstyleVersion = + versionCatalogs + .named("libs") + .findVersion("checkstyle") + .orElseThrow { GradleException("checkstyle version not found in libs.versions.toml") } + .requiredVersion toolVersion = checkstyleVersion configFile = rootProject.file("codestyle/checkstyle.xml") isIgnoreFailures = false @@ -54,9 +53,7 @@ checkstyle { } // Ensure Checkstyle runs after jandex to avoid task dependency issues -tasks - .matching { it.name.startsWith("checkstyle") } - .configureEach { mustRunAfter(tasks.matching { it.name == "jandex" }) } +tasks.withType().configureEach { tasks.findByName("jandex")?.let { mustRunAfter(it) } } tasks.withType(JavaCompile::class.java).configureEach { options.compilerArgs.addAll(listOf("-Xlint:unchecked", "-Xlint:deprecation")) From 79d22a74f51e9efd21616bd24f29a77f54910e00 Mon Sep 17 00:00:00 2001 From: Alexandre Dutra Date: Fri, 13 Jun 2025 17:16:54 +0200 Subject: [PATCH 3/3] use regexp --- codestyle/checkstyle.xml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/codestyle/checkstyle.xml b/codestyle/checkstyle.xml index 508557746f..d3986dc3e7 100644 --- a/codestyle/checkstyle.xml +++ b/codestyle/checkstyle.xml @@ -43,9 +43,8 @@ - - + +