bgalek/gradle-prettier

Gradle meets prettier!

★ 0Forks 0KotlinGitHub ↗Compare

README

gradle-prettier

CI Gradle Plugin Portal License

A fast Gradle settings plugin for Prettier Java. The first assemble installs the tooling, then the pre-commit hook formats staged Java files by launching Prettier directly without starting Gradle.

Usage

Apply the plugin in settings.gradle.kts:

plugins {
  id("io.github.bgalek.gradle-prettier") version "<version>"
}

Features

  • Keeps commits quick by running Prettier directly on staged Java files, with no Gradle startup.
  • Keeps package.json, package-lock.json, node_modules, and the fallback .prettierrc under .gradle/prettier-java.
  • Uses an existing root .prettierrc as-is when present.
  • Uses system Node when available, otherwise downloads a managed Node distribution through an isolated Gradle Node project.
  • Installs the isolated Node and Prettier tooling during the first assemble run.
  • Adds a root prettier task for manually formatting all Java files.
  • Respects root .prettierignore and .gitignore files during manual formatting.
  • Supports Gradle's configuration cache.
  • Disables all setup automatically when the CI environment variable is present.
  • Warns and continues when Node, npm dependencies, or formatting cannot run.

Why this plugin?

The main difference is the commit-time path: assemble handles the one-time tooling setup, and the generated hook starts Node and Prettier directly instead of starting Gradle for every commit. The plugin is for teams that want this fast feedback loop alongside settings-level configuration, hidden Node files, and fail-open local formatting.

gradle-prettier Prettier Gradle Plugin Spotless Jacet
Commit-time path Direct Prettier; tooling installed during assemble No built-in hook Custom setup Direct CLI available; hook setup is manual
Formatter prettier-plugin-java prettier-plugin-java Framework supporting Prettier Java, Google Java Format, Eclipse, Palantir, and others Its own native Java formatter with deliberately different output
Applied in Root settings.gradle.kts once Every Gradle project or module Gradle project, with optional centralized configuration Each project using the Java plugin
Node handling Uses system Node or downloads a managed fallback Applies Gradle Node and always enables download Discovers Node/npm; can integrate with Gradle Node No Node dependency
npm files Private .gradle/prettier-java directory Project directory through npm install --save-dev Managed internally; npm install cache is configurable None
Configuration Existing root .prettierrc or private generated fallback Standard Prettier discovery Gradle DSL or explicit config file; Prettier config auto-discovery is not used .jacet.json or Gradle DSL
Manual formatting prettier prettierWrite spotlessApply formatJava
Formatting check Intentionally none prettierCheck, wired to check spotlessCheck, wired to check by default checkFormatJava
Git integration Installs a fail-open pre-commit hook for staged Java files None Optional pre-push hook; pre-commit requires custom setup CLI supports staged formatting; hook setup is manual
CI behavior Does nothing when CI is set Enforces its check task Enforces checks by default Check task is available for explicit CI wiring
Failure policy Warns and continues when tooling is unavailable Fails when install or formatting tasks fail Fails on formatting violations or formatter errors Leaves parse/verification failures unchanged with warnings; check fails for unformatted files

Choose gradle-prettier when quick commits are the priority: the first assemble installs the tooling, and staged Java files are then formatted without Gradle startup.

Gradle task

Format all Java files:

./gradlew prettier

The first assemble run installs the private npm dependencies and downloads Node only when no system installation is available. Later builds reuse the existing installation. You can also prepare the tooling explicitly:

./gradlew prettierSetup

If Node or npm installation fails, the setup task logs a warning and completes without failing the surrounding build. The generated pre-commit hook also fails open and points to prettierSetup when tooling is unavailable; it never starts Gradle itself.

The plugin is applied to settings, not to an individual project. This lets it format Java files across a multi-project build.

Configuration

Defaults are intentionally reproducible:

prettierJava {
    tabWidth = 4
    printWidth = 120
    useTabs = false
    prettierVersion = "3.8.1"
    prettierJavaVersion = "2.9.4"
    nodeVersion = "22.14.0"
    installGitHook = true
    overwriteGitHook = true
}
Option Description
tabWidth Indentation width in the generated fallback config.
printWidth Line width in the generated fallback config.
useTabs Whether the generated fallback config uses tabs.
prettierVersion Prettier npm package version.
prettierJavaVersion prettier-plugin-java npm package version.
nodeVersion Managed Node version used when downloadNode is enabled.
downloadNode Overrides automatic detection. By default, managed Node is downloaded only when node is missing from PATH.
installGitHook Creates the pre-commit hook when the build is inside a Git repository.
overwriteGitHook Allows the generated hook to replace a differing pre-commit hook.

The formatting options apply only to the generated fallback config. A root .prettierrc always takes precedence and is never modified.

Use system Node

System Node is selected automatically when node is on Gradle's PATH. To force system Node:

prettierJava {
    downloadNode = false
}

The Gradle Node plugin is applied only inside the private, standalone prettierTooling build because it owns the npm installation and formatter task. That classloader isolation means the consuming build can use a different Gradle Node version without either configuration affecting the other.

To force the managed Node version even when system Node is available, set downloadNode = true.

Disable the Git hook

prettierJava {
    installGitHook = false
}

CI can also disable it without changing the settings file:

./gradlew check -PinstallGitHooks=false

When the CI environment variable is present, the plugin performs no setup at all: it creates no files, tooling build, task, or hook. The prettierJava configuration block remains valid so the same settings file works locally and in CI.

EditorConfig

The plugin does not create or modify .editorconfig. To align editors with the default fallback configuration, add:

[*.java]
indent_style = space
indent_size = 4
tab_width = 4
max_line_length = 120

Keep these values aligned with useTabs, tabWidth, and printWidth when overriding the defaults.

Contributors

bgalek

Issues