CheckStyle使用简要说明
2021-07-09 by uino 173 编码规范

1.eclipse安装CheckStyle插件

  • Eclispe菜单:Help->Eclispe Marketplace

checkstyle1-1.png

checkstyle1-2.png

  • 安装成功,重启Eclipse后确认: 菜单-Windows->Preferences->Checkstyle,出现CheckStyle说明安装成功。

checkstyle1-3.png

2.把checkstyle.xml检查规则文件放到maven java项目根目录下

checkstyle2-1.png

3.java项目中启用CheckStyle检查

  • Java项目(鼠标右键)->Properties->Checkstyle导入项目根目录下的checkstyle文件,注意选项目相对配置。

checkstyle3-1.png

checkstyle3-2.png

  • 为本项目启动checkstyle检查

checkstyle3-3.png

或者

checkstyle3-4.png

4.规范违反提示(Eclipse)

checkstyle4-1.png

5.Maven打包过程中用CheckStyle检查代码规范相关maven xml相关配置

以下示例配置再maven install和mvn checkstyle:checkstyle会执行代码规范检查

<build>
    <pluginManagement>
        <plugins>
            <!--compiler在maven声明周期内置,所以后面不用声明也可使用-->
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <!--公共checkstyle标准配置,可以在子模块中覆盖,修改自定义选项-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <configLocation>/checkstyle.xml</configLocation>
                    <consoleOutput>true</consoleOutput>
                    <encoding>UTF-8</encoding>
                    <consoleOutput>true</consoleOutput>
                    <failsOnError>false</failsOnError>
                    <linkXRef>false</linkXRef>
                    <skip>false</skip>
                    <violationSeverity>error</violationSeverity>
                </configuration>
                <executions>
                    <execution>
                    <id>install</id>
                    <phase>install</phase>
                    <goals>
                        <goal>checkstyle</goal>
                    </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </pluginManagement>

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version> 
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
        </plugin>
    </plugins>
</build>  

<reporting>
    <!--所有子模块都要执行的报告-->
    <plugins>
        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
        </plugin>
    </plugins>
</reporting>

6.CheckStyle代码检查规则文件下载

checkstyle.xml