Check style & code style 强制你遵循编码规范 为什么:统一代码规范的必要性 怎么做 具体操作步骤 使用说明 相关官方说明文档

为什么:统一代码规范的必要性

不同的小组\同学,采用不同的代码格式规范,导致每次 format 代码,都有大量的变化,review 代码时,引入很多干扰项。

目标:统一代码格式规范,保证 format 代码时,不会引入格式上的干扰,提升小组协作效率、代码 review 效率。

怎么做

明确规范:明确「统一」的代码规范

使用规范:借助工具,自动化格式代码 + 自动校验代码

具体操作步骤

clone code style 到本地工作目录

git clone [email protected]:adata/code-style.git

配置code style

在弹出的文件对话框中选择刚刚clone下来的google code style文件"intellij-java-google-style.xml"

一顿"OK"即可

配置CheckStyle插件

将下面的内容配置到项目的pom的plugins配置节中

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>3.1.2</version>
    <dependencies>
        <dependency>
            <groupId>com.puppycrawl.tools</groupId>
            <artifactId>checkstyle</artifactId>
            <version>8.44</version>
        </dependency>
    </dependencies>
    <configuration>
        <configLocation>google_checks.xml</configLocation>
        <!--跳过检查的文件-->
        <suppressionsLocation>../style/check_style_suppressions.xml</suppressionsLocation>
        <encoding>UTF-8</encoding>
        <consoleOutput>true</consoleOutput>
        <failsOnError>true</failsOnError>
        <includeTestSourceDirectory>false</includeTestSourceDirectory>
    </configuration>
    <executions>
        <execution>
            <id>checkstyle</id>
            <phase>validate</phase>
            <goals>
                <goal>check</goal>
            </goals>
        </execution>
    </executions>
</plugin>

跳过对指定文件的某些检查

建立一个checkstyle-suppressions.xml文件。其中加入下述内容

<!DOCTYPE suppressions PUBLIC
    "-//Puppy Crawl//DTD Suppressions 1.0//EN"
    "http://www.sparrowzoo.com/dtds/suppressions_1_0.dtd">

<suppressions>
    <suppress checks="RegexpSingleline"
              files="SparrowLoggerImpl.java"
              lines="400-410"/>
</suppressions>

使用说明

格式化

Eclipse 快捷键 CTRL+SHIFT+F

IDEA 快捷键 CTRL + ALT + L

养成好习惯 格式化后 CTRL+S保存

代码格式检查

默认进行代码检查

mvn clean install -Dmaven.test.skip

可以用 checkstyle.skip 选项跳过代码检查

mvn clean install -Dcheckstyle.skip -Dmaven.test.skip

相关官方说明文档

checkstyle github

https://github.com/checkstyle/checkstyle.git

google code style

https://raw.githubusercontent.com/google/styleguide/gh-pages/intellij-java-google-style.xml

style plugin

https://maven.apache.org/plugins/maven-checkstyle-plugin/index.html

https://maven.apache.org/plugins/maven-checkstyle-plugin/usage.html

Specifies the location of the XML configuration to use.

Potential values are a filesystem path, a URL, or a classpath resource. This parameter expects that the contents of the location conform to the xml format (Checkstyle Checker module) configuration of rulesets.

This parameter is resolved as resource, URL, then file. If successfully resolved, the contents of the configuration is copied into the ${project.build.directory}/checkstyle-configuration.xml file before being passed to Checkstyle as a configuration.

There are 2 predefined rulesets.

  • sun_checks.xml: Sun Checks.
  • google_checks.xml: Google Checks.

内置的style

https://github.com/checkstyle/checkstyle/tree/master/src/main/resources

upgrade check style

https://maven.apache.org/plugins/maven-checkstyle-plugin/examples/upgrading-checkstyle.html

定制版 code style ningg

https://checkstyle.sourceforge.io/google_style.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章