KAFKA-10787 Add import ordering checkstyle rule and configure an automatic formatter (#16097)

Reviewers: Chia-Ping Tsai <chia7712@gmail.com>
This commit is contained in:
gongxuanzhang 2024-06-03 04:02:07 +08:00 committed by GitHub
parent 0c9c1d405d
commit 342e69192f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 2 deletions

View File

@ -227,11 +227,16 @@ There are two code quality analysis tools that we regularly run, spotbugs and ch
Checkstyle enforces a consistent coding style in Kafka. Checkstyle enforces a consistent coding style in Kafka.
You can run checkstyle using: You can run checkstyle using:
./gradlew checkstyleMain checkstyleTest ./gradlew checkstyleMain checkstyleTest spotlessCheck
The checkstyle warnings will be found in `reports/checkstyle/reports/main.html` and `reports/checkstyle/reports/test.html` files in the The checkstyle warnings will be found in `reports/checkstyle/reports/main.html` and `reports/checkstyle/reports/test.html` files in the
subproject build directories. They are also printed to the console. The build will fail if Checkstyle fails. subproject build directories. They are also printed to the console. The build will fail if Checkstyle fails.
#### Spotless ####
The import order is a part of static check. please call `spotlessApply` to optimize the imports of Java codes before filing pull request :
./gradlew spotlessApply
#### Spotbugs #### #### Spotbugs ####
Spotbugs uses static analysis to look for bugs in the code. Spotbugs uses static analysis to look for bugs in the code.
You can run spotbugs using: You can run spotbugs using:

View File

@ -47,7 +47,9 @@ plugins {
// Updating the shadow plugin version to 8.1.1 causes issue with signing and publishing the shadowed // Updating the shadow plugin version to 8.1.1 causes issue with signing and publishing the shadowed
// artifacts - see https://github.com/johnrengelman/shadow/issues/901 // artifacts - see https://github.com/johnrengelman/shadow/issues/901
id 'com.github.johnrengelman.shadow' version '8.1.0' apply false id 'com.github.johnrengelman.shadow' version '8.1.0' apply false
id 'com.diffplug.spotless' version '6.14.0' apply false // 6.14.1 and newer require Java 11 at compile time, so we can't upgrade until AK 4.0 // the minimum required JRE of 6.14.0+ is 11
// refer:https://github.com/diffplug/spotless/tree/main/plugin-gradle#requirements
id 'com.diffplug.spotless' version "6.13.0" apply false
} }
ext { ext {
@ -198,6 +200,9 @@ def determineCommitId() {
} }
} }
def spotlessApplyModules = ['']
apply from: file('wrapper.gradle') apply from: file('wrapper.gradle')
if (repo != null) { if (repo != null) {
@ -793,6 +798,16 @@ subprojects {
skipProjects = [ ":jmh-benchmarks", ":trogdor" ] skipProjects = [ ":jmh-benchmarks", ":trogdor" ]
skipConfigurations = [ "zinc" ] skipConfigurations = [ "zinc" ]
} }
if (project.name in spotlessApplyModules) {
apply plugin: 'com.diffplug.spotless'
spotless {
java {
importOrder('kafka', 'org.apache.kafka', 'com', 'net', 'org', 'java', 'javax', '', '\\#')
removeUnusedImports()
}
}
}
} }
gradle.taskGraph.whenReady { taskGraph -> gradle.taskGraph.whenReady { taskGraph ->

View File

@ -82,6 +82,8 @@
<property name="file" value="${config_loc}/${importControlFile}"/> <property name="file" value="${config_loc}/${importControlFile}"/>
</module> </module>
<!-- don't define any import order here! Import order check/format is addressed by spotless.-->
<!-- whitespace --> <!-- whitespace -->
<module name="GenericWhitespace"/> <module name="GenericWhitespace"/>
<module name="NoWhitespaceBefore"/> <module name="NoWhitespaceBefore"/>