Maven and Sonar Integration
maven, code, quality, coverage, sonar, java ·How do you make sure that the code quality in your project is as high as it can be?
GuestBook - The Beginning
I guess as many software developers, the roots of my career started with some random exploration of HTML on MS Frontpage for my homepage that a 13-year-old me decided should be interesting to someone for some reason. After fiddling around and setting up a few landing pages on a few different topics, I decided that it's not very interacive. And that's when my quest to create a guestbook started. Like now I remember loading altavista search trying to find some way of how to enable my guests to leave something in my guestbook.
And that's how the rabbit hole sucked me in - first were the tries with Perl and cgi-bin, which worked but it seemed so unnatural. The file storage with delimiters did not somhow impress me as well. Then I found it - the holy grail of guestbooks - PHP. Finally, I was equipped to deliver the guestbook of my life.
A few years into the rabbit hole, I was coding my own forums, galleries, comment sections and poll questionnaires. It seemed that there is nothing that PHP can't do. I felt like a super hero!
Clean Code
My initial obsession with code coverage and code quality. So coming from the background of PHP initially, some small ridiculous tasks of JAVA in the University, I was never aware of what kind of beast it was. My professional carreer brought me to this established company with a more mature Agile process, JUnit Testing, CI and SVN (which later was migrated to Mercurial (Hg)). I felt like I moved to this big pond from a cup! Of course on my first commit I broke the build :)
And then one of the more senior colleagues showed me this book - Clean Code by Robert C. Martin - it changed the way I see things. I was disgusted by the code I wrote a few days ago. Soon after that enlightening moment I was introduced to Sonar code quality measuring system and then it all fell into places. It's possible to write clean, beatiful and quality code, control it with the help of various devops tools and make sure the project doesn't compromise on quality no matter the size.
Prerequisites
So to run SONAR locally you'll just need to download the newest version of it and start the server, if you're on bash:
Note: By default SONAR runs on in-memory H2 database, but if you'd like to run SONAR on some remote server and use in CI then it is recommended to configure and use a regular database like MySQL.
Once the server is started we can start looking at the project itself.
Other things you are going to need is installed Java, maven and a preferred IDE.
Configuring Maven
First of all maven has to be configured to to be able to use sonar targets. In the local maven repository file settings.xml should be configured and look like this:
There might be some additional things needed to configure if sonar is not running on default settings on your local or if external sonar server is used.
Example Sonar-Maven Project
Let's start by creating a quick Spring Boot project. The easieast way to do it is using the really nice spring boot initializer at start.spring.io - just chose if you wanna use maven of gradle, group and artifact names and the dependencies you want to use and the project can be downloaded. It literally takes seconds. Import it to your preferred IDE and you're ready to go.
Now the first thing you'll need is to add a couple of plugins:
- surefire - a plugin that generates standard test reports in XML which then can be read by multiple software (for example Jenkins or SONAR to analyze test results)
- jacoco - a plugin that measures how much code was covered while running certain targets.
That is basically everything that is needed to have sonar configured on the project and measuring unit test coverage.
Now to test the configuration just added some service returning hello world:
And a quick test for it:
Running Tests and Sonar Targets
To run the tests and sonar analysis, in the root folder run:
And if everything is fine, you should be able to see those lines in the end of a successful build:
View Project on Sonar
Now once the Example Project is analyzed, you can view it on Sonar Web App.
As we can see the statistics of the project looks good: 74 lines in total, 66 XML lines, 8 Java line and 100% test coverage.
Final Words
So to conclude, Sonar can be very useful tool for everyone - especially people working at modern fast paced agile teams. Especially when it is combined with Jenkins and is included in the CI cycle. As numbers and diagrams geek I really enjoy nice and simple way that Sonar visualises the measurements of code quality.
Hope that you guys enjoyed this post and waiting for your feedback on the comments.
Further improvements
A few ideas that could help improving the quality of a project further:
- Introduce Git-Flow into your development process
- Use Pull Requests for feature delivery
- SONAR and Jenkins integration for Pull Requests (run the build, tests and sonar on every feature branch and don't allow to merge it anything fails (it's important to define the thresholds on jenkins to fail the build))