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

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