Ver Fonte

feat:添加静态检查脚本

lllgodenough há 1 ano atrás
pai
commit
15956d1b00
3 ficheiros alterados com 61 adições e 0 exclusões
  1. 14 0
      checkstyle.xml
  2. 26 0
      hooks/pre-commit
  3. 21 0
      pom.xml

+ 14 - 0
checkstyle.xml

@@ -0,0 +1,14 @@
+<?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">
+    <module name="TreeWalker">
+        <module name="AvoidStarImport"/>
+        <module name="IllegalImport"/>
+        <module name="RedundantImport"/>
+        <module name="UnusedImports"/>
+        <module name="MethodLength"/>
+        <module name="ParameterNumber"/>
+    </module>
+</module>

+ 26 - 0
hooks/pre-commit

@@ -0,0 +1,26 @@
+#!/bin/bash
+
+echo "Running pre-commit checks..."
+
+# 获取项目根目录
+PROJECT_DIR=$(git rev-parse --show-toplevel)
+
+# 运行编译和代码检查
+cd "$PROJECT_DIR" && mvn compile
+
+# 检查编译是否成功
+if [ $? -ne 0 ]; then
+    echo "Compilation failed! Please fix errors before committing."
+    exit 1
+fi
+
+# 运行静态代码分析(例如Checkstyle)
+cd "$PROJECT_DIR" && mvn checkstyle:check
+
+if [ $? -ne 0 ]; then
+    echo "Code style check failed! Please fix errors before committing."
+    exit 1
+fi
+
+echo "All checks passed! Proceeding with commit."
+exit 0

+ 21 - 0
pom.xml

@@ -241,6 +241,27 @@
                     <testFailureIgnore>true</testFailureIgnore>
                 </configuration>
             </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-checkstyle-plugin</artifactId>
+                <version>3.1.2</version>
+                <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>
     </build>