diff --git a/DEVELOPER_GUIDE.md b/DEVELOPER_GUIDE.md
index 1c5c95c4c..56a30d420 100644
--- a/DEVELOPER_GUIDE.md
+++ b/DEVELOPER_GUIDE.md
@@ -74,6 +74,33 @@ You can import the OpenSearch project into IntelliJ IDEA as follows.
2. In the subsequent dialog navigate to the root `build.gradle` file
3. In the subsequent dialog select **Open as Project**
+## Java Language Formatting Guidelines
+
+Taken from [OpenSearch's guidelines](https://github.com/opensearch-project/OpenSearch/blob/main/DEVELOPER_GUIDE.md):
+
+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:
+
+ ./gradlew spotlessJavaCheck
+
+The code can be formatted with:
+
+ ./gradlew spotlessApply
+
+Please follow these formatting guidelines:
+
+* Java indent is 4 spaces
+* Line width is 140 characters
+* 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.
+* Wildcard imports (`import foo.bar.baz.*`) are forbidden and will cause the build to fail.
+* 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.
+* Note that JavaDoc and block comments i.e. `/* ... */` are not formatted, but line comments i.e `// ...` are.
+* 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.
+
+In order to gradually introduce the spotless formatting, we use the
+[ratchetFrom](https://github.com/diffplug/spotless/tree/main/plugin-gradle#ratchet) spotless functionality. This makes
+it so only files that are changed compared to the origin branch are inspected. Because of this, ensure that your
+origin branch is up to date with the plugins upstream when testing locally.
+
## Build
OpenSearch k-NN uses a [Gradle](https://docs.gradle.org/6.6.1/userguide/userguide.html) wrapper for its build.
diff --git a/build.gradle b/build.gradle
index e9c24b9a2..b3c2ff183 100644
--- a/build.gradle
+++ b/build.gradle
@@ -37,7 +37,7 @@ plugins {
id 'nebula.ospackage' version "8.3.0" apply false
id 'idea'
id 'jacoco'
- id 'checkstyle'
+ id "com.diffplug.spotless" version "6.3.0" apply false
}
repositories {
@@ -47,18 +47,10 @@ repositories {
maven { url "https://plugins.gradle.org/m2/" }
}
+apply from: 'gradle/formatting.gradle'
apply plugin: 'opensearch.opensearchplugin'
apply plugin: 'opensearch.rest-test'
-
-checkstyle {
- toolVersion = '9.3'
- configFile file("checkstyle/checkstyle.xml")
-}
-
-checkstyleMain.ignoreFailures = true
-checkstyleTest.ignoreFailures = true
-
def usingRemoteCluster = System.properties.containsKey('tests.rest.cluster') || System.properties.containsKey('tests.cluster')
def usingMultiNode = project.properties.containsKey('numNodes')
// Only apply jacoco test coverage if we are running a local single node cluster
@@ -94,6 +86,7 @@ jacoco {
toolVersion = "0.8.5"
}
+check.dependsOn spotlessCheck
check.dependsOn jacocoTestReport
opensearchplugin {
@@ -127,11 +120,6 @@ dependencies {
compile group: 'com.google.guava', name: 'guava', version:'30.0-jre'
compile group: 'commons-lang', name: 'commons-lang', version: '2.6'
testImplementation "org.opensearch.test:framework:${opensearch_version}"
- configurations.all {
- resolutionStrategy {
- force "com.puppycrawl.tools:checkstyle:${project.checkstyle.toolVersion}"
- }
- }
}
licenseHeaders.enabled = true
diff --git a/buildSrc/formatterConfig.xml b/buildSrc/formatterConfig.xml
new file mode 100644
index 000000000..713e55274
--- /dev/null
+++ b/buildSrc/formatterConfig.xml
@@ -0,0 +1,362 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
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
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/gradle/formatting.gradle b/gradle/formatting.gradle
new file mode 100644
index 000000000..ec33249c3
--- /dev/null
+++ b/gradle/formatting.gradle
@@ -0,0 +1,17 @@
+/*
+ * Copyright OpenSearch Contributors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+allprojects {
+ project.apply plugin: "com.diffplug.spotless"
+ spotless {
+ java {
+ ratchetFrom 'origin/1.x'
+ target '**/*.java'
+ removeUnusedImports()
+ eclipse().configFile rootProject.file('buildSrc/formatterConfig.xml')
+ trimTrailingWhitespace()
+ }
+ }
+}