|
| 1 | +// :api-spotless: {site-url}/api/latest/mill/scalalib/spotless |
| 2 | + |
| 3 | +// If your project has file groups each requiring different formatting, |
| 4 | +// you may want to give Mill's https://github.com/diffplug/spotless[Spotless] plugin a try. |
| 5 | +// It supports formatting all files with a single command |
| 6 | +// as opposed to using a different plugin/command for each group. |
| 7 | + |
| 8 | +// We define a module that extends link:{api-spotless}/SpotlessModule.html[SpotlessModule] and |
| 9 | +// provide a JSON configuration file with the format specifications. |
| 10 | +package build |
| 11 | + |
| 12 | +import mill.javalib.JavaModule |
| 13 | +import mill.scalalib.spotless.* |
| 14 | + |
| 15 | +object `package` extends JavaModule with SpotlessModule |
| 16 | + |
| 17 | +/** See Also: src/A.java */ |
| 18 | +/** See Also: resources/app.properties */ |
| 19 | +/** See Also: LICENSE */ |
| 20 | +/** See Also: .spotless-formats.json */ |
| 21 | + |
| 22 | +// As per the specifications: |
| 23 | + |
| 24 | +// * The `build.mill` is to be formatted using `ScalaFmt` step. |
| 25 | +// * The `resources/app.properties` file is to be formatted using `TrimTrailingWhitespace` step. |
| 26 | +// * All Java files are to be formatted using `PalantirFormatJava` and `LicenseHeader` steps. |
| 27 | + |
| 28 | +// NOTE: Most fields have default values and can be omitted in the JSON file. |
| 29 | +// An example is the `LicenseHeader.header` field that references the `LICENSE` file. |
| 30 | + |
| 31 | +// Next, we run the inherited `spotless` command to check/apply the format specifications. |
| 32 | + |
| 33 | +/** Usage |
| 34 | +> ./mill spotless --check # check fails initially |
| 35 | +checking format in 1 mill files |
| 36 | +format errors in build.mill |
| 37 | +checking format in 1 properties files |
| 38 | +format errors in resources/app.properties |
| 39 | +checking format in 1 java files |
| 40 | +format errors in src/A.java |
| 41 | +format errors in 3 files |
| 42 | +error: ...format check failed for 3 files |
| 43 | + |
| 44 | +> ./mill spotless # auto-fix format |
| 45 | +formatting build.mill |
| 46 | +formatting resources/app.properties |
| 47 | +formatting src/A.java |
| 48 | +formatted 3 files |
| 49 | +format completed |
| 50 | + |
| 51 | +> ./mill spotless # fast incremental format |
| 52 | +1 mill files are already formatted |
| 53 | +1 properties files are already formatted |
| 54 | +1 java files are already formatted |
| 55 | +*/ |
| 56 | + |
| 57 | +// This demonstrates how different file groups can be formatted with a single command. |
| 58 | + |
| 59 | +// For the full list of format steps and configuration options, please refer to the |
| 60 | +// link:{api-spotless}/Format.html[API documentation]. |
| 61 | + |
| 62 | +// TIP: For a multi-module project, it is sufficient to extend `SpotlessModule` in your |
| 63 | +// build root module and define a format specification for each use-case. |
| 64 | + |
| 65 | +// You can also run `spotless` xref:fundamentals/modules.adoc#_external_modules[globally] |
| 66 | +// if you prefer not to have to extend `SpotlessModule`. |
| 67 | + |
| 68 | +/** Usage |
| 69 | +> ./mill mill.scalalib.spotless.SpotlessModule/ --check |
| 70 | +checking format in 1 mill files |
| 71 | +checking format in 1 properties files |
| 72 | +checking format in 1 java files |
| 73 | +format check completed |
| 74 | +*/ |
0 commit comments