Apache Solr open-source search software
LUCENE-9564: add spotless and gjf (automatic code formatter)
Dawid Weissc94b035df86f
diff --git a/build.gradle b/build.gradleindex 59cddf5aec7..20e232d165b 100644--- a/build.gradle+++ b/build.gradle@@ -26,6 +26,7 @@ plugins {id "de.undercouch.download" version "4.0.2" apply falseid "net.ltgt.errorprone" version "1.2.1" apply falseid "com.palantir.docker" version "0.25.0" apply false+ id 'com.diffplug.spotless' version "5.8.2" apply false}apply from: file('gradle/defaults.gradle')@@ -145,6 +146,7 @@ apply from: file('gradle/validation/ecj-lint.gradle')apply from: file('gradle/validation/gradlew-scripts-tweaked.gradle')apply from: file('gradle/validation/validate-log-calls.gradle')apply from: file('gradle/validation/check-broken-links.gradle')+apply from: file('gradle/validation/spotless.gradle')// Source or data regeneration tasksapply from: file('gradle/generation/jflex.gradle')diff --git a/gradle/help.gradle b/gradle/help.gradleindex 4c1bf7e9c2f..161f0721cc4 100644--- a/gradle/help.gradle+++ b/gradle/help.gradle@@ -22,6 +22,7 @@ configure(rootProject) {["Workflow", "help/workflow.txt", "Typical workflow commands."],["Ant", "help/ant.txt", "Ant-gradle migration help."],["Tests", "help/tests.txt", "Tests, filtering, beasting, etc."],+ ["Formatting", "help/formatting.txt", "Code formatting conventions."],["Jvms", "help/jvms.txt", "Using alternative or EA JVM toolchains."],["Deps", "help/dependencies.txt", "Declaring, inspecting and excluding dependencies."],["ForbiddenApis", "help/forbiddenApis.txt", "How to add/apply rules for forbidden APIs."],diff --git a/gradle/validation/spotless.gradle b/gradle/validation/spotless.gradlenew file mode 100644index 00000000000..87fcdb6bdeb--- /dev/null+++ b/gradle/validation/spotless.gradle@@ -0,0 +1,61 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++/*+ * LUCENE-9564: This adds automatic (and enforced) code formatting.+ */++def resources = scriptResources(buildscript)++allprojects { prj ->+ plugins.withType(JavaPlugin) {+ prj.apply plugin: 'com.diffplug.spotless'++ spotless {+ java {+ licenseHeaderFile file("${resources}/asl-header.txt"), '^(\\s*package)'+ lineEndings 'UNIX'+ endWithNewline()+ googleJavaFormat('1.9')++ switch (project.path) {+ // Disable for everything else for now.+ default:+ target 'non-existing/**'+ break+ }+ }+ }+ }+}++// Add an alias to 'spotlessApply' simply called 'tidy' and add+// spotlessCheck to check.+allprojects { prj ->+ task tidy() {+ description "Applies formatters and cleanups to sources."+ group "verification"+ }++ tasks.matching { task -> task.name == "spotlessApply" }.configureEach { v ->+ tidy.dependsOn v+ }++ tasks.matching { task -> task.name == "spotlessCheck" }.configureEach { v ->+ check.dependsOn v+ }+}diff --git a/gradle/validation/spotless/asl-header.txt b/gradle/validation/spotless/asl-header.txtnew file mode 100644index 00000000000..2944f981947--- /dev/null+++ b/gradle/validation/spotless/asl-header.txt@@ -0,0 +1,16 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */diff --git a/help/formatting.txt b/help/formatting.txtnew file mode 100644index 00000000000..55490ac75ce--- /dev/null+++ b/help/formatting.txt@@ -0,0 +1,20 @@+Code formatting+===============++Starting with (LUCENE-9564) Java code is enforced to comply with+google-java-format conventions. In theory you shouldn't worry about+what the convention actually looks like - write the code in any way+you like and then run:++./gradlew tidy++prior to running your regular precommit checks. This will reformat+your code so that it complies with the convention and passes gradle+'check' task.++IMPORTANT: There is *no* way to mark sections of the code as excluded+from formatting. This is by design and cannot be altered. In vast+majority of cases the formatter will do a great job of cleaning up the+code. Occasionally you may want to rewrite the code (introduce a local+variable orreshape code paths) so that it's easier to read after+automatic formatting.