一键检查代码规范
你好呀,我的老朋友!我是老寇,欢迎来到老寇云平台!
话不多说,讲一讲一键检查代码规范!
# 引入插件
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<!-- 配置文件路径 -->
<configLocation>checkstyle/checkstyle.xml</configLocation>
<!-- true表示在命令行或控制台输出检查结果,否则结果不会直接在控制台显示 -->
<consoleOutput>true</consoleOutput>
<!-- true表示检查到错误,则整个构建过程失败 -->
<failsOnError>true</failsOnError>
<!-- 是否直接构造失败,true只要任何一个代码规范不通过,从而直接构建失败,false代码检查不通过,不会导致构建失败,但会报告违规情况 -->
<failOnViolation>true</failOnViolation>
<!-- 是否包含测试代码目录,true包含,false不包含,只检查主目录代码 -->
<includeTestSourceDirectory>true</includeTestSourceDirectory>
<!-- 是否跳过代码规范检查,true跳过,false不跳过 -->
<skip>false</skip>
<!-- checkstyle报告是否包含代码引用链接,false不包含,true包含 -->
<linkXRef>false</linkXRef>
</configuration>
<executions>
<!-- 执行mvn install启动代码规范扫描 -->
<execution>
<id>install</id>
<phase>install</phase>
<goals>
<goal>checkstyle</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
# checkstyle使用
checkstyle官方地址 (opens new window)
创建checkstyle.xml
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"https://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
<!-- https://checkstyle.sourceforge.io/property_types.html -->
<!-- 字符集 -->
<property name="charset" value="UTF-8"/>
<!-- 检查级别 -->
<property name="severity" value="error"/>
<!-- 抑制过滤器 https://checkstyle.sourceforge.io/filters/suppressionfilter.html#SuppressionFilter -->
<module name="SuppressionFilter">
<property name="file" value="checkstyle/checkstyle-suppressions.xml"/>
<property name="optional" value="false"/>
</module>
<!-- 检查协议头 https://checkstyle.sourceforge.io/checks/header/regexpheader.html -->
<module name="RegexpHeader">
<property name="headerFile" value="checkstyle/checkstyle-header.txt"/>
<property name="fileExtensions" value="java"/>
</module>
<module name="TreeWalker">
<!-- 匹配句子末尾的格式 https://checkstyle.sourceforge.io/checks/javadoc/javadocstyle.html#JavadocStyle -->
<module name="JavadocStyleCheck">
<property name="endOfSentenceFormat" value="([.?!。?!][ \t\n\r\f<])|([.?!。?!]$)"/>
</module>
<!-- 排除导入的未使用的依赖 https://checkstyle.sourceforge.io/checks/imports/unusedimports.html -->
<module name="UnusedImports">
<!-- true忽略文档依赖,false不忽略文档依赖 -->
<property name="processJavadoc" value="true"/>
</module>
</module>
</module>
注意:协议头验证,需要创建checkstyle-header.txt
^\Q/*\E$
^\Q * Copyright (c) \E20\d\d\-20\d\d\Q KCloud-Platform-IoT Author or Authors. All Rights Reserved.\E$
^\Q *\E$
^\Q * Licensed under the Apache License, Version 2.0 (the "License");\E$
^\Q * you may not use this file except in compliance with the License.\E$
^\Q * You may obtain a copy of the License at\E$
^\Q *\E$
^\Q * http://www.apache.org/licenses/LICENSE-2.0\E$
^\Q *\E$
^\Q * Unless required by applicable law or agreed to in writing, software\E$
^\Q * distributed under the License is distributed on an "AS IS" BASIS,\E$
^\Q * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\E$
^\Q * See the License for the specific language governing permissions and\E$
^\Q * limitations under the License.\E$
^\Q *\E$
^\Q */\E$
^$
^.*$
注意:checkstyle-suppressions.xml可以用来排除不扫描的文件,比如排除mapstruct生成的代码
<!DOCTYPE suppressions PUBLIC
"-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
"https://checkstyle.org/dtds/suppressions_1_2.dtd">
<suppressions>
<!-- https://checkstyle.sourceforge.io/filters/suppressionfilter.html -->
<suppress files="[\\/]org[\\/]laokou[\\/]admin[\\/]convertor[\\/].*ConvertorImpl.java" checks=".*"/>
<suppress files="[\\/]org[\\/]laokou[\\/]auth[\\/]convertor[\\/].*ConvertorImpl.java" checks=".*"/>
</suppressions>
# 使用
mvn clean install -P test
我是老寇,我们下次再见啦!
上次更新: 11/22/2024, 4:10:36 AM