diff --git a/CHANGES.md b/CHANGES.md
index 20814e87f1..b170b7086d 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -10,6 +10,7 @@ This document is intended for Spotless developers.
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
## [Unreleased]
+* Support for`clang-format` on maven-plugin ([#2406](https://github.com/diffplug/spotless/pull/2406))
## [3.0.2] - 2025-01-14
### Fixed
diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md
index 1d63ed362e..aacb203c53 100644
--- a/plugin-maven/CHANGES.md
+++ b/plugin-maven/CHANGES.md
@@ -3,6 +3,7 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
## [Unreleased]
+* Support for `clang-format` ([#2406](https://github.com/diffplug/spotless/pull/2406))
## [2.44.2] - 2025-01-14
* Eclipse-based tasks can now handle parallel configuration ([#2389](https://github.com/diffplug/spotless/issues/2389))
diff --git a/plugin-maven/README.md b/plugin-maven/README.md
index 4c81cc289e..459dd9f1d7 100644
--- a/plugin-maven/README.md
+++ b/plugin-maven/README.md
@@ -43,7 +43,7 @@ user@machine repo % mvn spotless:check
- [Groovy](#groovy) ([eclipse groovy](#eclipse-groovy))
- [Kotlin](#kotlin) ([ktfmt](#ktfmt), [ktlint](#ktlint), [diktat](#diktat), [prettier](#prettier))
- [Scala](#scala) ([scalafmt](#scalafmt))
- - [C/C++](#cc) ([eclipse cdt](#eclipse-cdt))
+ - [C/C++](#cc) ([eclipse cdt](#eclipse-cdt), [clang-format](#clang-format))
- [Python](#python) ([black](#black))
- [Antlr4](#antlr4) ([antlr4formatter](#antlr4formatter))
- [Sql](#sql) ([dbeaver](#dbeaver))
@@ -56,7 +56,7 @@ user@machine repo % mvn spotless:check
- [Gherkin](#gherkin)
- [Go](#go)
- [RDF](#RDF)
- - [Protobuf](#protobuf) ([buf](#buf))
+ - [Protobuf](#protobuf) ([buf](#buf), [clang-format](#clang))
- Multiple languages
- [Prettier](#prettier) ([plugins](#prettier-plugins), [npm detection](#npm-detection), [`.npmrc` detection](#npmrc-detection), [caching `npm install` results](#caching-results-of-npm-install))
- [eclipse web tools platform](#eclipse-web-tools-platform)
@@ -558,6 +558,18 @@ Additionally, `editorConfigOverride` options will override what's supplied in `.
```
+### clang-format
+
+[homepage](https://clang.llvm.org/docs/ClangFormat.html). [changelog](https://releases.llvm.org/download.html). `clang-format` is a formatter for c, c++, c#, objective-c, protobuf, javascript, and java. You can use clang-format in any language-specific format, but usually you will be creating a generic format.
+
+```xml
+
+ 14.0.0-1ubuntu1.1
+ /path/to/buf
+
+
+```
+
## Python
[code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/python/Python.java). [available steps](https://github.com/diffplug/spotless/tree/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/python/Black.java).
@@ -1218,17 +1230,17 @@ RDF parsing is done via [Apache Jena](https://jena.apache.org/) in the version t
[code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/protobuf/Protobuf.java). [available steps](https://github.com/diffplug/spotless/tree/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/protobuf).
```xml
-
- proto/*.proto
-
+
+ proto/*.proto
+
-
- target/**/
-
+
+ target/**/
+
-
+
```
diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/cpp/Clang.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/cpp/Clang.java
new file mode 100644
index 0000000000..f4db16049a
--- /dev/null
+++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/cpp/Clang.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2024-2025 DiffPlug
+ *
+ * Licensed 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.
+ */
+package com.diffplug.spotless.maven.cpp;
+
+import org.apache.maven.plugins.annotations.Parameter;
+
+import com.diffplug.spotless.FormatterStep;
+import com.diffplug.spotless.cpp.ClangFormatStep;
+import com.diffplug.spotless.maven.FormatterStepConfig;
+import com.diffplug.spotless.maven.FormatterStepFactory;
+
+public class Clang implements FormatterStepFactory {
+ @Parameter
+ private String version;
+
+ @Parameter
+ private String pathToExe;
+
+ @Parameter
+ private String style;
+
+ @Override
+ public FormatterStep newFormatterStep(FormatterStepConfig config) {
+ ClangFormatStep clang = ClangFormatStep.withVersion(version == null ? ClangFormatStep.defaultVersion() : version);
+
+ if (pathToExe != null) {
+ clang = clang.withPathToExe(pathToExe);
+ }
+
+ if (style != null) {
+ clang = clang.withStyle(style);
+ }
+
+ return clang.create();
+ }
+
+}
diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/cpp/Cpp.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/cpp/Cpp.java
index 44940ae4d8..8739a1c717 100644
--- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/cpp/Cpp.java
+++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/cpp/Cpp.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2016-2023 DiffPlug
+ * Copyright 2016-2025 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -40,6 +40,10 @@ public void addEclipseCdt(EclipseCdt eclipse) {
addStepFactory(eclipse);
}
+ public void addClangFormat(Clang clang) {
+ addStepFactory(clang);
+ }
+
@Override
public String licenseHeaderDelimiter() {
return CppDefaults.DELIMITER_EXPR;
diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/protobuf/Protobuf.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/protobuf/Protobuf.java
index 3362ea2ad3..78d60f4bde 100644
--- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/protobuf/Protobuf.java
+++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/protobuf/Protobuf.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2016-2024 DiffPlug
+ * Copyright 2016-2025 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
import com.diffplug.common.collect.ImmutableSet;
import com.diffplug.spotless.maven.FormatterFactory;
+import com.diffplug.spotless.maven.cpp.Clang;
import com.diffplug.spotless.maven.generic.LicenseHeader;
/**
@@ -49,4 +50,8 @@ public String licenseHeaderDelimiter() {
public void addBuf(Buf buf) {
addStepFactory(buf);
}
+
+ public void addClang(Clang clang) {
+ addStepFactory(clang);
+ }
}
diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/cpp/ClangMavenIntegrationTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/cpp/ClangMavenIntegrationTest.java
new file mode 100644
index 0000000000..f068cebc45
--- /dev/null
+++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/cpp/ClangMavenIntegrationTest.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2024-2025 DiffPlug
+ *
+ * Licensed 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.
+ */
+package com.diffplug.spotless.maven.cpp;
+
+import org.junit.jupiter.api.Test;
+
+import com.diffplug.spotless.maven.MavenIntegrationHarness;
+import com.diffplug.spotless.tag.ClangTest;
+
+@ClangTest
+class ClangMavenIntegrationTest extends MavenIntegrationHarness {
+
+ @Test
+ @ClangTest
+ void csharp() throws Exception {
+ writePomWithCppSteps("", "", "src/**/*.cs", "", "",
+ "", "", "14.0.0-1ubuntu1.1", "", "");
+ setFile("src/test.cs").toResource("clang/example.cs");
+ mavenRunner().withArguments("spotless:apply").runNoError();
+ assertFile("src/test.cs").sameAsResource("clang/example.cs.clean");
+ }
+
+ @Test
+ @ClangTest
+ void proto() throws Exception {
+ writePomWithCppSteps("", "", "**/*.proto", "", "",
+ "", "", "14.0.0-1ubuntu1.1", "", "");
+ setFile("buf.proto").toResource("protobuf/buf/buf.proto");
+ mavenRunner().withArguments("spotless:apply").runNoError();
+ assertFile("buf.proto").sameAsResource("protobuf/buf/buf.proto.clean");
+ }
+
+}