KAFKA-10787: Add import ordering checkstyle rule and configure an automatic formatter#16097
KAFKA-10787: Add import ordering checkstyle rule and configure an automatic formatter#16097chia7712 merged 29 commits intoapache:trunkfrom
Conversation
chia7712
left a comment
There was a problem hiding this comment.
@gongxuanzhang could you please include the auto-formater in this PR? That can save our life :)
| project/plugins/project/ | ||
| patch-process/* | ||
| .idea | ||
| !/.idea/codeStyles |
There was a problem hiding this comment.
the whole idea folder is not in version control, so do we need this line?
There was a problem hiding this comment.
the whole
ideafolder is not in version control, so do we need this line?
It has to be, and it's very important.
This file will allow you to import your idea automatically into the project's code format
There was a problem hiding this comment.
It seems to me using build tool to address that is more acceptable. We all use gradle to build kafka but there are many IDE.
We can add IDE supports in the future but could you please keep this PR simple for now?
|
I revised this PR.It add spotless gradle plugin and mini configuration.
|
chia7712
left a comment
There was a problem hiding this comment.
@gongxuanzhang nice patch. BTW, could you add docs to README for spotlessApply?
| testImplementation libs.junitJupiter | ||
| testImplementation libs.slf4jlog4j | ||
| testImplementation libs.caffeine | ||
| files="(ReplicaFetcherThreadBenchmark).java"/> | ||
|
|
||
| <!--import order --> | ||
| <suppress checks="ImportOrder" files="^*$"/> |
There was a problem hiding this comment.
I think checkstyle should consistent with auto format . If you open the A module auto format, we should open the module check rule.
ImportOrder rule can't custom in each module(It's going to take a lot of changes,maybe should add build.gradle every module).
So i add this line in order that open rule some module in future,This is what I think is a more convenient way to modify by module
There was a problem hiding this comment.
Making the consistency is extra effort. Also, those changes will complicate checkstyle files. Hence, I prefer to remove them if we do NOT use it actually.
| skipConfigurations = [ "zinc" ] | ||
| } | ||
|
|
||
| afterEvaluate { |
There was a problem hiding this comment.
Maybe we can set spotless directly. For example:
if (project.name in spotlessApplyModules) {
apply plugin: 'com.diffplug.spotless'
spotless {
java {
importOrder('kafka', 'org.apache.kafka', 'com', 'net', 'org', 'java', 'javax', '', '\\#')
removeUnusedImports()
}
}
}
There was a problem hiding this comment.
I change the PR,please review it @chia7712
chia7712
left a comment
There was a problem hiding this comment.
@gongxuanzhang thanks for updated PR.
|
|
||
| #### Spotless #### | ||
| This plugin can review code by rules,it is disabled by default. You can enable it by setting `build.gradle` add a module name into the `spotlessApplyModules` variable like | ||
| `def spotlessApplyModules = ['core']`,then you can run spotless using: |
There was a problem hiding this comment.
spotlessApplyModules is a temporary limit, and we don't need to expose it.
| # New version of Swagger 2.2.14 requires minimum JDK 11. | ||
| swaggerVersion=2.2.8 | ||
| # 6.14.0 and newer are need for java 11 | ||
| spotlessVersion=6.13.0 |
There was a problem hiding this comment.
spotlessVersion is used by only one-line, hence we can inline it.
| files="(ReplicaFetcherThreadBenchmark).java"/> | ||
|
|
||
| <!--import order --> | ||
| <suppress checks="ImportOrder" files="^*$"/> |
There was a problem hiding this comment.
Making the consistency is extra effort. Also, those changes will complicate checkstyle files. Hence, I prefer to remove them if we do NOT use it actually.
|
I update this PR,plz review it |
chia7712
left a comment
There was a problem hiding this comment.
@gongxuanzhang thanks for updated PR
| // artifacts - see https://github.com/johnrengelman/shadow/issues/901 | ||
| 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 | ||
| // 6.14.0 and newer are need for java 11 |
There was a problem hiding this comment.
how about: the minimum required JRE of 6.14.0+ is 11
There was a problem hiding this comment.
I refer to spotless github: https://github.com/diffplug/spotless/tree/main/plugin-gradle#requirements
| <property name="file" value="${config_loc}/${importControlFile}"/> | ||
| </module> | ||
|
|
||
| <!-- import order rule not config in the checkstyle.xml. |
There was a problem hiding this comment.
how about:
DON'T define any import order here! Import order check/format is addressed by spotless.
| subproject build directories. They are also printed to the console. The build will fail if Checkstyle fails. | ||
|
|
||
| #### Spotless #### | ||
| This plugin can review code by rules and can also help you check the code , it is disabled by default. Some of our code reviews use `spotless` instead of `checkstyle`: |
There was a problem hiding this comment.
The import order is a part of static check. please call spotlessApply to optimize the imports of Java codes before filing pull request.
…matic formatter (apache#16097) Reviewers: Chia-Ping Tsai <chia7712@gmail.com>
…matic formatter (apache#16097) Reviewers: Chia-Ping Tsai <chia7712@gmail.com>
…matic formatter (apache#16097) Reviewers: Chia-Ping Tsai <chia7712@gmail.com>

add a import-order rule. It includes the following parts
checkstyle import-order
In order to uniform coding specifications in Kafka. This is the first step.
Referred to #10428 import order .
We specify the import order as "kafka, org.apache.kafka, com, net, org, java, javax and static imports."
suppress all module
If the detection function is enabled,A lot of code needs to be changed.
In order to pass CI and minimal modification. suppress all module checkstyle import-order.
We can open the module step by step.
in Intellj IDEA auto format coding configuration
If we want to uniform all code. The manual approach is cumbersome and error-prone.
And we need everyone to follow the rules in the future. So we need auto format coding tools.
In this PR I add a IDEA configuration and update
gitignorefile