Recently I gave a small seminar for my current client about Clean Code and Craftsmanship. The group I was talking to
consisted of developers of all levels from junior to senior.
To my complete consternation, when I started talking about tools like PMD / CheckStyle and SonarQube, I found out that
none of them had ever heard of these tools. Not even the Senior developers.
This article will give a short explanation about what SonarQube is.
It is also a quick guide on how to start working with it.
This will be done with the use of docker because I want to :-)
This is what SonarQube has to say about it.
SonarQube provides the capability to not only show health of an application but also to highlight issues newly
introduced. With a Quality Gate in place, you can fix the leak and therefore improve code quality systematically.
SonarQube will analyse your code by analysing it statically. Without knowing the inner workings of your application it will
“look” at your code and search for code constructions that are known to be fragile or just wrong. After the scan
it will generate a report in the form of a browseable website. All issues are explained and you are probably also provided
a possible solution for it.
Noncompliant code: “for” loop increment clauses should modify the loops’ counters
Lots of these language constructions can be tested statically and at the point of this writing there are 423 checks done.
With docker it is very easy to start your own SonarQube service:
The command provided above will erase all history when stopped, but for learning purposes and demo’s that is fine.
Farther below I will give some more permanent solutions for local use and some hints for enterprise use.
now go to your maven project and run:
If maven the tells you that the build was successful, you can go look on localhost:9000 and
be amazed at how good or bad your code is.
If you are willing to take the time to really look into the found issues (yes even the ‘info’ ones) and willing to
go and fix these issues, you are on the way to becoming a better developer.
The beauty of using a tool like Sonar is that it will also keep history when configured correctly and therefore provide
you with a way of monitoring improvement. If you see improvements and also demonstrate this during sprint reviews, you
will start to notice a marked positive effect on your team. Team members will be more proud of the code they are writing
and become better as a group. Reviews will start to take code quality into account.
When you have a level of quality reached where you are comfortable you don’t want to loose it. This is the moment you
can introduce the use of a quality gate. This is a level of quality you can define for your project and if the code does
not meet the requirements set by the Quality Gate the build will fail. So that is the moment that Code quality might
fail the build. A very powerful thing and one that raises the maturity level of the team significantly.
If you are done with the above provided commands and you want a more permanent solution for your local projects but don’t
want to have to create a complete pipeline just for your hobby projects…
We need a database image and the sonar image.
docker-compose.yml file containing the two images and their connections
This image creates volumes for all stuff needed to be saved.
Lets start it up.
You can leave of the
-d if you want to see what’s happening. It will start in the foreground and not in ‘detached’ mode.
Now if you stop the containers it will preserve the state of your database in the volumes defined.
To stop the containers go back to your
docker-compose.yml file and do:
And if you want to loose all your data (volumes) too:
If you use tools like Jenkins to control your pipeline it is very useful to add SonarQube as part of the pipeline setup.
If you do not have control over the pipeline environment, you should ask the Ops guys to install Sonar for you. If you do have
control over the pipeline, it is a very good idea to make it part of it. It will make code quality something as part of your
daily life. Jenkins has good integration for tools like sonar and configuring it is not the obstacle it might seem.
You don’t have to do anything to have sonar working for maven. It is one of the default plugins (it is that important yes!) and always available
for all projects. You just have to tell it where sonar ‘lives’ and this can be done through the command line (see above).
If you want to just be able to do
mvn sonar:sonar you can tell maven where sonar lives by editing the
by adding the following piece of code to it:
some lines have been commented out. This is because in the form I provided above (docker-compose.yml) you don’t need to tell maven
where the database is because it already knows. If you choose to install a database native on your machine and sonar to (without docker)
you can enable these lines and adjust them to conform to your needed settings.
Not doing these kinds of code checks as a developer is robbing you of a learning experience and the opportunity of an extra
review. The static code checker does not get tired or is under pressure. It will just check your code and help you become
better and your code too.
So SonarQube … Just do it!