The problem
In today’s world there are so many IDEs to choose from to develop Java applications and the IDEs have variety of code formatting configurations, sometimes it very hard to be consistent with code formatting.
The other important thing is when a developer modifies code and the IDEs reformats the code, with this its very hard to understand whats the modified code while looping at a pull request or code review since its includes the code formatting changes as well.
The option to solve this one is that every developer has to choose the same IDE or same formatting configurations and use them accross IDEs. This may not be feasible if a devloper chooses to use IDEs like VS Code,Atom etc…
In order to solve this one lets use a build tool plugin to format the code whenever there a change irrespective of IDEs style preferences. For maven build tool we have a plugin named formatter-maven-plugin which does this job.
See it in Action
Here is some code which is not even formatted (probably some good programmer written using text editor 😉)
public class StreamsDemonstration {
public static void main(String[] args) {System.out.println("IntStream of 1 - 10");
IntStream.rangeClosed(1, 10).forEach(System.out::println);
System.out.println("LongStream of 1 - 10");
LongStream.rangeClosed(1,10).forEach(System.out::println);
}
}
Here is some code that is formatted with different style than the the team is recommended (team wants to use google java code formatting).
public class StreamsDemonstration {
public static void main(String[] args) {
System.out.println("IntStream of 1 - 10");
IntStream.rangeClosed(1, 10).forEach(System.out::println);
System.out.println("LongStream of 1 - 10");
LongStream.rangeClosed(1,10).forEach(System.out::println);
}
}
Now lets the maven code formatting plugin to format the code according to google style. Here is the maven confguration for it.
<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
. See the below code which was formatted using the configured style.
public class StreamsDemonstration {
public static void main(String[] args) {
System.out.println("IntStream of 1 - 10");
IntStream.rangeClosed(1, 10).forEach(System.out::println);
System.out.println("LongStream of 1 - 10");
LongStream.rangeClosed(1, 10).forEach(System.out::println);
}
}
You can specify any format style you want.
And finally, you don’t want to miss the formatted code without being committed so lets use the maven scm plugin
to find uncommitted changes and fail the build if there are any and make sure to execute this plugin in compile
phase so you will find changes quickly.
<!-- Checks for any uncommitted changes and fail the build if there are any -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
<version>1.11.2</version>
<executions>
<execution>
<goals>
<goal>check-local-modification</goal>
</goals>
<phase>compile</phase>
</execution>
</executions>
</plugin>
Or if you prefer to do auto commit these changes while building without any build failures then use the goals scm:add
and scm:checkin
and run these goals only after install
phase to make sure all the formatted code has no issues.