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
🌐
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
🌐
GitHub
github.com › talios › googleformatter-maven-plugin
GitHub - talios/googleformatter-maven-plugin: Plugin for the Apache Maven build system to reformat source files. · GitHub
A simple Apache Maven plugin to reformat a projects source/test-sources using the google-java-format project to conform with the Google Code Style Guide.
Author: talios
🌐
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 ...
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 › 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
🌐
GitHub
github.com › harvanir › maven-google-java-format
GitHub - harvanir/maven-google-java-format
Simplifying maven plugin setup to integrate with google-java-format using pre-commit hook.
Author: harvanir
Top answer
1 of 1
3

You need to include a maven plugin that supports Googles Java format. It seems like your goal verifyGoogleJavaFormat is available for Gradle, but not for Maven. One option is to use https://github.com/Cosium/git-code-format-maven-plugin. It supports format verification and automatic formatting upon comitting.

The Github Repo documentation states that you can include it in your pom.xml like this:

<build>
    <plugins>
        <plugin>
            <groupId>com.cosium.code</groupId>
            <artifactId>git-code-format-maven-plugin</artifactId>
            <version>3.3</version>
            <executions>
                <!-- On commit, format the modified java files -->
                <execution>
                    <id>install-formatter-hook</id>
                    <goals>
                        <goal>install-hooks</goal>
                    </goals>
                </execution>
                <!-- On Maven verify phase, fail if any file
                (including unmodified) is badly formatted -->
                <execution>
                    <id>validate-code-format</id>
                    <goals>
                        <goal>validate-code-format</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Now, when I run mvn verify on my unformatted project I get an error:

[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  6.090 s
[INFO] Finished at: 2022-03-12T17:56:13+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.cosium.code:git-code-format-maven-plugin:3.3:validate-code-format (validate-code-format) on project javatest: /home/bisensee/repos/javatest/src/main/java/Position.java is not correctly formatted ! -> [Help 1] 

When we make our next commit the formatter is automatically applied via a Git hook and the next mvn verify succeeds.

Find elsewhere
🌐
GitHub
github.com › Cosium › git-code-format-maven-plugin
GitHub - Cosium/git-code-format-maven-plugin: A maven plugin that automatically deploys code formatters as pre-commit git hook · GitHub
<build> <plugins> <plugin> <groupId>com.cosium.code</groupId> <artifactId>git-code-format-maven-plugin</artifactId> <version>${git-code-format-maven-plugin.version}</version> <executions> <!-- On commit, format the modified files --> <execution> <id>install-formatter-hook</id> <goals> <goal>install-hooks</goal> </goals> </execution> <!-- On Maven verify phase, fail if any file (including unmodified) is badly formatted --> <execution> <id>validate-code-format</id> <goals> <goal>validate-code-format</goal> </goals> </execution> </executions> <dependencies> <!-- Enable https://github.com/google/google-java-format --> <dependency> <groupId>com.cosium.code</groupId> <artifactId>google-java-format</artifactId> <version>${git-code-format-maven-plugin.version}</version> </dependency> </dependencies> </plugin> </plugins> </build>
Author: Cosium
🌐
GitHub
gist.github.com › sandeepkv93 › 51db90f0e288cf21ca7ff34aa143d828
Automated Java Code Formatting with Maven using google-java-format · GitHub
Google Java Format (google-java-format): Google's Java code formatter that enforces Google Java Style · Spotify's fmt-maven-plugin: A Maven plugin that integrates google-java-format into the Maven build lifecycle
🌐
GitHub
github.com › spotify › fmt-maven-plugin
GitHub - spotify/fmt-maven-plugin: Opinionated Maven Plugin that formats your Java code. · GitHub
If you want your IDE to stick to the same format, google-java-format also includes integrations for IntelliJ and Eclipse IDE's, following the installation instructions on the README. To have your sources automatically formatted on each build, add to your pom.xml: <build> <plugins> <plugin> <groupId>com.spotify.fmt</groupId> <artifactId>fmt-maven-plugin</artifactId> <version>VERSION</version> <executions> <execution> <goals> <goal>format</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>com.google.googlejavaformat</groupId> <artifactId>google-java-format</artifactId> <version>1.27.0</version> </dependency> </dependencies> </plugin> </plugins> </build>
Author: spotify
🌐
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 ...
🌐
GitHub
github.com › google › google-java-format › blob › master › pom.xml
google-java-format/pom.xml at master · google/google-java-format
<artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${java.version}</source> <target>${java.version}</target> <encoding>UTF-8</encoding> <compilerArgs> <!-- compile-time arguments for google-java-format --> <arg>-XDcompilePolicy=simple</arg> <arg>--should-stop=ifError=FLOW</arg> <arg>-Xplugin:ErrorProne</arg> <arg>--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED</arg> <arg>--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED</arg>
Author: google
🌐
GitHub
github.com › qbixus › google-java-format
GitHub - qbixus/google-java-format: Reformats Java source code to comply with Google Java Style. · GitHub
The plugin adds a google-java-format formatter implementation that can be configured in Window > Preferences > Java > Code Style > Formatter > Formatter Implementation. ... Cosium/maven-git-code-format: A maven plugin that automatically deploys ...
Author: qbixus
🌐
Maven Repository
mvnrepository.com › artifact › com.github.google.google-java-format
Maven Repository: com.github.google.google-java-format
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 maven mobile module ...
🌐
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 ...
🌐
Trguduru
trguduru.github.io › 2019 › 06 › java-code-formatting-maven-plugin
Java Code Formatting Maven Plugin - thiru's thoughts
June 26, 2019 - <plugin> <groupId>net.revelc.code.formatter</groupId> <artifactId>formatter-maven-plugin</artifactId> <version>2.10.0</version> <configuration> <!--Using the provided code formatting style to format the code --> <configFile>${project.basedir}/src/etc/google-code-style.xml</configFile> </configuration> <executions> <execution> <id>format</id> <goals> <goal>format</goal> </goals> <phase>process-resources</phase> </execution> <execution> <id>validate</id> <goals> <goal>validate</goal> </goals> <phase>compile</phase> </execution> </executions> </plugin> Make sure to copy the google java code style into your project folders, here i copied into src/etc folder and configured the same in the plugin configuration. Now run the maven build command such as mvn clean install.
🌐
GitHub
github.com › sherter › google-java-format-gradle-plugin
GitHub - sherter/google-java-format-gradle-plugin · GitHub
plugins { id 'com.github.sherter.google-java-format' version '0.9' } Make sure you have defined a repository that contains version 1.8 of google-java-format · repositories { mavenCentral() } Execute the task googleJavaFormat to format all *.java files in the project ·
Author: sherter