Code Specification series----Checkstyle Manual

Source: Internet
Author: User
Tags checkstyle
Introduced
Checkstyle is a project under SourceForge that provides a tool to help Java developers comply with certain coding specifications. It automates the code-checking process and frees developers from this important, but tedious, task.
Checkstyle provides the main check content by default:
Javadoc Notes
• Naming conventions
• Title
Import Statement
• Volume size
• Blank
• modifiers
• block
• Code issues
• Class Design
• Mix check (live some useful, such as System.out and Printstacktrace)
As you can see from the above, Checkstyle provides most of the functionality for checking code specifications, without providing much of the ability to improve code quality and modify code, such as PMD and jalopy. However, it is powerful enough for team development, especially for companies that emphasize code specifications.
Eclipse plug-in installation and use
Step one: http://sourceforge.net/projects/checkclipse/download the Checkstyle Eclipse plug-in checkclipse. After downloading, put the package in Eclipse's Plugins folder, and then restart Eclipse. Find Checkclipse under the windows->preferences. The following figure:

Tick set Project Dir as Checkjstyle Basedir
Step Two: Right click on the project file you want to do Checkstyle, select "Properties". The following figure:

Tick the Enable Checkstyle and set Project ClassLoader.
Then Checkstyle configuraion file to select the Checkstyle profile in the project. Here I put the configuration file in the project root directory, so click on the right "Browse" button, in the project root directory to select the file. Press the "OK" button.
The code for the entire project will be prompted for errors based on the principles set in the configuration file. The results are as follows:

The diagram shows that code that does not conform to code specifications will have an error prompt, and there is a hint.
MAVEN plug-in installation and use
First, modify the Pom.xml file to check the top level of the code base and configure the Checkstyle Maven plug-in in the build section to download and install the corresponding version of the plug-in (Maven will automatically download it from its mirrored library), as follows:
Java code <project> ... <build> <plugins> <plugin> <groupid>o Rg.apache.maven.plugins</groupid> <artifactId>maven-checkstyle-plugin</artifactId> & lt;version>2.3</version> </plugin> </plugins> </build> ... </project >
The latest version of Maven-checkstyle-plugin is 2.5, and the corresponding Checkstyle core version of the 5.0;maven-checkstyle-plugin 2.3 corresponds to the Checkstyle core version of 4.4. To view the Pom file for the plug-in, see the following, which is the version number of the corresponding checkstyle.
Java Code <dependency> <groupId>checkstyle</groupId> <artifactid>checkstyle</artifactid > <version>4.4</version> </dependency>
Next, copy the custom rule profile to the top level directory and refer to the configuration in the Checkstyle plug-in configuration in the reporting section.
Java code    <reporting>       <plugins>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactid>maven-checkstyle-plugin</artifactid >               <configuration>                  < configlocation>my_checks.xml</configlocation>                </configuration>            </plugin>       </plugins>   </reporting>   
You can also place the configuration file under a subfolder with a relative path in the configuration.
Java code    <reporting>       <plugins>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactid>maven-checkstyle-plugin</artifactid >               <configuration>                  < configlocation>build-tools/src/main/resources/xx/my_checks.xml</configlocation>                </configuration>            </plugin>       </plugins>    </reporting>  
If you use a plug-in's own rules file, you can configure it as follows. Maven-checkstyle-plugin Plug-ins with the rules of the sun_checks.xml, maven_checks.xml, etc., you can view the plug-in package.
Java code    <reporting>       <plugins>                   <plugin>                     <groupid >org.apache.maven.plugins</groupId>                     <artifactId>maven-checkstyle-plugin</artifactId>                      <configuration>                                < configlocation>config/maven_checks.xml</configlocation>                    </configuration>                     <version>2.3</version>                           </plugin>       </plugins>    </reporting>  
Add the JXR plug-in in the reporting section, generate Code reports, so click on the link in the Checkstyle report to see the wrong code directly.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.