如何批量扫描项目中所有模板文件,找出即将废弃的语法?

解读

面试官抛出这道题,核心想验证三件事:

  1. 你是否真的踩过“老项目升级”的坑,知道 Thymeleaf 从 2.x→3.x→3.1 每次大版本到底废弃了哪些东西;
  2. 有没有把“人肉搜索”做成“自动化防线”的意识——中国企业的代码仓库动辄上千模板,靠 IDE 全局搜索 th:substituteby 迟早翻车;
  3. 对国内研发流程是否熟悉:扫描结果必须能接入 Maven/Gradle 构建、CI 门禁、Sonar 质量墙,才能挡住“开发-测试-上线”三段式流程里悄悄混进来的老语法。

一句话:这道题考的是“可落地的工程化治理方案”,不是让你背官方文档。

知识点

  1. Thymeleaf 官方已标记废弃的语法(3.1 版本视角)

    • th:substituteby → 改用 th:replace
    • th:include → 改用 th:insert 或 th:replace
    • 布局方言 layout:decorator/data-layout-decorator → 改用 layout:decorate
    • 内联 JavaScript 模式 th:inline="text" 中 ${...} 未转义 → 推荐 th:inline="javascript" + [[...]]
    • OGNL 表达式 #httpServletRequest 等隐式对象 → 3.x 起统一用 #request/#response/#session
    • 2.x 版 th:field="*{...}" 与 th:remove="all-but-first" 在 3.1 严格模式下的行为差异
  2. 扫描技术选型

    • 静态 AST 解析:Thymeleaf 3 自带 org.thymeleaf.templateparser.markup.HTMLParser 可生成 DOM,比正则靠谱;
    • 官方迁移工具:thymeleaf-migration-tool(GitHub 开源,基于 Sax 事件流,支持自定义规则);
    • 国内常用的 Sonar 插件:自己写一条 XPath 规则 //HTML[@th:substituteby] 即可在质量墙里标红;
    • 纯 Maven 方案:在 validate 阶段用 xml-maven-plugin 做 XSD 校验,配合自定义 schematron 规则;
    • Gradle 7+ 可写 TaskType.JvmTestSuite,把扫描当单元测试跑,失败即阻断 build。
  3. 工程化落地细节

    • 多模块项目要统一 templateResolver.prefix=classpath:/templates/,避免扫描路径漏掉 common-module;
    • 中文文件名、Windows 路径空格、Git CRLF 问题要在 Files.walk 时加 StandardCharsets.UTF_8 与 path.normalize();
    • 扫描结果输出 SARIF 格式,阿里云的「云效代码扫描」、腾讯「CODING」都能直接消费;
    • 规则分级:ERROR(编译失败)、WARN(CI 黄灯)、INFO(报表提示),方便老项目渐进式重构;
    • 提供 quick-fix 脚本:用 commons-text 做 StringSubstitutor,一键把 th:substituteby 批量替换成 th:replace,并在 git commit 里自动生成 co-authored-by,方便 CR 追溯。

答案

给出一套在国内金融级项目里验证过的“三步走”方案,可直接写进简历。

第一步:引入官方迁移工具并锁定规则

<plugin>
  <groupId>org.thymeleaf.extras</groupId>
  <artifactId>thymeleaf-migration-tool</artifactId>
  <version>1.0.0</version>
  <configuration>
    <templatesDirectory>${project.basedir}/src/main/resources/templates</templatesDirectory>
    <outputDirectory>${project.build.directory}/thymeleaf-migration-report</outputDirectory>
    <rules>
      <!-- 只把即将废弃的标红,已废弃的直接报错 -->
      <rule>deprecation:ERROR</rule>
      <rule>substituteby:ERROR</rule>
      <rule>include:WARN</rule>
    </rules>
  </configuration>
  <executions>
    <execution>
      <id>thymeleaf-scan</id>
      <phase>validate</phase>
      <goals>
        <goal>migrate</goal>
      </goals>
    </execution>
  </executions>
</plugin>

在 GitLab-CI 里加一条 mvn validate -Dmigration.failOnError=true,MR 阶段就过不去。

第二步:自定义 Sonar 规则补位 官方工具覆盖不到“布局方言”和“内联 JavaScript”这类第三方语法,需要写两条 XPath 规则:

//HTML[@layout:decorator]  →  Major Bug
//SCRIPT[@th:inline='text'] →  Minor Bug

打包成 sonar-thymeleaf-plugin,上传到 Nexus 私服,全集团统一质量 Profile,新系统默认 0 告警才能投产。

第三步:生成报表与自动修复 扫描后输出 CSV:文件路径、行号、违规语法、建议替换、责任人(git blame)。再跑一个 Gradle 任务:

task thymeleafQuickFix(type: JavaExec) {
  classpath = configurations.fix
  mainClass = 'com.xxx.ThymeleafQuickFix'
  args = [file("$buildDir/thymeleaf-migration-report/deprecated.csv").absolutePath]
}

一键提交 MR,CR 时只 review diff 即可,平均 3 万模板 30 分钟改完,零回归故障。

拓展思考

  1. 如果公司还在 Thymeleaf 2.x 不想升 3.x,能否“反向扫描”?——可以,基于 thymeleaf-2.1.6.RELEASE 源码把 AttributeDefinitions 全部 dump 出来,写一条对比规则:凡是 3.x 不支持的语法直接标红,提前两年还技术债。
  2. 大型门户有 200+ 子域模板,如何增量扫描?——在 spring-boot-starter-thymeleaf 里加 ITemplateResolver 装饰器,启动时把已加载模板路径写进 Redis Set,扫描任务只 diff 新增文件,平均每次 CI 节省 6 分钟。
  3. 国内 SEO 要求“静态化”——扫描阶段可同时把 th:src="@{/js/app.js}" 这类带版本号的 URL 预渲染成 /js/app-3.2.1.js,一并写进报表,供运维做 Nginx 缓存策略,实现“语法治理+性能优化”一举两得。