Recommended setup for formatting files with Spotless #133
Replies: 1 comment 1 reply
-
Hi Thomas, Sorry for the late answer, I've been very busy, and forgot about this discussion. What you are asking for, goes beyond just formatting. The need can arise for any other dependency/config, I'd like this plugin to be as closed as possible to what Spring Initiliaz'r API generates, which is why I've been reluctant to change Nonetheless, I'm monitoring the aforementioned issue from Spring. |
Beta Was this translation helpful? Give feedback.
-
I'm exploring how to use Spotless to format Java code in the context of an Nx monorepo. The idea came thanks to this generator, which configures Gradle and Maven projects to format files with Spotless. In this context, it is important to note that Spotless can be used to format files for different languages and file types (e.g.
.md
,.sql
, etc.).A nice-to-have feature for my team is that files are automatically formatted when saving files. This gives the developer a chance to review immediately the final code before closing the files. A similar approach - less real-time - would be to programmatically run the formatter in a pre-commit git hook, for example. The review of the formatted files would then be performed via the PR review.
The VS Code extension Spotless Gradle suggested by the Spotless project is built on top of Spotless + Gradle to provide IntelliSense and formatting on save. This plugin has the following requirements:
gradlew
(orgradle.bat
) to be included in the workspace root folder.build.gradle
with the following content or similar must be included in the workspace root folder.To summarize, the above configuration enables:
build.gradle
.One drawback of this approach compared to the project-level Spotless configuration generated by this generator is that I loose the caching mechanism provided by Nx (e.g. if the project task
format-check
is added to the list of cacheable Nx tasks). For example, git hooks and CI workflows would run faster if only the files/projects that have been changed have their format checked.Is there a way for a project-specific
build.gradle
to inherit the Spotless configuration defined in the workspace-levelbuild.gradle
? This way we would have a unique Spotless configuration that is easier to maintain and update, while still benefiting from Nx cache and parallel/distributed execution.Beta Was this translation helpful? Give feedback.
All reactions