Skip to content

Commit 04cc24e

Browse files
authored
Add documentation showing how to share configuration (#2643)
2 parents ad04d48 + 16a3008 commit 04cc24e

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

plugin-gradle/README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ Spotless supports all of Gradle's built-in performance features (incremental bui
9393
- [Dependency resolution modes](#dependency-resolution-modes)
9494
- [How do I preview what `spotlessApply` will do?](#how-do-i-preview-what-spotlessapply-will-do)
9595
- [Can I apply Spotless to specific files?](#can-i-apply-spotless-to-specific-files)
96+
- [How to centralize Spotless configuration](#central-configuration)
9697
- [Example configurations (from real-world projects)](#example-configurations-from-real-world-projects)
9798

9899
***Contributions are welcome, see [the contributing guide](../CONTRIBUTING.md) for development info.***
@@ -1913,8 +1914,6 @@ If you use this feature, you will get an error if you use a formatter in a subpr
19131914
- If you don't like what spotless did, `git reset --hard`
19141915
- If you'd like to remove the "checkpoint" commit, `git reset --soft head~1` will make the checkpoint commit "disappear" from history, but keeps the changes in your working directory.
19151916
1916-
<a name="examples"></a>
1917-
19181917
## Can I apply Spotless to specific files?
19191918
19201919
You can target specific files by setting the `spotlessFiles` project property to a comma-separated list of file patterns:
@@ -1925,6 +1924,31 @@ cmd> gradle spotlessApply -PspotlessFiles=my/file/pattern.java,more/generic/.*-p
19251924
19261925
The patterns are matched using `String#matches(String)` against the absolute file path.
19271926
1927+
<a name="central-configuration"></a>
1928+
1929+
## How to centralize Spotless configuration
1930+
1931+
If you want to centralize your Spotless configuration for use across many projects, you might want to consider the [Blowdryer](https://github.com/diffplug/blowdryer) plugin.
1932+
1933+
If you are content with only centralizing configuration files, it is possible to define a common configuration that is deployed as a standard artifact so that it can be then be reused by each project. For example:
1934+
1935+
```kotlin
1936+
val spotlessConfig by configurations.creating
1937+
dependencies {
1938+
// 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.
1939+
spotlessConfig("org.mycompany:code-configuration:1.0.0")
1940+
}
1941+
spotless {
1942+
java {
1943+
removeUnusedImports()
1944+
importOrder(resources.text.fromArchiveEntry(spotlessConfig, "java-import-order.txt").asString())
1945+
eclipse().configXml(resources.text.fromArchiveEntry(spotlessConfig, "java-formatter.xml").asString())
1946+
}
1947+
}
1948+
```
1949+
1950+
<a name="examples"></a>
1951+
19281952
## Example configurations (from real-world projects)
19291953
19301954
* [A few thousand github projects](https://github.com/search?l=gradle&q=spotless&type=Code)

plugin-maven/README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ user@machine repo % mvn spotless:check
7272
- [Disabling warnings and error messages](#disabling-warnings-and-error-messages)
7373
- [How do I preview what `mvn spotless:apply` will do?](#how-do-i-preview-what-mvn-spotlessapply-will-do)
7474
- [Can I apply Spotless to specific files?](#can-i-apply-spotless-to-specific-files)
75+
- [How to centralize Spotless configuration](#central-configuration)
7576
- [Example configurations (from real-world projects)](#example-configurations-from-real-world-projects)
7677

7778
***Contributions are welcome, see [the contributing guide](../CONTRIBUTING.md) for development info.***
@@ -2068,7 +2069,39 @@ You can adjust this with
20682069

20692070
Note that for Incremental build support the goals have to be bound to a phase prior to `test`.
20702071

2071-
<a name="examples"></a>
2072+
<a name="central-configuration"></a>
2073+
2074+
## How to centralize Spotless configuration
2075+
2076+
Rather than copying the formatter files across many projects, it is possible to define a common configuration that is deployed as a standard artifact so that it can be then be reused by each project; for example:
2077+
2078+
```xml
2079+
<plugin>
2080+
<groupId>com.diffplug.spotless</groupId>
2081+
<artifactId>spotless-maven-plugin</artifactId>
2082+
<version>${spotless-maven-plugin.version}</version>
2083+
<dependencies>
2084+
<dependency>
2085+
<groupId>org.mycompany</groupId>
2086+
<artifactId>code-configuration</artifactId>
2087+
<version>1.0.0</version>
2088+
</dependency>
2089+
</dependencies>
2090+
<configuration>
2091+
<java>
2092+
<removeUnusedImports/>
2093+
<importOrder>
2094+
<file>java-import-order.txt</file>
2095+
</importOrder>
2096+
<eclipse>
2097+
<file>java-formatter.xml</file>
2098+
</eclipse>
2099+
<lineEndings>UNIX</lineEndings>
2100+
</java>
2101+
</configuration>
2102+
```
2103+
2104+
In this example, 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.
20722105

20732106
## Example configurations (from real-world projects)
20742107

0 commit comments

Comments
 (0)