### Install Spotless Git Pre-Push Hook
Source: https://github.com/diffplug/spotless/blob/main/plugin-gradle/README.md
Install a Git pre-push hook to automatically run `spotlessCheck` before each push. If formatting issues are found, it will attempt to fix them with `spotlessApply` and abort the push.
```bash
./gradlew spotlessInstallGitPrePushHook
```
--------------------------------
### FormatterStep Implementation Example
Source: https://github.com/diffplug/spotless/blob/main/CONTRIBUTING.md
Illustrates the basic structure of implementing a FormatterStep. Ensure it implements Serializable, equals, and hashCode for build system compatibility.
```java
class FooStep implements FormatterStep {
// ... implementation ...
}
```
--------------------------------
### Centralize Spotless Configuration with Dependencies
Source: https://github.com/diffplug/spotless/blob/main/plugin-gradle/README.md
Centralize Spotless configuration across multiple projects by defining a common configuration as a deployable artifact. This example shows how to include custom import order and formatter configuration files from a dependency.
```kotlin
configurations {
spotlessConfig
}
dependencies {
// the files `java-import-order.txt` and `java-formatter.xml` should be at the root of the deployed `org.mycompany:code-configuration:1.0.0` jar.
spotlessConfig("org.mycompany:code-configuration:1.0.0")
}
spotless {
java {
removeUnusedImports()
importOrder(resources.text.fromArchiveEntry(configurations.spotlessConfig, "java-import-order.txt").asString())
eclipse().configXml(resources.text.fromArchiveEntry(spotlessConfig, "java-formatter.xml").asString())
}
}
```
--------------------------------
### Javascript Configuration Example
Source: https://github.com/diffplug/spotless/blob/main/plugin-maven/README.md
Configure Javascript processing, including file includes and integration with Prettier, ESLint, and Biome. Supports custom license headers with content and delimiter.
```xml
src/**/*.js/* (C)$YEAR */REGEX_TO_DEFINE_TOP_OF_FILE
```
--------------------------------
### Configure npm and Node.js Executables
Source: https://github.com/diffplug/spotless/blob/main/plugin-maven/README.md
If Spotless cannot auto-discover your npm or Node.js installation, you can explicitly configure the paths to the executables. If only one is provided, the other is assumed to be in the same directory.
```xml
/usr/bin/npm/usr/bin/node
```
--------------------------------
### Ruby Asciidoctor Usage Example
Source: https://github.com/diffplug/spotless/blob/main/testlib/src/main/resources/asciidoc/adocfmt/clean_custom.adoc
Demonstrates loading and converting AsciiDoc content using the Asciidoctor Ruby library. Ensure the 'asciidoctor' gem is installed.
```ruby
require 'asciidoctor'
doc = Asciidoctor.load '*This* is http://asciidoc.org[AsciiDoc]!', header_footer: true
puts doc.convert
```
--------------------------------
### Configure Groovy Gradle Formatting with Spotless
Source: https://github.com/diffplug/spotless/blob/main/plugin-gradle/README.md
Configure formatting specifically for `.gradle` files using the `groovyGradle` extension. This example also applies the Groovy Eclipse formatter.
```gradle
spotless {
groovyGradle {
target '*.gradle' // default target of groovyGradle
greclipse()
}
}
```
--------------------------------
### Configure Spotless Plugin in Gradle
Source: https://github.com/diffplug/spotless/blob/main/plugin-gradle/README.md
Add the Spotless dependency and configure its settings in your build.gradle file. This example shows how to define custom formatting rules for miscellaneous files and Java code.
```gradle
spotless {
// optional: limit format enforcement to just the files changed by this feature branch
ratchetFrom 'origin/main'
format 'misc', {
// define the files to apply `misc` to
target '*.gradle', '.gitattributes', '.gitignore'
// define the steps to apply to those files
trimTrailingWhitespace()
leadingSpacesToTabs() // or leadingTabsToSpaces. Takes an integer argument if you don't like 4
endWithNewline()
}
java {
// don't need to set target, it is inferred from java
// apply a specific flavor of google-java-format
googleJavaFormat('1.8').aosp().reflowLongStrings().skipJavadocFormatting()
// fix formatting of type annotations
formatAnnotations()
// make sure every file has the following copyright header.
// optionally, Spotless can set copyright years by digging
// through git history (see "license" section below)
licenseHeader '/* (C)$YEAR */'
}
}
```
--------------------------------
### Enable npm install caching for Prettier
Source: https://github.com/diffplug/spotless/blob/main/plugin-maven/README.md
Configure Spotless to cache the results of `npm install` to speed up subsequent installations. You can use the default cache directory or specify a custom one.
```xml
true/usr/local/shared/.spotless-npm-install-cache
```
--------------------------------
### Biome Custom Configuration for Spotless
Source: https://github.com/diffplug/spotless/blob/main/plugin-gradle/README.md
Example of a custom Biome configuration file (biome-spotless.json) that extends a main configuration and includes all files. This is useful when Spotless formats no files due to biome.json exclusions.
```jsonc
// biome-spotless.json
{
"extends": "./biome.json",
"include": ["**"]
}
```
--------------------------------
### Configure Protobuf Formatting with Buf
Source: https://github.com/diffplug/spotless/blob/main/plugin-gradle/README.md
Set up Protobuf code formatting using the 'buf' formatter. This step must be the first in the chain. License headers can also be configured.
```gradle
spotless {
protobuf {
// by default the target is every '.proto' file in the project
buf()
licenseHeader '/* (C) $YEAR */' // or licenseHeaderFile
}
}
```
--------------------------------
### Cache npm install results for Prettier
Source: https://github.com/diffplug/spotless/blob/main/plugin-gradle/README.md
Instruct Spotless to cache the results of npm install calls to speed up subsequent installations. This can use a default cache directory or a specified path.
```gradle
spotless {
typescript {
prettier().npmInstallCache() // will use the default cache directory (the build-directory of the respective module)
prettier().npmInstallCache("${rootProject.rootDir}/.gradle/spotless-npm-cache") // will use the specified directory (creating it if not existing)
}
}
```
--------------------------------
### Configure JSON Formatting
Source: https://github.com/diffplug/spotless/blob/main/plugin-gradle/README.md
Set up the target files for JSON formatting and choose a specific formatter. This is the entry point for all JSON configurations.
```gradle
spotless {
json {
target 'src/**/*.json' // you have to set the target manually
simple() // has its own section below
prettier().config(['parser': 'json']) // see Prettier section below
eclipseWtp('json') // see Eclipse web tools platform section
gson() // has its own section below
jackson() // has its own section below
biome() // has its own section below
jsonPatch([]) // has its own section below
}
}
```
--------------------------------
### Java Type Annotation Example
Source: https://github.com/diffplug/spotless/blob/main/plugin-maven/README.md
Example of Java code with type annotations formatted correctly and incorrectly.
```java
@Override
@Deprecated
@Nullable @Interned String s;
```
```java
@Override
@Deprecated
@Nullable
@Interned
String s;
```
--------------------------------
### Configure SQL Formatting
Source: https://github.com/diffplug/spotless/blob/main/plugin-maven/README.md
Set up SQL formatting by specifying include paths and choosing a formatter like DBeaver, Prettier, or IDEA.
```xml
src/main/resources/**/*.sql
```
--------------------------------
### List of Python Formatters
Source: https://github.com/diffplug/spotless/blob/main/README.md
Includes the BlackStep for formatting Python code according to the Black style guide.
```java
lib('python.BlackStep') +'{{yes}} | {{no}} | {{no}} | {{no}} |',
```
--------------------------------
### HTML Output of Asciidoctor Conversion
Source: https://github.com/diffplug/spotless/blob/main/testlib/src/main/resources/asciidoc/adocfmt/clean_custom.adoc
The resulting HTML generated from the Asciidoctor Ruby usage example. This shows the structure and content after conversion.
```html
Untitled
```
--------------------------------
### Configure YAML Formatting with Jackson
Source: https://github.com/diffplug/spotless/blob/main/plugin-gradle/CHANGES.md
Demonstrates how to configure YAML formatting using the Jackson library within Spotless, enabling specific YAML features like quote minimization.
```gradle
spotless {
yaml {
jackson().yamlFeature("MINIMIZE_QUOTES", true)
}
}
```
--------------------------------
### Configure C/C++ Formatting
Source: https://github.com/diffplug/spotless/blob/main/plugin-gradle/README.md
Set up C/C++ formatting, defining the target files and specifying formatters like clangFormat or eclipseCdt. A license header can also be applied.
```gradle
spotless {
cpp {
target 'src/native/**' // you have to set the target manually
clangFormat() // has its own section below
eclipseCdt() // has its own section below
licenseHeader '/* (C) $YEAR */' // or licenseHeaderFile
}
}
```
--------------------------------
### Configure npm and Node Executables for Prettier
Source: https://github.com/diffplug/spotless/blob/main/plugin-gradle/README.md
Manually specify the paths to npm and Node.js executables if Spotless cannot auto-discover them. This ensures Prettier can run correctly.
```gradle
spotless {
format 'javascript', {
prettier().npmExecutable('/usr/bin/npm').nodeExecutable('/usr/bin/node').config(...)
```
--------------------------------
### Configure Go Formatter (gofmt)
Source: https://github.com/diffplug/spotless/blob/main/plugin-maven/README.md
Configure the gofmt version and the path to the Go executable. Use '*' to allow any found version.
```xml
go1.25.1/opt/sdks/go1.25.1/bin/go
```
--------------------------------
### Configure ESLint with Specific NPM Packages
Source: https://github.com/diffplug/spotless/blob/main/plugin-gradle/README.md
Configure ESLint for TypeScript by specifying exactly which npm packages to use, including versions. This allows for precise control over the ESLint setup.
```gradle
spotless {
typescript {
eslint('8.30.0') // version is optional
eslint(['my-eslint-fork': '1.2.3', 'my-eslint-plugin': '1.2.1']) // can specify exactly which npm packages to use
eslint()
// configuration is mandatory. Provide inline config or a config file.
// a) inline-configuration
.configJs('''
{
env: {
browser: true,
es2021: true
},
extends: 'standard-with-typescript',
overrides: [
],
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
project: './tsconfig.json',
},
rules: {
}
}
''')
// b) config file
.configFile('.eslintrc.js')
// recommended: provide a tsconfig.json - especially when using the styleguides
.tsconfigFile('tsconfig.json')
}
}
```
--------------------------------
### Running Spotless Checks and Applying Fixes in Gradle
Source: https://github.com/diffplug/spotless/blob/main/plugin-gradle/README.md
Demonstrates the command-line workflow for checking code formatting violations and applying automatic fixes using the Spotless Gradle plugin. Shows the output of a failed build due to formatting issues and the successful build after applying fixes.
```console
user@machine repo % ./gradlew build
:spotlessJavaCheck FAILED
The following files had format violations:
src\main\java\com\diffplug\gradle\spotless\FormatExtension.java
-\t ····if·(targets.length·==·0)·{
+ if·(targets.length·==·0)·{
Run './gradlew spotlessApply' to fix these violations.
user@machine repo % ./gradlew spotlessApply
:spotlessApply
BUILD SUCCESSFUL
user@machine repo % ./gradlew build
BUILD SUCCESSFUL
```
--------------------------------
### Configure Scala Formatting
Source: https://github.com/diffplug/spotless/blob/main/plugin-gradle/README.md
Set up Scala formatting using the default settings or specify a license header. The license header can be defined with a regex to identify the top of the file.
```gradle
spotless {
scala {
// by default, all `.scala` and `.sc` files in the java sourcesets will be formatted
scalafmt() // has its own section below
licenseHeader '/* (C) $YEAR */', 'package ' // or licenseHeaderFile
// note the 'package ' argument - this is a regex which identifies the top
// of the file, be careful that all of your sources have a package declaration,
// or pick a regex which works better for your code
}
}
```
--------------------------------
### Declare Specific Formatters for Predeclaration (Gradle)
Source: https://github.com/diffplug/spotless/blob/main/plugin-gradle/CHANGES.md
After enabling dependency predeclaration, declare each formatter and its configuration within the `spotlessPredeclare` block. This example shows how to configure Eclipse for Java and ktfmt for Kotlin.
```gradle
spotlessPredeclare {
java { eclipse() }
kotlin { ktfmt('0.28') }
}
```
--------------------------------
### Configure Asciidoc Includes
Source: https://github.com/diffplug/spotless/blob/main/plugin-maven/README.md
Specify which Asciidoc files should be processed by Spotless. The target path must be set manually.
```xml
src/docs/**/*.adoc
```
--------------------------------
### Configure Javascript Formatting with Spotless
Source: https://github.com/diffplug/spotless/blob/main/plugin-gradle/README.md
Set the target files for Javascript formatting and enable formatters like Prettier, ESLint, and Biome. You can also configure license headers.
```gradle
spotless {
javascript {
target 'src/**/*.js' // you have to set the target manually
prettier() // has its own section below
eslint() // has its own section below
biome() // has its own section below
licenseHeader '/* (C) $YEAR */', 'REGEX_TO_DEFINE_TOP_OF_FILE' // or licenseHeaderFile
}
}
```
--------------------------------
### Custom Gradle Formatter Rule
Source: https://github.com/diffplug/spotless/blob/main/plugin-gradle/CHANGES.md
Define a custom formatter rule in Gradle to enforce specific coding standards, such as preventing certain words from appearing in code. This example throws an AssertionError if 'fubar' is detected.
```gradle
custom 'no swearing', {
if (it.toLowerCase().contains('fubar')) {
throw new AssertionError('No swearing!');
}
}
```
--------------------------------
### Configure C++ Formatting
Source: https://github.com/diffplug/spotless/blob/main/plugin-maven/README.md
Set up general C++ formatting rules, including file inclusions and license headers. Use `` for target files and `` or `` for license headers.
```xml
src/native/**/* (C)$YEAR */
```
--------------------------------
### Check and Apply Formatting with Gradle
Source: https://github.com/diffplug/spotless/blob/main/README.md
Use './gradlew build' to check for formatting violations. If violations are found, run './gradlew spotlessApply' to automatically fix them. Then, run './gradlew build' again to confirm.
```console
user@machine repo % ./gradlew build
:spotlessJavaCheck FAILED
The following files had format violations:
src\main\java\com\diffplug\gradle\spotless\FormatExtension.java
-\t\t····if·(targets.length·==·0)·{
+\t\tif·(targets.length·==·0)·{
Run './gradlew spotlessApply' to fix these violations.
user@machine repo % ./gradlew spotlessApply
:spotlessApply
BUILD SUCCESSFUL
user@machine repo % ./gradlew build
BUILD SUCCESSFUL
```
--------------------------------
### Configure Python Formatting with Black
Source: https://github.com/diffplug/spotless/blob/main/plugin-gradle/README.md
Set up Python code formatting using the 'black' formatter. Specify the target files and optionally configure license headers.
```gradle
spotless {
python {
target 'src/main/**/*.py' // have to set manually
black() // has its own section below
licenseHeader '/* (C) $YEAR */', 'REGEX_TO_DEFINE_TOP_OF_FILE' // or licenseHeaderFile
}
}
```
--------------------------------
### Basic Kotlin Configuration
Source: https://github.com/diffplug/spotless/blob/main/plugin-gradle/README.md
Configure Kotlin formatting for standard Kotlin files. Includes options for ktfmt, ktlint, diktat, prettier, tableTestFormatter, and license headers.
```gradle
spotless {
// if you are using build.gradle.kts, instead of 'spotless {' use:
// configure {
kotlin {
// by default the target is every '.kt' and '.kts` file in the java sourcesets
ktfmt() // has its own section below
ktlint() // has its own section below
diktat() // has its own section below
prettier() // has its own section below
tableTestFormatter() // has its own section below
licenseHeader '/* (C)$YEAR */' // or licenseHeaderFile
}
kotlinGradle {
target('*.gradle.kts') // default target for kotlinGradle
ktlint() // or ktfmt() or prettier()
}
}
```
--------------------------------
### Execute Native Command for Formatting
Source: https://github.com/diffplug/spotless/blob/main/plugin-maven/README.md
Run an external native binary to format code. The unformatted code is sent via stdin, and the formatted code is expected on stdout. Arguments for the command can be specified.
```xml
Greetings to Mars from sed/usr/bin/seds/World/Mars/g
```
--------------------------------
### Protobuf Configuration
Source: https://github.com/diffplug/spotless/blob/main/plugin-maven/README.md
Configure Protobuf formatting including include and exclude patterns.
```xml
proto/*.prototarget/**/
```
--------------------------------
### Configure Go Formatting
Source: https://github.com/diffplug/spotless/blob/main/plugin-maven/README.md
Configure includes for Go files. The gofmt section is configured separately.
```xml
src/**/*.go
```
--------------------------------
### Configure Java Formatting in Gradle
Source: https://github.com/diffplug/spotless/blob/main/plugin-gradle/README.md
Set up Java code formatting using the Spotless Gradle plugin. Includes options for import order, removing unused imports, and forbidding wildcard imports.
```gradle
spotless {
java {
// Use the default importOrder configuration
importOrder()
// optional: you can specify import groups directly
// note: you can use an empty string for all the imports you didn't specify explicitly, '|' to join group without blank line, and '\#` prefix for static imports
importOrder('java|javax', 'com.acme', '', '\#com.acme', '\#')
// optional: instead of specifying import groups directly you can specify a config file
// export config file: https://github.com/diffplug/spotless/blob/main/ECLIPSE_SCREENSHOTS.md#creating-spotlessimportorder
importOrderFile('eclipse-import-order.txt') // import order file as exported from eclipse
removeUnusedImports()
forbidWildcardImports() // or expandWildcardImports, see below
forbidModuleImports()
// Cleanthat will refactor your code, but it may break your style: apply it before your formatter
cleanthat() // has its own section below
// Choose one of these formatters.
googleJavaFormat() // has its own section below
eclipse() // has its own section below
prettier() // has its own section below
clangFormat() // has its own section below
idea() // has its own section below
tableTestFormatter() // has its own section below
formatAnnotations() // fixes formatting of type annotations, see below
licenseHeader '/* (C) $YEAR */' // or licenseHeaderFile
}
}
```
--------------------------------
### Configure Generic Native Command
Source: https://github.com/diffplug/spotless/blob/main/plugin-gradle/README.md
This snippet shows how to configure Spotless to run a generic native command-line tool, such as terraform. You need to specify the target files, the command name, the path to the binary, and any additional arguments.
```gradle
spotless {
// run a native binary
format 'terraform', {
target 'src/**/*.tf', 'src/**/*.tfvars' // you have to set the target manually
nativeCmd('terraform', '/opt/homebrew/bin/terraform', ['fmt', '-']) // name, path to binary, additional arguments
}
}
```
--------------------------------
### Configure YAML Formatting
Source: https://github.com/diffplug/spotless/blob/main/plugin-gradle/README.md
Basic configuration for YAML formatting using the Spotless Gradle plugin. Specify the target files and choose a formatter.
```gradle
spotless {
yaml {
target 'src/**/*.yaml' // you have to set the target manually
jackson() // has its own section below
prettier() // has its own section below
}
}
```
--------------------------------
### Configure Shell Formatting
Source: https://github.com/diffplug/spotless/blob/main/plugin-gradle/README.md
Basic configuration for Shell formatting using the Spotless Gradle plugin. Specify the target files and enable the shfmt formatter.
```gradle
spotless {
shell {
target 'scripts/**/*.sh' // default: '**/*.sh'
shfmt() // has its own section below
}
}
```
--------------------------------
### Spotless Build Tool Integration Table
Source: https://github.com/diffplug/spotless/blob/main/README.md
A comparative table showing Spotless feature support across different build tools: Gradle, Maven, and sbt. Indicates which features are available for each.
```markdown
| Feature / FormatterStep | [gradle](plugin-gradle/README.md) | [maven](plugin-maven/README.md) | [sbt](https://github.com/moznion/sbt-spotless) | [(Your build tool here)](CONTRIBUTING.md#how-to-add-a-new-plugin-for-a-build-system) |
| --------------------------------------------- | ------------- | ------------ | ------------ | --------|
| Automatic [idempotency safeguard](PADDEDCELL.md) | :+1: | :+1: | :+1: | :white_large_square: |
| Misconfigured [encoding safeguard](https://github.com/diffplug/spotless/blob/08340a11566cdf56ecf50dbd4d557ed84a70a502/testlib/src/test/java/com/diffplug/spotless/EncodingErrorMsgTest.java#L34-L38) | :+1: | :+1: | :+1: | :white_large_square: |
| Toggle with [`spotless:off` and `spotless:on`](plugin-gradle/#spotlessoff-and-spotlesson) | :+1: | :+1: | :white_large_square: | :white_large_square: |
| [Ratchet from](https://github.com/diffplug/spotless/tree/main/plugin-gradle#ratchet) `origin/main` or other git ref | :+1: | :+1: | :white_large_square: | :white_large_square: |
| Define [line endings using git](https://github.com/diffplug/spotless/tree/main/plugin-gradle#line-endings-and-encodings-invisible-stuff) | :+1: | :+1: | :+1: | :white_large_square: |
| Fast incremental format and up-to-date check | :+1: | :+1: | :white_large_square: | :white_large_square: |
| Fast format on fresh checkout using buildcache | :+1: | :white_large_square: | :white_large_square: | :white_large_square: |
| [`generic.EndWithNewlineStep`](lib/src/main/java/com/diffplug/spotless/generic/EndWithNewlineStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
| [`generic.IndentStep`](lib/src/main/java/com/diffplug/spotless/generic/IndentStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
| [`generic.Jsr223Step`](lib/src/main/java/com/diffplug/spotless/generic/Jsr223Step.java) | :white_large_square: | :+1: | :white_large_square: | :white_large_square: |
| [`generic.LicenseHeaderStep`](lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java) | :+1: | :+1: | :+1: | :white_large_square: |
| [`generic.NativeCmdStep`](lib/src/main/java/com/diffplug/spotless/generic/NativeCmdStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
| [`generic.ReplaceRegexStep`](lib/src/main/java/com/diffplug/spotless/generic/ReplaceRegexStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
```
--------------------------------
### Configure Groovy Formatting with Spotless
Source: https://github.com/diffplug/spotless/blob/main/plugin-gradle/README.md
Set up Groovy formatting, including import order, semicolon removal, license headers, and the Groovy Eclipse formatter. Use `excludeJava()` to prevent formatting of Java files within Groovy source sets.
```gradle
apply plugin: 'groovy'
sKaynakça {
groovy {
// Use the default importOrder configuration
importOrder()
// optional: you can specify import groups directly
// note: you can use an empty string for all the imports you didn't specify explicitly, and '\#` prefix for static imports
importOrder('java', 'javax', 'com.acme', '', '\#com.acme', '\#')
// optional: instead of specifying import groups directly you can specify a config file
// export config file: https://github.com/diffplug/spotless/blob/main/ECLIPSE_SCREENSHOTS.md#creating-spotlessimportorder
importOrderFile('eclipse-import-order.txt') // import order file as exported from eclipse
// removes semicolons at the end of lines
removeSemicolons()
// the Groovy Eclipse formatter extends the Java Eclipse formatter,
// so it formats Java files by default (unless `excludeJava` is used).
greclipse() // has its own section below
licenseHeader('/* (C) $YEAR */') // or licenseHeaderFile
//---- Below is for `groovy` only ----
// excludes all Java sources within the Groovy source dirs from formatting
excludeJava()
}
groovyGradle {
target '*.gradle' // default target of groovyGradle
greclipse()
}
}
```
--------------------------------
### Configure Multiple Language-Specific Formats in Gradle
Source: https://github.com/diffplug/spotless/blob/main/plugin-gradle/CHANGES.md
Demonstrates how to configure multiple language-specific formats within the Spotless Gradle plugin. This allows for distinct formatting rules for different languages like Kotlin and Kotlin Script.
```groovy
import com.diffplug.gradle.spotless.KotlinExtension
spotless {
kotlin {
target 'src/**/*.kt'
ktlint()
}
format 'kotlinScript', KotlinExtension, {
target 'src/**/*.kts'
ktfmt()
}
}
```
--------------------------------
### Using Biome from System Path
Source: https://github.com/diffplug/spotless/blob/main/plugin-gradle/README.md
Configures Spotless to use the 'biome' command directly, assuming it is available on the user's system path.
```gradle
spotless {
format 'biome', {
target '**/*.js','**/*.ts','**/*.json'
// Uses the "biome" command, which must be on the user's path. -->
biome().pathToExe('biome')
}
}
```
--------------------------------
### Configure Kotlin Formatting with Spotless Maven
Source: https://github.com/diffplug/spotless/blob/main/plugin-maven/README.md
Use this configuration to set up general Kotlin formatting rules, including include paths and specific formatter configurations. Defaults are provided for includes.
```xml
src/main/kotlin/**/*.ktsrc/test/kotlin/**/*.kt/* (C)$YEAR */
```
--------------------------------
### Configure clang-format for C#
Source: https://github.com/diffplug/spotless/blob/main/plugin-gradle/README.md
Set up clang-format for C# code, including specifying the target files, clang-format version, code style, and the path to the executable if not on the system's PATH.
```gradle
spotless {
format 'csharp', {
// you have to set the target manually
target 'src/**/*.cs'
clangFormat('10.0.1') // version is optional. Explicitly allow "any found version" with '*'.
// can also specify a code style
clangFormat().style('LLVM') // or Google, Chromium, Mozilla, WebKit
// TODO: support arbitrary .clang-format
// if clang-format is not on your path, you must specify its location manually
clangFormat().pathToExe('/usr/local/Cellar/clang-format/10.0.1/bin/clang-format')
// Spotless always checks the version of the clang-format it is using
// and will fail with an error if it does not match the expected version
// (whether manually specified or default). If there is a problem, Spotless
// will suggest commands to help install the correct version.
// TODO: handle installation & packaging automatically - https://github.com/diffplug/spotless/issues/673
}
}
```
--------------------------------
### Configure IntelliJ IDEA Formatter
Source: https://github.com/diffplug/spotless/blob/main/plugin-gradle/README.md
Use this snippet to integrate IntelliJ IDEA's command line formatter into your Spotless configuration. You must manually set the target files and can optionally specify custom code style settings paths and the binary path if IDEA is not in your system's PATH.
```gradle
spotless {
format 'myFormatter', {
// you have to set the target manually
target 'src/main/**/*.java','jbang/*.java'
idea()
.codeStyleSettingsPath('/path/to/config') // if you have custom formatting rules, see below for how to extract/reference them
.withDefaults(false) // Disable using default code style settings when no custom code style is defined for a file type (default: true)
// if idea is not on your path, you must specify the path to the executable
idea().binaryPath('/path/to/idea')
}
}
```
--------------------------------
### Apply spotless formatting
Source: https://github.com/diffplug/spotless/blob/main/CONTRIBUTING.md
Run this command to format your code using Spotless. This is also part of the automated pipeline checks.
```shell
./gradlew spotlessApply
```
--------------------------------
### Use Biome Binary from System Path
Source: https://github.com/diffplug/spotless/blob/main/plugin-maven/README.md
Configures Spotless to use the 'biome' command found in the user's system PATH. No slashes are used in the executable name.
```xml
biome
```
--------------------------------
### Configure JavaScript and TypeScript Formatting with Rome
Source: https://github.com/diffplug/spotless/blob/main/plugin-gradle/CHANGES.md
Shows how to add the 'rome' step for formatting JavaScript and TypeScript code using the Rome formatter.
```gradle
spotless {
javascript {
targetExcludeIfContentContains("**/package.json", "**/package-lock.json")
rome()
}
typescript {
targetExcludeIfContentContains("**/package.json", "**/package-lock.json")
rome()
}
}
```