I take pride in the cleanliness of my code. One of the major ways to ensure my code is in fact clean is to use a static code analysis tool. SonarQube (formerly known as Sonar) is definitely my go to tool for this. Below are the steps I follow to integrate with SonarQube.
In the maven pom.xml I add the sonar plugin, and properties it requires:
Then to run it:
Now if you connect your SonarQube server (http://localhost:9000) you see the results of your run. One thing notably missing is the test coverage results. In addition to analyzing code it has the ability to display test coverage. Ideally these coverage metrics, in addition to being aggregated can be broken down into unit vs. integration test coverage. Here is how I generally configure it:
Now to analyze your code including test coverage with sonar you need to run two commands: ```
Finally, refresh the page on the SonarQube server and check out the coverage test results in addition to the standard analysis!
Note: This example works well for a simple maven project, it would need to be tweaked for multi-module projects. My understanding is that main difference in a multi-module project is that you need to output to the same coverage report for the entire project, and not have a single report per module.
In addition SonarQube server integration described above, the Sonar team have two plugins for IntelliJ IDEA.
SonarQube Community Plugin This plugin connects to an existing SonarQube server and shows you outstanding issues from your local project. It is helpful when you are working with an existing server and working through and cleaning up some outstanding issues.
SonarLint This plugin does not require a SonarQube server, and instead does a local analysis of your current project. It is especially helpful when you do not have a SonarQube server to work with.