1. add spotless plugin to pom.xml
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>2.43.0</version>
<executions>
<execution>
<goals>
<goal>check</goal>
<goal>apply</goal>
</goals>
</execution>
</executions>
</plugin>
here, update <version>2.43.0</version> appropriately
google-java-format versions
2. add spotless configuratioin
<configuration>
<java>
<includes>
<include>src/main/java/**/*.java</include>
<include>src/test/java/**/*.java</include>
</includes>
<googleJavaFormat>
<version>1.28.0</version>
<style>AOSP</style>
</googleJavaFormat>
<importOrder />
<removeUnusedImports />
</java>
</configuration>
more spotless format configuration
3. add hooks/pre-commit
#!/usr/bin/env bash
mvn spotless:apply
4. configure maven plugin to install a pre-commit hook
<plugin>
<groupId>com.rudikershaw.gitbuildhook</groupId>
<artifactId>git-build-hook-maven-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<installHooks>
<pre-commit>hooks/pre-commit</pre-commit>
</installHooks>
</configuration>
<executions>
<execution>
<goals>
<goal>install</goal>
</goals>
</execution>
</executions>
</plugin>
you should be good to go
things learned
based on https://github.com/google/google-java-format, it looks like we add
<dependency>
<groupId>com.google.googlejavaformat</groupId>
<artifactId>google-java-format</artifactId>
<version>${google-java-format.version}</version>
</dependency>
why do we add it under <plugin> tag?
✏️
-
dependenciesinpom.xmlare for things application code needs at compile/run/test time -
pluginsare for tools that run during the build (formatters, linters, code generators, packagers) -
google-java-formatis a formatter engine, not something app code should ship with
all combined
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>2.43.0</version>
<executions>
<execution>
<goals>
<goal>check</goal>
<goal>apply</goal>
</goals>
</execution>
</executions>
<configuration>
<java>
<includes>
<include>src/main/java/**/*.java</include>
<include>src/test/java/**/*.java</include>
</includes>
<googleJavaFormat>
<version>1.28.0</version>
<style>AOSP</style>
</googleJavaFormat>
<importOrder />
<removeUnusedImports />
</java>
</configuration>
</plugin>
<plugin>
<groupId>com.rudikershaw.gitbuildhook</groupId>
<artifactId>git-build-hook-maven-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<installHooks>
<pre-commit>hooks/pre-commit</pre-commit>
</installHooks>
</configuration>
<executions>
<execution>
<goals>
<goal>install</goal>
</goals>
</execution>
</executions>
</plugin>