美文网首页
Maven插件方式执行CheckStyle并自定义风格文件

Maven插件方式执行CheckStyle并自定义风格文件

作者: 张云飞Vir | 来源:发表于2023-01-03 22:25 被阅读0次

    一、写在前面

    代码风格包含了变量的命名,缩进,注释等内容,在团队开发中,多人协同开发要避免各种风格混合带来的混乱,统一的代码风格是必须的。在开发过程中,我们可以使用一些工具来改进这一状况,比如 checkStyle 工具。

    二、开始

    Checkstyle 是一种用于检查 Java 源代码是否遵守代码标准或验证规则集(最佳实践)的工具。

    它由这些组成:

    • 执行程序:即 checkStyle 的执行程序,它可以再 IDEA 编辑器中启动,也可以在 Maven 插件方式启动,也可以二进制方式启动。
    • 代码风格描述规则:以 XML 方式描述的代码风格的规则,比如sun_checks.xml 这个文件。
    • 相关的配置文件:比如在使用Maven插件时,插件的配置文件需要配置。

    checkStyle 可以通过多种方式使用

    我个人更喜欢在 Maven 插件 的方式使用,代码编写完执行一次,需要更改地方会在执行结果中告诉我,然后再修正遇到的问题。

    我这里推荐 作者blinkfox
    修改的代码风格规则文件,作者很用心,根据google的风格进行了修改,比如缩进(谷歌是2个空格缩进,而我也习惯了4个空格缩进),同时编写了中文的风格说明,非常赞。
    更多请参考: blinkfox-checks.md

    Maven 插件方式使用CheckStyle

    (1) 下载 一个 规则文件

    在这里下载 一个 google_checks.xml 文件
    下载地址:https://github.com/blinkfox/java-style/tree/master/docs/styles

    (2) 配置Maven插件

        <properties>
            <checkstyle.skip>true</checkstyle.skip>
        </properties>
    
        <build>
            <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>9.3</version>
                        </dependency>
                    </dependencies>
                    <configuration>
                        <skip>${checkstyle.skip}</skip>
    <!--                    <configLocation>style/my_sun_checks.xml</configLocation>-->
                        <configLocation>style/blinkfox-checks.xml</configLocation>
                        <encoding>UTF-8</encoding>
                        <consoleOutput>true</consoleOutput>
                        <failsOnError>true</failsOnError>
                        <linkXRef>false</linkXRef>
                        <includeTestSourceDirectory>false</includeTestSourceDirectory>
                    </configuration>
                    <executions>
                        <execution>
                            <id>validate</id>
                            <phase>validate</phase>
                            <goals>
                                <goal>check</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    

    疑问:我这里添加了自定义属性 checkstyle.skip,为什么呢?
    解答:配置了checkstyle后,它会在 maven 构建的 验证阶段执行,如果失败,会导致编译失败。我这里不想让它默认执行,仅在 希望执行的时候执行,因此默认是不执行的,只有 ${checkstyle.skip} 是 true 的时候执行,通过 -Dcheckstyle.skip=false 进行传参。

    (3) 执行 CheckStyle

    mvn checkstyle:check -Dcheckstyle.skip=false
    # 或者指定子模块
    mvn checkstyle:check -Dcheckstyle.skip=false -pl pvdms-svc
    
    

    针对哪些文件哪些规则进行过滤的规则

    通过配置一个 checkstyle-suppressions.xml 过滤配置文件来指定针对哪些文件哪些规则进行过滤。

    示例:

    
    # 在pom里对应的配置项指定 check_style_suppressions.xml 的位置
    <suppressionsLocation>${session.executionRootDirectory}/1_doc/style/check_style_suppressions.xml</suppressionsLocation>
    
    check_style_suppressions.xml 的内容如下:
    # 注意设置的 项 是 正则表达式
    # 比如过滤 SparrowLoggerImpl.java 的 RegexpSingleline 规则
    # 比如过滤 MyBatisPlusGenerator.java 的 全部规则
    
    <suppressions>
        <suppress checks="RegexpSingleline"
                  files="SparrowLoggerImpl.java"
                  lines="400-410"/>
    
        <suppress checks="[a-zA-Z]"
                  files="MyBatisPlusGenerator.java"
                  lines="1-1000"/>
    
    </suppressions>
    

    参考:
    https://maven.apache.org/plugins-archives/maven-checkstyle-plugin-3.0.0/examples/suppressions-filter.html

    三、扩展

    3.1 跳过代码检查

    mvn install 时可以跳过代码检查,使用 用 checkstyle.skip 来做:

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

    3.2 CheckStyle 配置详解

    请参考:
    https://maven.apache.org/plugins/maven-checkstyle-plugin/checkstyle-mojo.html

    https://www.pudn.com/news/6306d065f8728f1f6c829ae9.html

    四、参考

    Blinkfox Java 编程风格指南,且包含 CheckStyle 和 IDEA 格式化文件,作者很用心
    https://github.com/blinkfox/java-style

    https://juejin.cn/post/7178683426852044858?share_token=8408902a-f460-4642-b312-e7e661a9878d

    谷歌开发指南,英文:
    https://google.github.io/styleguide/javaguide.html

    https://juejin.cn/post/7178683426852044858?share_token=8408902a-f460-4642-b312-e7e661a9878d

    下载google_checks.xml 文件
    https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml

    Google 开源项目风格指南——中文版
    https://zh-google-styleguide.readthedocs.io/en/latest/google-javascript-styleguide/javascript_language_rules/#var

    https://zh-google-styleguide.readthedocs.io/en/latest/google-javascript-styleguide/javascript_language_rules/#var

    相关文章

      网友评论

          本文标题:Maven插件方式执行CheckStyle并自定义风格文件

          本文链接:https://www.haomeiwen.com/subject/mlahcdtx.html