You can let pre-commit hook trigger formatter for files staged for commit.

git-code-format-maven-plugin uses google-java-format formatter and can install client-side pre-commit git hook during compile phase. It requires Maven 3.5.x, which should be enforced.

<build>
  <plugins>
    <plugin>
      <groupId>com.cosium.code</groupId>
      <artifactId>git-code-format-maven-plugin</artifactId>
      <version>VERSION</version>
      <executions>
        <execution>
          <goals>
            <goal>install-hooks</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    <plugin>
      <artifactId>maven-enforcer-plugin</artifactId>
      <version>VERSION</version>
      <executions>
        <execution>
          <goals>
            <goal>enforce</goal>
          </goals>
          <configuration>
            <rules>
              <requireMavenVersion>
                <version>[3.5.4,)</version>
              </requireMavenVersion>
            </rules>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Point to standalone Maven in IDE as git-code-format-maven-plugin does not play along nicely with embedded Maven.

mvn compile to get hook installed. For IDEA, that's it.

As git-code-format-maven-plugin only formats changed files (which is good), it is probably good to format whole project upfront once (mvn git-code-format:format-code -Dgcf.globPattern=**/*).

Workaround for Eclipse

Because of a bug in EGit, which sometimes ignores Git hooks completely, developers using Eclipse on Windows should have Cygwin in PATH. An empty cygpath.exe will do. Run 'Command Prompt' as a administrator and execute C:\>echo "" > /"Program Files"/Git/bin/cygpath.exe (kudos to hook is not working eclipse egit client).

Reboot.

A note on java import statements ordering

Optimise imports or reformat in IDE or reformat with plugins, can lead to changes in imports ordering. A nasty surprise if an older version of git-code-format-maven-plugin is being used together with fmt-maven-plugin (to format or validate code later in CI, for example).

  • git-code-format-maven-plugin will sort imports (since version 1.20)
  • fmt-maven-plugin will always sort imports
  • googleformatter-maven-plugin can optionally sort imports (not per default)
Answer from zziga on Stack Overflow
🌐
Maven Repository
mvnrepository.com › artifact › com.google.googlejavaformat › google-java-format
Maven Repository: com.google.googlejavaformat » google-java-format
July 30, 2026 - A Java source code formatter that follows Google Java Style. ... aar android apache api arm assets build build-system bundle client clojure cloud config cran data database eclipse example extension framework github gradle groovy io ios javascript jvm kotlin library logging macos maven mobile ...
🌐
Maven Repository
mvnrepository.com › artifact › com.google.googlejavaformat
Maven Repository: com.google.googlejavaformat
A Java source code formatter that follows Google Java Style. ... aar android apache api arm assets build build-system bundle client clojure cloud config data database eclipse example extension framework github gradle groovy io ios javascript jvm kotlin library logging maven mobile model module ...
🌐
Maven Central Repository
search.maven.org › artifact › com.google.googlejavaformat › google-java-format
Maven Central: com.google.googlejavaformat:google-java-format
--> <project xmlns="http://mav... <artifactId>google-java-format</artifactId> <name>Google Java Format</name> <description> A Java source code formatter that follows Google Java Style....
🌐
GitHub
github.com › google › google-java-format
GitHub - google/google-java-format: Reformats Java source code to comply with Google Java Style. · GitHub
August 14, 2026 - The formatter can be used in software which generates java to output more legible java code. Just include the library in your maven/gradle/etc. configuration. google-java-format uses internal javac APIs for parsing Java source.
Author: google
🌐
Maven Central Repository
search.maven.org › artifact › com.google.googlejavaformat › google-java-format › 1.8 › jar
Maven Central: com.google.googlejavaformat:google-java-format:1.8
--> <project xmlns="http://mav... <artifactId>google-java-format</artifactId> <name>Google Java Format</name> <description> A Java source code formatter that follows Google Java Style....
Top answer
1 of 4
8

You can let pre-commit hook trigger formatter for files staged for commit.

git-code-format-maven-plugin uses google-java-format formatter and can install client-side pre-commit git hook during compile phase. It requires Maven 3.5.x, which should be enforced.

<build>
  <plugins>
    <plugin>
      <groupId>com.cosium.code</groupId>
      <artifactId>git-code-format-maven-plugin</artifactId>
      <version>VERSION</version>
      <executions>
        <execution>
          <goals>
            <goal>install-hooks</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    <plugin>
      <artifactId>maven-enforcer-plugin</artifactId>
      <version>VERSION</version>
      <executions>
        <execution>
          <goals>
            <goal>enforce</goal>
          </goals>
          <configuration>
            <rules>
              <requireMavenVersion>
                <version>[3.5.4,)</version>
              </requireMavenVersion>
            </rules>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Point to standalone Maven in IDE as git-code-format-maven-plugin does not play along nicely with embedded Maven.

mvn compile to get hook installed. For IDEA, that's it.

As git-code-format-maven-plugin only formats changed files (which is good), it is probably good to format whole project upfront once (mvn git-code-format:format-code -Dgcf.globPattern=**/*).

Workaround for Eclipse

Because of a bug in EGit, which sometimes ignores Git hooks completely, developers using Eclipse on Windows should have Cygwin in PATH. An empty cygpath.exe will do. Run 'Command Prompt' as a administrator and execute C:\>echo "" > /"Program Files"/Git/bin/cygpath.exe (kudos to hook is not working eclipse egit client).

Reboot.

A note on java import statements ordering

