-
Notifications
You must be signed in to change notification settings - Fork 14k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
KAFKA-10787: Add import ordering checkstyle rule and configure an automatic formatter #16097
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gongxuanzhang could you please include the auto-formater in this PR? That can save our life :)
.gitignore
Outdated
@@ -13,6 +13,7 @@ project/boot/ | |||
project/plugins/project/ | |||
patch-process/* | |||
.idea | |||
!/.idea/codeStyles |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the whole idea
folder is not in version control, so do we need this line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the whole
idea
folder 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gongxuanzhang nice patch. BTW, could you add docs to README
for spotlessApply?
build.gradle
Outdated
@@ -1007,7 +1026,7 @@ project(':core') { | |||
testImplementation libs.junitJupiter | |||
testImplementation libs.slf4jlog4j | |||
testImplementation libs.caffeine | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please revert this change
checkstyle/suppressions.xml
Outdated
@@ -361,4 +361,7 @@ | |||
<suppress checks="(ClassDataAbstractionCoupling|ClassFanOutComplexity)" | |||
files="(ReplicaFetcherThreadBenchmark).java"/> | |||
|
|||
<!--import order --> | |||
<suppress checks="ImportOrder" files="^*$"/> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why we need this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
build.gradle
Outdated
@@ -787,6 +800,12 @@ subprojects { | |||
skipProjects = [ ":jmh-benchmarks", ":trogdor" ] | |||
skipConfigurations = [ "zinc" ] | |||
} | |||
|
|||
afterEvaluate { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I change the PR,please review it @chia7712
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gongxuanzhang thanks for updated PR.
README.md
Outdated
@@ -232,6 +232,12 @@ You can run checkstyle using: | |||
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. | |||
|
|||
#### 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
spotlessApplyModules
is a temporary limit, and we don't need to expose it.
gradle.properties
Outdated
@@ -28,6 +28,8 @@ scalaVersion=2.13.14 | |||
# Adding swaggerVersion in gradle.properties to have a single version in place for swagger | |||
# 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
spotlessVersion
is used by only one-line, hence we can inline it.
checkstyle/suppressions.xml
Outdated
@@ -361,4 +361,7 @@ | |||
<suppress checks="(ClassDataAbstractionCoupling|ClassFanOutComplexity)" | |||
files="(ReplicaFetcherThreadBenchmark).java"/> | |||
|
|||
<!--import order --> | |||
<suppress checks="ImportOrder" files="^*$"/> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gongxuanzhang thanks for updated PR
build.gradle
Outdated
@@ -47,7 +47,8 @@ plugins { | |||
// 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 | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about: the minimum required JRE of 6.14.0+ is 11
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I refer to spotless github: https://github.com/diffplug/spotless/tree/main/plugin-gradle#requirements
checkstyle/checkstyle.xml
Outdated
@@ -82,6 +82,10 @@ | |||
<property name="file" value="${config_loc}/${importControlFile}"/> | |||
</module> | |||
|
|||
<!-- import order rule not config in the checkstyle.xml. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about:
DON'T define any import order here! Import order check/format is addressed by spotless.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I update that @chia7712
README.md
Outdated
|
||
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. | ||
|
||
#### 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The import order is a part of static check. please call
spotlessApply to optimize the imports of Java codes before filing pull request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
…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
gitignore
file