Checkstyle整合pre-commit实现代码规范检测

项目配置

对于Spring Boot项目使用Maven作为构建工具,采用类似的方式配置Git的pre-commit钩子来运行Checkstyle。以下是一个基本的步骤:

  1. 配置Checkstyle插件:
    在Maven的pom.xml文件中,添加Checkstyle插件的配置。确保配置文件checkstyle.xml中定义了你期望的代码规范。示例插件配置如下:
<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>8.44</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>validate</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <configLocation>config/checkstyle.xml</configLocation>
                    <consoleOutput>true</consoleOutput>
                    <!-- 随机返回非零的数 -->
                    <failsOnError>true</failsOnError>
                    <!-- 警告阻塞 -->
                    <failOnViolation>true</failOnViolation>
                </configuration>
            </plugin>
        </plugins>
    </build>

这将在validate阶段执行Checkstyle检查。

  1. 创建pre-commit脚本:
    在项目的.git/hooks目录下创建一个名为pre-commit的文件,其中包含运行Maven Checkstyle插件的命令。
#!/bin/bash

# Run Checkstyle before committing
 clean checkstyle:check

# Get the exit code of the Checkstyle task
MVN_RESULT=$?

echo "MVN_RESULT: $MVN_RESULT"

# Check if Maven Checkstyle fails or has warnings, prevent the commit
if [ $MVN_RESULT -ne 0 ]; then
    echo "Maven Checkstyle failed. Commit aborted."
	echo "MVN_RESULT: $MVN_RESULT"
    exit 1
fi

请注意,这里使用的是mvn validate,因为Maven的validate阶段包含了checkstyle:check目标。

  1. 赋予执行权限:
    确保pre-commit文件有执行权限。可以使用以下命令:
  chmod +x .git/hooks/pre-commit
  1. 测试:
    在提交代码之前,进行一次测试。如果Checkstyle检查不通过,提交将被阻止。

这样配置后,每次在提交代码时,Git将运行pre-commit钩子,它将触发Maven Checkstyle插件,以确保代码符合规范。

完成的项目代码大家可以参考 https://github.com/yanghaiji/checkstyle-githooks

Checkstyle.xml

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
        "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
        "https://checkstyle.org/dtds/configuration_1_3.dtd">

<module name="Checker">
	<!-- 字符集位utf8 -->
    <property name="charset" value="UTF-8"/>

    <!-- 设置违规问题的默认严重程度为“警告” error | warning | info -->
    <property name="severity" value="warning"/>

    <!-- 指定要由Checkstyle处理的文件扩展名(Java、properties和XML文件) -->
   
    <property name="fileExtensions" value="java, properties, xml , yaml , yml"/>
    <!-- 排除所有'module-info.java'文件 -->
    <!-- See https://checkstyle.org/config_filefilters.html -->
    <module name="BeforeExecutionExclusionFileFilter">
        <property name="fileNamePattern" value="module\-info\.java$"/>
    </module>
    <!-- 检查空白字符 -->       
    <!-- See http://checkstyle.sf.net/config_whitespace.html -->
    <module name="FileTabCharacter">
        <property name="eachLine" value="true"/>
    </module>

    <module name="LineLength">
        <property name="max" value="120"/>
        <property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://"/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小杨同学~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值