Optimise imports or reformat in IDE or reformat with plugins, can lead to changes in imports ordering. A nasty surprise if an older version of git-code-format-maven-plugin is being used together with fmt-maven-plugin (to format or validate code later in CI, for example).

  • git-code-format-maven-plugin will sort imports (since version 1.20)
  • fmt-maven-plugin will always sort imports
  • googleformatter-maven-plugin can optionally sort imports (not per default)
2 of 4
1

In order to run this formatter after each developer commit, you will have to first have a Jenkins commit hook in place, that will trigger a Jenkins build. One of the phases of the build, should execute the fmt-maven-plugin's (or any others) check functionality in order to ensure that the code is properly formatted.

Adding a webhook

First thing to do is add a webhook that will trigger a Jenkins build after every commit in your git repository. You can find how to do this here. For Gitlab specific instructions, this post from medium may be helpful.

Executing the check

This can be done by executing the check goal on the fmt-maven-plugin

Maven acceps either <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal> as a means of calling a plugin goal, so for your specific problem, you can run:

mvn fmt:check

That being said, you will have to add a Jenkins build step, that will run the mentioned command. Step 5 from this tutorial shows you how to add a build step.

Hope that this actually helps :D

🌐
GitHub
github.com › google › google-java-format › blob › master › pom.xml
google-java-format/pom.xml at master · google/google-java-format
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> · <groupId>com.google.googlejavaformat</groupId> <artifactId>google-java-format-parent</artifactId> <packaging>pom</packaging> <version>HEAD-SNAPSHOT</version> ·
Author: google
🌐
GitHub
github.com › google › google-java-format › issues › 69
Maven plugin · Issue #69 · google/google-java-format
August 19, 2016 - Support using Google Java Format as part a build in Maven thru a plugin. Google coding styles helps a great deal to apply a common scheme to a code repository. Although, there are ways to share/aut...
Author: google
Find elsewhere
🌐
GitHub
gist.github.com › sandeepkv93 › 51db90f0e288cf21ca7ff34aa143d828
Automated Java Code Formatting with Maven using google-java-format · GitHub
This guide explains how to set up automated code formatting in a Java Maven project using Google's Java code formatter via the fmt-maven-plugin.
🌐
Medium
medium.com › @nirav-shah › code-formatting-using-spotless-google-java-format-for-java-maven-projects-with-git-pre-commit-860d6c15bf7c
Code Formatting Using Spotless & Google Java Format for Java Maven Projects with Git Pre Commit Hooks | by Nirav Shah | Medium
September 21, 2023 - Technogoloy & Plugin versions that I have used for article are, java 1.8, spotless maven plugin version 2.9.0, Google Java Formatter (version 1.7 which is supported for java 1.8), Git Build Hook Maven Plugin. You can download an example project from here.
🌐
GitHub
github.com › talios › googleformatter-maven-plugin
GitHub - talios/googleformatter-maven-plugin: Plugin for the Apache Maven build system to reformat source files. · GitHub
<plugin> <groupId>com.theoryinpractise</groupId> <artifactId>googleformatter-maven-plugin</artifactId> <version>1.7.3</version> <executions> <execution> <id>reformat-sources</id> <configuration> <includeStale>false</includeStale> <style>GOOGLE</style> <formatMain>true</formatMain> <formatTest>true</formatTest> <filterModified>false</filterModified> <skip>false</skip> <fixImports>false</fixImports> <maxLineLength>100</maxLineLength> </configuration> <goals> <goal>format</goal> </goals> <phase>process-sources</phase> </execution> </executions> </plugin> ... Exposed formatter.modified to reformat only changed SCM files. Requires Java 8 to run now.
Author: talios
🌐
GitHub
github.com › harvanir › maven-google-java-format
GitHub - harvanir/maven-google-java-format
Many repositories within company isn't use java code format convention. This plugin to solve them. ... <build> </plugins> <plugin> <groupId>org.harvanir.maven</groupId> <artifactId>google-java-format</artifactId> <version>1.0.0-SNAPSHOT</version> <executions> <execution> <goals> <goal>install</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
Author: harvanir
🌐
Medium
aru-sha4.medium.com › java-check-style-and-formatting-using-maven-a1a1b4e6e10a
Java Check-style and Formatting using Maven | by Arushi Sharma | Medium
January 7, 2020 - Here is the breakdown of the entire ... code formatting, to follow a particular style guide. These steps have been tested with Maven 3.0.4 and Java 1.7 · Maven has a dedicated Checkstyle plugin, maven-checkstyle-plugin that generates the report of code style violations. It follows a pre-defined style ruleset which can either be a custom file or either of the two predefined files: sun_checks.xml (follows the Sun Code Conventions) or google_checks.xml ...
🌐
Maven Central
central.sonatype.com › artifact › com.google.googlejavaformat › google-java-format › 1.21.0
Maven Central: com.google.googlejavaformat:google-java-format:1.21.0
--> <project xmlns="http://mav... <artifactId>google-java-format</artifactId> <name>Google Java Format</name> <description> A Java source code formatter that follows Google Java Style....
🌐
Maven Repository
mvnrepository.com › artifact › com.github.sherter.google-java-format
Maven Repository: com.github.sherter.google-java-format
aar android apache api arm assets build build-system bundle client clojure cloud config cran data database eclipse example extension framework github gradle groovy io ios javascript jvm kotlin library logging maven mobile module npm osgi persistence plugin resources rlang sdk server service ...