Java Code Formatting Maven Tools

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

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

  • JDK — 1.8

Maven Lifecycle Phase

Configuration Parameters

Fig 1: impsort-maven-plugin parameters

For additional parameters, refer this.

Plugin Goals

<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

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

  • JDK — Default target for maven-compiler-plugin version 3.8.1

Maven Lifecycle Phase

Configuration Parameters

Fig 6: Configuration parameters of formatter-maven-plugin

For additional parameters, refer this.

Plugin Goals

<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.

Software Architect at Paypal