(Transfer) Coding Specification Series (2): Eclipse Checkstyle Configuration

Original address: http://chenzhou123520.iteye.com/blog/1627618

The last article introduced " Coding Specifications Series (1): Eclipse Code Templates Settings ", which mainly introduces the configuration of Checkstyle in Eclipse. Regarding Checkstyle, the introduction from Baidu Encyclopedia is as follows: CheckStyle is a project under SourceForge, which provides a tool to help JAVA developers comply with certain coding standards. It automates the code specification checking process, thereby freeing developers from this important but tedious task. The main contents of CheckStyle inspection include: Javadoc comments, naming conventions, headings, Import statements, size, whitespace, modifiers, blocks, code problems, class design, mixed checks.

If we want to use Checkstyle, we must first install the Checkstyle plug-in in Eclipse. The installation tutorials about the plug-in are all over the Internet, so I will skip it here. The following mainly talks about the configuration of Checkstyle. Specific steps are as follows:

1. Open Windows-->Preferences-->Checkstyle in Eclipse, as shown below:

Checkstyle configuration

Through the interface, we can find that the Checkstyle plug-in has two built-in configuration files for code inspection by default: Sun Checks and Sun Checks (Eclipse). These two configuration files define the options that Checkstyle needs to check. Generally, the project team will customize a configuration file according to the project requirements, and then import it into Eclipse by means of import. Today, we mainly introduce this method.

2. Click New, select External Configuration File in the Type drop-down box in the pop-up interface, and then introduce our custom configuration check xml file, Name can be customized, as shown in the following figure:

Checkstyle configuration

Click OK to save, and then we can see the configuration we just imported in the previous interface. It is chenzhou_checkstyle in the picture below

We can select the row with the mouse, and then click the Set as Default button on the right, so that Checkstyle has been configured.

 

After configuring Checkstyle, we also need to activate Checkstyle inspection in the project separately, the specific method is:

Right-click on the project-->Properties-->Checkstyle, select the checkbox in front of Checkstyle active for this project in the pop-up interface, and select our custom configuration chenzhou_checkstyle in the Configure column, as shown in the following figure

After clicking OK to save the project will rebuild, so Checkstyle checks have been activated.

We can open our code, if there is any place in the code that does not conform to the specification defined by Checkstyle, Eclipse will highlight it in yellow, as shown in the following figure:

If we hover over the highlighted code, Eclipse will give a warning message.

For the detailed configuration of Checkstyle check options, we can modify the configuration in Eclipse, as shown below, select chenzhou_checkstyle

Click the Configure button on the right to pop up the following interface

In the figure above, the menu items on the left represent options that we can configure, and each menu item can be expanded into multiple sub-menu items. We can configure it separately. The menu item marked with a green checkmark indicates that the item and all sub-items have been added to the check item. If the child items are not all added, the parent menu item will not show a green checkmark.

 

If we need to add a configuration item, we can click to select a menu item, and then click the Add button below. After selecting OK, the sub-menu item of this item will be displayed in the right area. We can check the check box before the menu item. box to configure specific items. As shown below

For the meaning of each specific item, I will not go into details one by one, and everyone should be able to understand!

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327037093&siteId=291194637