Maven項目使用Checkstyle檢查代碼

Maven項目使用Checkstyle檢查代碼

Checkstyle可以做到自定義風格的代碼檢查,這裏提供一些使用的例子供參考。

idea中配置checkstyle-IDEA插件

使用checkstyle-IDEA插件,可以直接依靠idea檢查代碼,優點是有圖形界面,操作直觀,安裝好之後所有項目共用,缺點是需要手動執行檢查。
按快捷鍵 Ctrl+Alt+S ,選擇Plugins ,在marketplace輸入checkStyle-IDEA搜索安裝。
在這裏插入圖片描述
如果搜索不到,也可以打開插件官網https://plugins.jetbrains.com/搜索checkStyle-IDEA,點擊安裝後,回到idea會自動下載安裝。
在這裏插入圖片描述
在這裏插入圖片描述
安裝好之後,按快捷鍵Ctrl+Alt+S,選擇Checkstyle項進行配置,如圖(檢查規則文件可以參考文末我自定義的)
在這裏插入圖片描述
在左下角有Checkstyle的選項卡,選擇規則文件運行即可。
在這裏插入圖片描述

在Maven項目中配置使用Checkstyle

由於項目是Maven項目,希望在編譯的時候自動執行檢查,不需要額外手動執行,可以選擇在pom.xml配置maven-checkstyle-plugin插件,綁定到Maven的生命週期,這樣在執行mvn compile等命令時自動觸發執行檢查。

單模塊的maven項目

單模塊的maven項目只需要配置plugins即可, pom.xml配置如下:

<build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-checkstyle-plugin</artifactId>
                    <version>3.1.1</version>
                    <dependencies>
                        <!--指定依賴的checkstyle版本-->
                        <dependency>
                            <groupId>com.puppycrawl.tools</groupId>
                            <artifactId>checkstyle</artifactId>
                            <version>8.33</version>
                        </dependency>
                    </dependencies>
                    <!--指定配置文件-->
                    <configuration>
                        <configLocation>checkstyle.xml</configLocation>
                        <encoding>UTF-8</encoding>
                        <consoleOutput>true</consoleOutput>
                        <failsOnError>true</failsOnError>
                        <linkXRef>false</linkXRef>
                    </configuration>
                    <executions>
                        <execution>
                            <id>validate</id>
                            <phase>validate</phase>
                            <goals>
                                <goal>check</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.9.0</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

多模塊的maven項目

多模塊的maven項目,參考官方配置(https://maven.apache.org/plugins/maven-checkstyle-plugin/examples/multi-module-config.html),只需要在父模塊的pom.xml裏面配置插件即可,pom.xml配置如下

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
        <version>3.1.1</version>
        <dependencies>
          <dependency>
            <groupId>com.example.whizbang</groupId>
            <artifactId>build-tools</artifactId>
            <version>1.0</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>

Jenkins中配置

Jenkins中使用Checkstyle,要先配置好maven的checkstyle(上面的步驟),然後編輯Jenkins的構建配置。
1.在Build的Goals and options中添加checkstyle:checkstyle,
2.再勾選構建配置的[Deprecated] Publish Checkstyle analysis results選項。
3.保存,重新構建項目,如果右邊沒有Checkstyle Trend顯示,則重啓一下Jenkins。
在這裏插入圖片描述
在這裏插入圖片描述

異常

com.puppycrawl.tools.checkstyle.api.CheckstyleException: 
cannot initialize module TreeWalker 
- TreeWalker is not allowed as a parent of LineLength Please review 
 'Parent Module' section for this Check in web documentation if Check is standard.

查看官方文檔,LineLength 這個屬性需要放置在Checker裏面的,但是網上很多示例是放到TreeWalker裏面了。

Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:3.1.1:check
 (validate) on project junitdemo: Failed during checkstyle configuration:
  cannot initialize module TreeWalker - Token "INTERFACE_DEF" was not found 
  in Acceptable tokens list in check com.puppycrawl.tools.checkstyle.checks.blocks.RightCurlyCheck

在官方倉庫中的google_checks.xml,使用了INTERFACE_DEF新增加的token,通過查看GitHub的提交記錄可以看到,RightCurlyCheck在8.33的版本中還沒有合併代碼支持INTERFACE_DEF,所以直接去掉就可以了。

參考

官方倉庫:https://github.com/checkstyle/checkstyle
checks列表:https://checkstyle.org/checks.html
Maven插件:https://maven.apache.org/plugins/maven-checkstyle-plugin/

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