Skip to content

Commit f38ac8a

Browse files
authored
feat(build): Add Checkstyle plugin and an IllegalImport rule (#1880)
1 parent 2d3d0d0 commit f38ac8a

File tree

6 files changed

+73
-46
lines changed

6 files changed

+73
-46
lines changed

build-logic/src/main/kotlin/polaris-java.gradle.kts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,31 @@ plugins {
3030
`java-library`
3131
`java-test-fixtures`
3232
`jvm-test-suite`
33+
checkstyle
3334
id("polaris-spotless")
3435
id("jacoco-report-aggregation")
3536
id("net.ltgt.errorprone")
3637
}
3738

3839
apply<PublishingHelperPlugin>()
3940

40-
if (project.extra.has("duplicated-project-sources")) {
41-
// skip the style check for duplicated projects
42-
tasks.withType<Checkstyle>().configureEach { enabled = false }
41+
checkstyle {
42+
val checkstyleVersion =
43+
versionCatalogs
44+
.named("libs")
45+
.findVersion("checkstyle")
46+
.orElseThrow { GradleException("checkstyle version not found in libs.versions.toml") }
47+
.requiredVersion
48+
toolVersion = checkstyleVersion
49+
configFile = rootProject.file("codestyle/checkstyle.xml")
50+
isIgnoreFailures = false
51+
maxErrors = 0
52+
maxWarnings = 0
4353
}
4454

55+
// Ensure Checkstyle runs after jandex to avoid task dependency issues
56+
tasks.withType<Checkstyle>().configureEach { tasks.findByName("jandex")?.let { mustRunAfter(it) } }
57+
4558
tasks.withType(JavaCompile::class.java).configureEach {
4659
options.compilerArgs.addAll(listOf("-Xlint:unchecked", "-Xlint:deprecation"))
4760
options.errorprone.disableAllWarnings = true

build-logic/src/main/kotlin/polaris-spotless.gradle.kts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
*/
1919

2020
import com.diffplug.spotless.FormatterFunc
21-
import gradle.kotlin.dsl.accessors._fa00c0b20184971a79f32516372275b9.java
22-
import gradle.kotlin.dsl.accessors._fa00c0b20184971a79f32516372275b9.spotless
2321
import java.io.Serializable
2422
import org.gradle.api.GradleException
2523

codestyle/checkstyle.xml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one
4+
or more contributor license agreements. See the NOTICE file
5+
distributed with this work for additional information
6+
regarding copyright ownership. The ASF licenses this file
7+
to you under the Apache License, Version 2.0 (the
8+
"License"); you may not use this file except in compliance
9+
with the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing,
14+
software distributed under the License is distributed on an
15+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
KIND, either express or implied. See the License for the
17+
specific language governing permissions and limitations
18+
under the License.
19+
-->
20+
<!DOCTYPE module PUBLIC
21+
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
22+
"https://checkstyle.org/dtds/configuration_1_3.dtd">
23+
24+
<module name="Checker">
25+
<property name="charset" value="UTF-8"/>
26+
<property name="severity" value="warning"/>
27+
<property name="fileExtensions" value="java, properties, xml"/>
28+
29+
<!-- Excludes all 'module-info.java' files -->
30+
<!-- See https://checkstyle.org/config_filefilters.html -->
31+
<module name="BeforeExecutionExclusionFileFilter">
32+
<property name="fileNamePattern" value="module\-info\.java$"/>
33+
</module>
34+
35+
<!-- https://checkstyle.org/config_filters.html#SuppressionFilter -->
36+
<module name="SuppressionFilter">
37+
<property name="file" value="${org.checkstyle.google.suppressionfilter.config}"
38+
default="checkstyle-suppressions.xml" />
39+
<property name="optional" value="true"/>
40+
</module>
41+
42+
<module name="TreeWalker">
43+
<!-- Checks for imports -->
44+
<!-- See http://checkstyle.org/config_imports.html -->
45+
<module name="IllegalImport">
46+
<property name="illegalPkgs" value=".*\.shaded\..*, .*\.relocated\..*"/>
47+
<property name="regexp" value="true"/>
48+
</module>
49+
50+
</module>
51+
</module>

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#
1919

2020
[versions]
21+
checkstyle = "10.25.0"
2122
hadoop = "3.4.1"
2223
iceberg = "1.9.0" # Ensure to update the iceberg version in regtests to keep regtests up-to-date
2324
quarkus = "3.21.4"

plugins/spark/v3.5/spark/build.gradle.kts

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -112,44 +112,6 @@ dependencies {
112112
}
113113
}
114114

115-
// TODO: replace the check using gradlew checkstyle plugin
116-
tasks.register("checkNoDisallowedImports") {
117-
doLast {
118-
// List of disallowed imports. Right now, we disallow usage of shaded or
119-
// relocated libraries in the iceberg spark runtime jar.
120-
val disallowedImports =
121-
listOf("import org.apache.iceberg.shaded.", "org.apache.iceberg.relocated.")
122-
123-
// Directory to scan for Java files
124-
val sourceDirs = listOf(file("src/main/java"), file("src/test/java"))
125-
126-
val violations = mutableListOf<String>()
127-
// Scan Java files in each directory
128-
sourceDirs.forEach { sourceDir ->
129-
fileTree(sourceDir)
130-
.matching {
131-
include("**/*.java") // Only include Java files
132-
}
133-
.forEach { file ->
134-
val content = file.readText()
135-
disallowedImports.forEach { importStatement ->
136-
if (content.contains(importStatement)) {
137-
violations.add(
138-
"Disallowed import found in ${file.relativeTo(projectDir)}: $importStatement"
139-
)
140-
}
141-
}
142-
}
143-
}
144-
145-
if (violations.isNotEmpty()) {
146-
throw GradleException("Disallowed imports found! $violations")
147-
}
148-
}
149-
}
150-
151-
tasks.named("check") { dependsOn("checkNoDisallowedImports") }
152-
153115
tasks.register<ShadowJar>("createPolarisSparkJar") {
154116
archiveClassifier = "bundle"
155117
isZip64 = true

quarkus/test-commons/build.gradle.kts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ plugins {
2424
}
2525

2626
configurations.all {
27-
exclude(group = "org.antlr", module = "antlr4-runtime")
28-
exclude(group = "org.scala-lang", module = "scala-library")
29-
exclude(group = "org.scala-lang", module = "scala-reflect")
27+
if (name != "checkstyle") {
28+
exclude(group = "org.antlr", module = "antlr4-runtime")
29+
exclude(group = "org.scala-lang", module = "scala-library")
30+
exclude(group = "org.scala-lang", module = "scala-reflect")
31+
}
3032
}
3133

3234
dependencies {

0 commit comments

Comments
 (0)