Java Code Formatting Maven Tools

Arijeet Saha
4 min readAug 7, 2021

Code written today has a good chance of getting the functionality changed tomorrow. So the readability of the code is of profound importance which makes it easier for maintainability and extensibility. Hence the importance of keeping a consistent coding formatting style.

Useful Maven Tools

1. impsort-maven-plugin

This plugin is used to automatically sort/validate the order of all the import statements in the Java source file. In addition to sort, it provides a lot of other useful functionalities which help to keep the codebase consistent like grouping of imports and remove unused imports.

Example:

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>net.revelc.code</groupId>
<artifactId>impsort-maven-plugin</artifactId>
<version>1.6.2</version>
<configuration>
<removeUnused>true</removeUnused>
<staticGroups>*</staticGroups>
<groups>java.,javax.,org.,com.</groups>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugin>
<groupId>net.revelc.code</groupId>
<artifactId>impsort-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>sort</goal>
</goals>
</execution>
</executions>
</plugin>
</build>

System Requirements

  • Maven — 3.3.9 and above
  • JDK — 1.8

Maven Lifecycle Phase

Binds to process-sources.

Configuration Parameters

Below are the few configuration parameters which are supported by the impsort-maven-plugin which are very useful.

Fig 1: impsort-maven-plugin parameters

For additional parameters, refer this.

Plugin Goals

  • check
<plugin>
<groupId>net.revelc.code</groupId>
<artifactId>impsort-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>

This is used to validate all the imports in the Java source files based on the added configuration parameters.

  • sort
<plugin>
<groupId>net.revelc.code</groupId>
<artifactId>impsort-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>

This goal is used to sort/modify all the imports in the Java source files based on the added configuration parameters.

Example

Fig 2: Java Source file with unused, unsorted and ungrouped imports

After execution of mvn clean install with goal as “check” -

Fig 3: Build Failure reported by import-maven-plugin

After execution of mvn clean install with goal as “sort” -

Fig 4: Sorted imports with the removal of duplicate imports

Below impsort-maven-plugin config is used for the above result -

Fig 5: impsort-maven-plugin with configuration parameters

2. formatter-maven-plugin

This plugin is used to automatically (re)format/validate a maven project(including source code). Keeping a coding style format consistent following certain specifications is an important requirement preventing unnecessary conflicts and code diffs. So, teams usually maintain a common style but need to share it across all team members. To prevent this manual effort of sharing and importing the style sheet in IDE, this plugin is recommended which will format the code on every maven install/verify.

Example:

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>net.revelc.code.formatter</groupId>
<artifactId>formatter-maven-plugin</artifactId>
<version>2.16.0</version>
<configuration>
<configFile>formatter-maven-plugin/eclipse/java.xml</configFile>
<configJsonFile>formatter-maven-plugin/jackson/json.properties</configJsonFile>
<configXmlFile>formatter-maven-plugin/eclipse/xml.properties</configXmlFile>
<encoding>UTF-8</encoding>
<sourceDirectory>.</sourceDirectory>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<plugins>
<plugin>
<groupId>net.revelc.code.formatter</groupId>
<artifactId>formatter-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>format</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

System Requirements

  • Maven — 3.3.9 and above
  • JDK — Default target for maven-compiler-plugin version 3.8.1

Maven Lifecycle Phase

Binds to process-sources.

Configuration Parameters

Below are the few configuration parameters which are supported by the formatter-maven-plugin which are very useful.

Fig 6: Configuration parameters of formatter-maven-plugin

For additional parameters, refer this.

Plugin Goals

  • format
<plugin>
<groupId>net.revelc.code.formatter</groupId>
<artifactId>formatter-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>format</goal>
</goals>
</execution>
</executions>
</plugin>

This is used to format the maven project which includes all the filetypes mentioned in the formatter plugin.

  • validate
<plugin>
<groupId>net.revelc.code.formatter</groupId>
<artifactId>formatter-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>validate</goal>
</goals>
</execution>
</executions>
</plugin>

This is used to validate the format of all the filetypes mentioned in the formatter plugin.

Example:

Fig 7: unformatted java source code
Fig 8: Unformatted JSON file

After execution of mvn clean install with goal as “format” -

Fig 9: formatted java source code using formatter-maven-plugin
Fig 10: formatted json using formatter-maven-plugin

After execution of mvn clean install with goal as “validate” -

Fig 11: Build failure due to unformatted json file
Fig 12: Build failure due to unformatted java source file

Thanks for reading! Feel free to comment or message me, when you have questions or suggestions.

--

--