SCM-Manager Plugins Part 6: Checkstyle
We published a new SCM-Manager plugin: scm-checkstyle. It helps you to improve your software quality in Java projects by checking your code against code conventions in a very early stage of software development where adaptations are only little time-consuming. The plugin requires SCM-Manager version 1.33 or higher.
Checkstyle in General
Checkstyle is a tool that helps Java code programmers to adhere to coding standards. These coding standards can be defined individually. The tool checks the written code against those standards and shows the programmer where he didn´t stick to them. Checkstyle can check your code for several aspects:
- layout issues
- class design problems
- duplicate code
- bug patterns
There are several ways you can use Checkstyle:
- from the command line
- as part of an Ant build
- plugin (e.g. for Eclipse, NetBeans, Maven, …)
SCM-Manager Checkstyle Plugin
Now there is a new way of using Checkstyle: the scm-checkstyle-plugin. The plugin checks your code every time you commit changes to the main repository. There are two possibilities:
- Your code passes the check, because there are no violations against the defined rules.
- Your commit is refused, because it contains violations against the Checkstyle rules.
This harsh behavior in case of violations leads to the realization of the rules and therefore to a higher coding standard.
There are two different ways of using the plugin: with a global configuration (one config-file for all repositories) or repository specific configuration files. As configuration file you need a .xml.
You can also activate or deactivate the plugin by selecting “Do not check” at “Reaction on Push”. The plugin is active if you select “Check and block”.
At the ”Config → General“ screen it is possible to enable and disable the repository specific configuration. Whenever you change this option you have to restart the web server, otherwise the change is not active. The repository specific configuration can be accessed via the “Repositories” screen. If the box of this option is checked the configuration tab of the repository isn´t shown. At the general configuration screen you can also test your config-file. Simply fill in the path to a .xml-config-file that you want to test (“Config-File for testing”) and a .java-file that you want use for testing (“Path of files to test”). After clicking “Checkstyle Test” the test will run and look like this:
As mentioned before you need a configuration file that contains the rules Checkstyle should use. You can write the file from the scratch, or use an existing file. There is for example a preconfigured file: Sun_Checks.xml (Link).
If you are using e.g. Eclipse you can use the “Checkstyle configurator” from the eclipse-cs plugin. It contains the Sun Checks set of rules and it is possible to create your own set by selection from the given modules and configuring them:
Existing rule sets can be exported to .xml-files.
Using the Plugin
If the plugin is activated and you provided the path to your configuration file your code is being checked every time a commit to the repository is done. If there are no violations of configured rules your changes will be committed as usual, otherwise the push will not be executed and you will receive an error message. In case you use a Git or SVN repository the error message contains detailed information about the sources of the push rejection. This looks like this:
Now it´s up to you to write the code according to your rules ;)
The scm-checkstyle-plugin helps you to implement quality management in a very easy way by enforcing you to stick to your own coding rules. The advantage is that the rules are defined for the repositories, therefore each developer has to stick to them if he wants to push his changes.
With best regards, your SCM-Manager Support Team