Automatically enforce code formatting in Maven projects
#java #mavenPut this plugin in your POM file:
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>${spotless-maven-plugin.version}</version>
<configuration>
<pom>
<sortPom>
<expandEmptyElements>false</expandEmptyElements>
<keepBlankLines>false</keepBlankLines>
<endWithNewline>true</endWithNewline>
</sortPom>
</pom>
<java>
<removeUnusedImports/>
<googleJavaFormat>
<version>${google-java-format.version}</version>
<style>GOOGLE</style>
<reflowLongStrings>true</reflowLongStrings>
<formatJavadoc>true</formatJavadoc>
</googleJavaFormat>
</java>
</configuration>
<executions>
<execution>
<id>check-code-formatting</id>
<goals>
<goal>check</goal>
</goals>
<phase>process-sources</phase>
</execution>
</executions>
</plugin>
As of writing this, I'm using:
<spotless-maven-plugin.version>2.44.4</spotless-maven-plugin.version>
<google-java-format.version>1.26.0</google-java-format.version>
This plugin will enforce Google's Java format on your code, and it will run these checks during the process-sources
phase, which happens just before: generate-resources
-> process-resources
-> compile
, and just after generate-sources
.