chore: 前后端引入代码检查工具并整改存量问题
为统一前后端代码规范与质量,前端引入 ESLint + Prettier,后端在 Spotless 之外新增 Checkstyle 与 SpotBugs,检查接入构建与打包流程并失败即阻断,同时一次性整改全部存量告警,检查范围为后端主源码与前端源码。 变更内容: - 前端新增 ESLint(flat config)与 Prettier 配置及 lint / lint:fix / format / format:check 脚本,并新增对应开发依赖 - 后端 build.gradle 应用 checkstyle 与 com.github.spotbugs 插件,新增 gradle/checkstyle/checkstyle.xml、suppressions.xml 与 gradle/spotbugs/exclude.xml - 新增 Gradle frontendLint 任务并作为 frontendBuild 前置,新增 lint 聚合任务(Spotless + Checkstyle + SpotBugs + 前端 ESLint) - 修复后端 Checkstyle 1 处(补花括号)与 SpotBugs 17 处(getParent() 判空、拆装箱冗余、异常捕获与日志、volatile 同步等),并按设计豁免值对象引用暴露等告警 - 修复前端规则问题(未使用变量、无意义赋值、v-for key、v-html 安全提示)并格式化存量文件 - README 补充依赖安装、代码检查与打包说明 说明: - SpotBugs 工具版本固定为 4.8.6,规避其 BCEL 与 Spring Boot BOM 选定 commons-lang3 3.14.0 的 NoSuchMethodError - 检查范围仅主源码,src/test/ 本地测试不纳入 Checkstyle/SpotBugs - 前端格式化统一为无分号、单引号、行宽 120、LF,与 .gitattributes 保持一致
This commit is contained in:
+63
-1
@@ -1,8 +1,13 @@
|
||||
import com.github.spotbugs.snom.Confidence
|
||||
import com.github.spotbugs.snom.Effort
|
||||
|
||||
plugins {
|
||||
id 'java'
|
||||
id 'checkstyle'
|
||||
id 'org.springframework.boot' version '3.3.6'
|
||||
id 'io.spring.dependency-management' version '1.1.7'
|
||||
id 'com.diffplug.spotless' version '8.10.2'
|
||||
id 'com.github.spotbugs' version '6.5.12'
|
||||
}
|
||||
|
||||
group = 'cn.rosercode'
|
||||
@@ -120,6 +125,44 @@ tasks.named('compileJava') {
|
||||
dependsOn 'spotlessJavaCheck'
|
||||
}
|
||||
|
||||
// ===== 代码静态检查(Checkstyle / SpotBugs)=====
|
||||
// 二者挂在 check 任务上,由 ./package.sh(gradle check)触发;
|
||||
// 不进入 ./reload.sh 的快速热重载路径(compileJava),保持开发体验。
|
||||
// 配置文件放在 gradle/ 下(项目根的 config/ 被 .gitignore 忽略,仅用于运行时配置)。
|
||||
checkstyle {
|
||||
configDirectory = file('gradle/checkstyle')
|
||||
maxWarnings = 0
|
||||
ignoreFailures = false
|
||||
}
|
||||
|
||||
spotbugs {
|
||||
// 固定 4.8.6:其 BCEL 不依赖 commons-lang3 3.18+ 的 getJdkModulePath,
|
||||
// 避免与 Spring Boot BOM 选定的 commons-lang3 3.14.0 冲突(NoSuchMethodError)。
|
||||
toolVersion = '4.8.6'
|
||||
effort = Effort.MAX
|
||||
reportLevel = Confidence.valueOf('MEDIUM')
|
||||
excludeFilter = file('gradle/spotbugs/exclude.xml')
|
||||
ignoreFailures = false
|
||||
}
|
||||
|
||||
tasks.named('spotbugsMain') {
|
||||
reports {
|
||||
xml {
|
||||
required = true
|
||||
outputLocation = file("${layout.buildDirectory.get().asFile}/reports/spotbugs/main.xml")
|
||||
}
|
||||
html {
|
||||
required = true
|
||||
outputLocation = file("${layout.buildDirectory.get().asFile}/reports/spotbugs/main.html")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 测试源码(src/test)仅本地验证、不提交(见 AGENTS.md),不纳入 Checkstyle/SpotBugs
|
||||
tasks.matching { it.name == 'checkstyleTest' || it.name == 'spotbugsTest' }.configureEach {
|
||||
enabled = false
|
||||
}
|
||||
|
||||
// ===== 前端构建与打包 =====
|
||||
def frontendDir = file("${projectDir}/frontend")
|
||||
def frontendDistDir = file("${frontendDir}/dist")
|
||||
@@ -144,11 +187,23 @@ tasks.register('npmInstall', Exec) {
|
||||
outputs.dir("${frontendDir}/node_modules")
|
||||
}
|
||||
|
||||
// 前端代码检查(ESLint),失败则中断构建
|
||||
tasks.register('frontendLint', Exec) {
|
||||
group = 'frontend'
|
||||
description = '运行前端 ESLint 检查(npm run lint)'
|
||||
dependsOn 'npmInstall'
|
||||
workingDir frontendDir
|
||||
commandLine resolveNpm(), 'run', 'lint'
|
||||
inputs.dir("${frontendDir}/src")
|
||||
inputs.file("${frontendDir}/eslint.config.js")
|
||||
inputs.file("${frontendDir}/package.json")
|
||||
}
|
||||
|
||||
// 构建前端产物
|
||||
tasks.register('frontendBuild', Exec) {
|
||||
group = 'frontend'
|
||||
description = '构建前端产物(npm run build)'
|
||||
dependsOn 'npmInstall'
|
||||
dependsOn 'npmInstall', 'frontendLint'
|
||||
workingDir frontendDir
|
||||
commandLine resolveNpm(), 'run', 'build'
|
||||
inputs.dir("${frontendDir}/src")
|
||||
@@ -158,6 +213,13 @@ tasks.register('frontendBuild', Exec) {
|
||||
outputs.dir(frontendDistDir)
|
||||
}
|
||||
|
||||
// 聚合代码检查任务:后端 Spotless/Checkstyle/SpotBugs(主源码)+ 前端 ESLint
|
||||
tasks.register('lint') {
|
||||
group = 'verification'
|
||||
description = '运行全部代码检查(后端 Spotless、Checkstyle、SpotBugs 与前端 ESLint)'
|
||||
dependsOn 'spotlessCheck', 'checkstyleMain', 'spotbugsMain', 'frontendLint'
|
||||
}
|
||||
|
||||
// 将前端产物打入可执行 jar 的静态资源目录,实现前后端一体化部署
|
||||
tasks.named('bootJar') {
|
||||
dependsOn 'frontendBuild'
|
||||
|
||||
Reference in New Issue
Block a user