2016-03-22 by Cameron Ke
To do a good job, an artisan needs the best tools. 工欲善其事，必先利其器。
In an agile software development, there are several best practices to ensure the code quality. Such as, continuous quality inspection, build automation, continuous integration, and so forth. Today, I would like to introduce a few tools which can support Java workshop on these best practices.
Continuous quality inspection
As a developer, it is always very good that someone can mentor you when coding. Or, some tools. On the developers’ desktop, the modern IDEs can help developers inspecting the code on-the-fly, for instance Eclipse, Netbeans, and IntelliJ IDEA (Community Edition is free). On top of that, you can install several plugins for further inspection, i.e.:
- Checkstyle plugin: code style inspection, scans source code, supports Oracle code conventions, Google Java Style, and highly configurable, can be customized to your own coding standard.
- Findbugs plugin: static code analysis, scans byte code, finds common programming flaws.
- PMD plugin: static code analysis, scan source code, finds common programming flaws.
- SonarQube plugin: displays flaws found in a central SonarQube server. (I will introduce SonarQube later on.)
(You can find the above plugins for all three IDEs.)
It is important that everyone in a team should use the same rule-sets, so that the code quality is consistent. It is also good to keep in mind that run a final check before submitting it to the source code repository, so called pre-check-in.
On the server side, SonarQube can be used as a quality control center. It is a web-based platform, the rules, alerts, thresholds, exclusions, settings can be configured online. And it can be integrated with the modern continuous integration engines, or build tools. Besides the coding rules inspection, SonarQube evaluates a project in a higher angle, such as architecture and design, duplications, complexity, technical debt, SQALE rating, and so on. By leveraging its database, it not only allows to combine metrics altogether but also to mix them with historical measures, presenting them in run charts, pie charts, and etc.
By using these tools, you can lift up the code quality in several aspects, for example: avoiding the common pitfalls, finding potential bugs, eliminating dead code, improving code readability and maintainability, and detecting performance issues. Furthermore, these tools can actually “teach” you how to write beautiful code, clean code, and robust code.
Although you can build a Java application in IDEs, using a build tool is a better choice, especially in team development. Because using a IDE to build an application, it needs developers manually invoke the build process. This is not practical for team development, where it is very difficult to keep track of what needs to be built, in what sequence and what dependencies there are in the building process. Using a build tool allows the build process to be automated and more consistent. It also can be integrated with continuous integration Engine. In the Java world, there are three major build tools: Ant, Maven, Gradle. The pros and cons are listed in the following table:
More detailed comparison for those three tools, see:
Artifacts, the outputs of build processes, can be managed in Sonatype Nexus, which is an artifacts repository. The main benefits of using an artifacts repository are:
- versioning and centralizing binary artifacts
- fasten build speed through caching of dependencies
- exchanging binary artifacts efficiently among teams
Except Nexus, there are two other popular artifacts repositories: JFrog Artifactory and Apache Archiva, their functions are similar to Nexus.
More detailed comparison for those three tools, see:
“Continuous Integration (CI) is a development practice that requires developers to integrate code into a shared repository several times a day. Each check-in is then verified by an automated build, allowing teams to detect problems early. By integrating regularly, you can detect errors quickly, and locate them more easily.” Quote from Thoughtworks.
Jenkins is one of the best continuous integration engines. It is very easy to use and configure. With over 900 plugins, it is highly extendable and can integrate with many other tools. For example, the tools mentioned previously, i.e. Checkstyle, PMD, Findbugs, SonarQube, Maven, Gradle, and Ant can all be integrated into Jenkins, and triggered automatically by it.
On the other hand, if you use Git as source code repository, an even better solution would be to use GitLab and GitLab CI. The main benefits are twofold. Firstly, GitLab introduces code review mechanism. It means that each developer works on his/her own branch, so-called feature branch. After the developer finished the feature, he/she will submit a merge request for applying to merge this feature into the main track. Only if the reviewer reviewed and accepted the merge request, the feature then can be merged into the main track. Secondly, the continuous integration not only happens to the main track, but also builds and tests feature branches before accepting the merge requests.
By using the tools mentioned in this article, you can easily apply a few best practices of agile development into your Java projects. It can rise the code quality, increase the build and integration efficiency, and reduce the risk dramatically.