Javarevisited
Published in

Javarevisited

Maven Plugins for Clean Code

Code Review:

Code Review is a practice that is used nowadays in almost every software development company. It allows you to detect errors at an earlier stage of the CI / CD cycle and reduce the risk of their later occurrence. It is also a great opportunity to exchange knowledge between programmers — both knowledge about good coding practices and the domain knowledge of a given product.

As code review is important but finding format change or finding variables not being used is very much overwhelming for reviewers. It is difficult to catch these things by the human eye so why not automate it with the help of some tools.

Useful Maven plugins

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 a grouping of imports and remove unused imports.

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

Integrate plugin in the CI/CD

<profile>
<id>validate-format</id>
<activation>
<property>
<name>{pass property specific to CI/CD}</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>net.revelc.code</groupId>
<artifactId>impsort-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
</profile>

So when this plugin will be used in IDE the goal is sort but when used in CI/CD goal is check because we don’t want to sort in CI/CD rather we want to validate it and fail the build in case of validation fails.

More info: https://code.revelc.net/impsort-maven-plugin/

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.

We have java coding standards defined in our company that’s why added it as dependency, if you have for your company do add those standards.

<plugin>
<groupId>net.revelc.code.formatter</groupId>
<artifactId>formatter-maven-plugin</artifactId>
<version>2.0.1</version>
<executions>
<execution>
<goals>
<goal>format</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.test.standards</groupId>
<artifactId>java-coding-standards</artifactId>
</dependency>
</dependencies>
<configuration>
<configFile>eclipse-neon/eclipse-formatter.xml</configFile>
<lineEnding>KEEP</lineEnding>
<compilerSource>${java.version}</compilerSource>
<compilerCompliance>${java.version}</compilerCompliance>
<compilerTargetPlatform>${java.version}</compilerTargetPlatform>
</configuration>
</plugin>
</plugins>

Integrate this with CI/CD

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

So when this plugin will be used in IDE the goal is format but when used in CI/CD goal is validate because we don’t want to sort in CI/CD rather we want to validate it.

More info: https://code.revelc.net/formatter-maven-plugin/

3. maven pmd plugin

PMD is a source code analyzer to find common programming flaws like unused variables, empty catch blocks, unnecessary object creation, and so forth.

<plugin>
<artifactId>maven-pmd-plugin</artifactId>
<configuration>
<excludeRoots>
<excludeRoot>target/generated-sources</excludeRoot>
<excludeRoot>target/generated-test-sources</excludeRoot>
</excludeRoots>
<linkXRef>true</linkXRef>
</configuration>
</plugin>

More info: https://maven.apache.org/plugins/maven-pmd-plugin/

4. Spot Bugs Maven Plugin

SpotBugs looks for bugs in Java programs. It is based on the concept of bug patterns. A bug pattern is a code idiom that is often an error. Bug patterns arise for a variety of reasons:

  • Difficult language features
  • Misunderstood API methods
  • Misunderstood invariants when code is modified during maintenance
  • Garden variety mistakes: typos, use of the wrong boolean operator

SpotBugs uses static analysis to inspect Java bytecode for occurrences of bug patterns.

<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<version>4.2.0</version>
<executions>
<execution>
<id>check</id>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>

Earlier findBugs was the plugin but it is now deprecated. This plugin is very useful to encourage developers to use best practices.

More info: https://spotbugs.github.io/spotbugs-maven-plugin/index.html

Action for developers

Please run mvn clean install in the terminal before raising a PR so that all the plugins are executed and your CI/CD job will not fail because of these issues.

Please comment any plugin/tool which I missed here to add.

--

--

--

A humble place to learn Java and Programming better.

Recommended from Medium

iOS Restrictions You May Encounter During Development Process

Transitioning from Software Testing to Development

What does a Rustc Reading Club do?

“Planet Hopping” or “Galactic Express”

Transform a traditional company to a Remote-Company

Gutenberg Clone Content Guide

Test on Samsung Internet for free with BrowserStack

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Archit Agarwal

Archit Agarwal

SDE-3 at PayPal

More from Medium

Practical Guide to JUnit 5 Parameterized Tests

How to Use a Custom Class As @PathVariable in Feign 👨‍🏫

Inversion of Control (IoC)

Unit Tests using JUnit and Mockito

Inside a machine