Skip to content

Commit 0db9b2e

Browse files
authored
Switch checkstyle to spotless (#297)
Switches checkstyle functionality to spotless. Sets ratchetFrom to origin/main. This will mean it will only fail on files recently changed. Pull formatting from OpenSearch. Signed-off-by: John Mazanec <[email protected]>
1 parent e77ef78 commit 0db9b2e

File tree

5 files changed

+409
-274
lines changed

5 files changed

+409
-274
lines changed

DEVELOPER_GUIDE.md

+27
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,33 @@ You can import the OpenSearch project into IntelliJ IDEA as follows.
7474
2. In the subsequent dialog navigate to the root `build.gradle` file
7575
3. In the subsequent dialog select **Open as Project**
7676

77+
## Java Language Formatting Guidelines
78+
79+
Taken from [OpenSearch's guidelines](https://github.com/opensearch-project/OpenSearch/blob/main/DEVELOPER_GUIDE.md):
80+
81+
Java files in the OpenSearch codebase are formatted with the Eclipse JDT formatter, using the [Spotless Gradle](https://github.com/diffplug/spotless/tree/master/plugin-gradle) plugin. The formatting check can be run explicitly with:
82+
83+
./gradlew spotlessJavaCheck
84+
85+
The code can be formatted with:
86+
87+
./gradlew spotlessApply
88+
89+
Please follow these formatting guidelines:
90+
91+
* Java indent is 4 spaces
92+
* Line width is 140 characters
93+
* Lines of code surrounded by `// tag::NAME` and `// end::NAME` comments are included in the documentation and should only be 76 characters wide not counting leading indentation. Such regions of code are not formatted automatically as it is not possible to change the line length rule of the formatter for part of a file. Please format such sections sympathetically with the rest of the code, while keeping lines to maximum length of 76 characters.
94+
* Wildcard imports (`import foo.bar.baz.*`) are forbidden and will cause the build to fail.
95+
* If *absolutely* necessary, you can disable formatting for regions of code with the `// tag::NAME` and `// end::NAME` directives, but note that these are intended for use in documentation, so please make it clear what you have done, and only do this where the benefit clearly outweighs the decrease in consistency.
96+
* Note that JavaDoc and block comments i.e. `/* ... */` are not formatted, but line comments i.e `// ...` are.
97+
* There is an implicit rule that negative boolean expressions should use the form `foo == false` instead of `!foo` for better readability of the code. While this isn't strictly enforced, if might get called out in PR reviews as something to change.
98+
99+
In order to gradually introduce the spotless formatting, we use the
100+
[ratchetFrom](https://github.com/diffplug/spotless/tree/main/plugin-gradle#ratchet) spotless functionality. This makes
101+
it so only files that are changed compared to the origin branch are inspected. Because of this, ensure that your
102+
origin branch is up to date with the plugins upstream when testing locally.
103+
77104
## Build
78105

79106
OpenSearch k-NN uses a [Gradle](https://docs.gradle.org/6.6.1/userguide/userguide.html) wrapper for its build.

build.gradle

+3-15
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ plugins {
3737
id 'nebula.ospackage' version "8.3.0" apply false
3838
id 'idea'
3939
id 'jacoco'
40-
id 'checkstyle'
40+
id "com.diffplug.spotless" version "6.3.0" apply false
4141
}
4242

4343
repositories {
@@ -47,18 +47,10 @@ repositories {
4747
maven { url "https://plugins.gradle.org/m2/" }
4848
}
4949

50+
apply from: 'gradle/formatting.gradle'
5051
apply plugin: 'opensearch.opensearchplugin'
5152
apply plugin: 'opensearch.rest-test'
5253

53-
54-
checkstyle {
55-
toolVersion = '9.3'
56-
configFile file("checkstyle/checkstyle.xml")
57-
}
58-
59-
checkstyleMain.ignoreFailures = true
60-
checkstyleTest.ignoreFailures = true
61-
6254
def usingRemoteCluster = System.properties.containsKey('tests.rest.cluster') || System.properties.containsKey('tests.cluster')
6355
def usingMultiNode = project.properties.containsKey('numNodes')
6456
// Only apply jacoco test coverage if we are running a local single node cluster
@@ -94,6 +86,7 @@ jacoco {
9486
toolVersion = "0.8.5"
9587
}
9688

89+
check.dependsOn spotlessCheck
9790
check.dependsOn jacocoTestReport
9891

9992
opensearchplugin {
@@ -127,11 +120,6 @@ dependencies {
127120
compile group: 'com.google.guava', name: 'guava', version:'30.0-jre'
128121
compile group: 'commons-lang', name: 'commons-lang', version: '2.6'
129122
testImplementation "org.opensearch.test:framework:${opensearch_version}"
130-
configurations.all {
131-
resolutionStrategy {
132-
force "com.puppycrawl.tools:checkstyle:${project.checkstyle.toolVersion}"
133-
}
134-
}
135123
}
136124

137125
licenseHeaders.enabled = true

0 commit comments

Comments
 (0)