AgentLint 实测验证 98/100基于 120 个 Java 开源仓库统计

    Java CLAUDE.md & AGENTS.md 模板

    定向 Gradle/Maven 单测、Optional 空安全与 Spring/JVM 工程规范,提炼自 Java 开源仓库。

    # Java Project — Java Harness
    
    Java 21 Gradle multi-module service with JUnit 5, Spotless formatting, and Checkstyle gates.
    
    ## Commands
    
    - Fast single test: `./gradlew :core:test --tests '*InvoiceServiceTest'`
    - Full verification: `./gradlew check`
    - Format & lint: `./gradlew spotlessApply && ./gradlew checkstyleMain`
    
    ## Rules
    
    - Don't run the full multi-module Gradle build for a single class change. Instead target the module and test filter above. Because full builds take 2+ minutes per loop.
    - Don't return `null` from public service methods when absence is expected. Instead return `Optional<T>` or an empty collection. Because unchecked nulls cause production NPEs.
    - When changing database entities, read `docs/flyway-migrations.md` first.
    
    ## Continuity
    
    - Keep `application-local.yml` and `CLAUDE.local.md` out of git and record migration steps in `HANDOFF.md`.

    Java 开源生态中最常采用的工程规则

    基于 4,533 仓库研究中 120 个公开 Java 仓库的实测统计:

    maven工具链

    Maven build tool

    49.1%26 个仓库
    docker工具链

    Docker for containerization

    47.2%25 个仓库
    unit-tests测试

    Write unit tests for all changes

    41.5%22 个仓库
    spring框架

    Spring Framework

    39.6%21 个仓库
    spring-boot框架

    Spring Boot framework

    37.7%20 个仓库
    junit测试

    JUnit for testing

    32.1%17 个仓库
    optional代码规范

    Use Optional instead of null

    32.1%17 个仓库
    gradle工具链

    Gradle build tool

    30.2%16 个仓库
    必写验证命令(对应规则 W1 与 W7)
    安装依赖
    ./gradlew dependencies
    快速单测(秒级迭代)
    ./gradlew :module:test --tests '*TargetTest'
    全量测试
    ./gradlew check
    Lint 与静态检查
    ./gradlew spotlessCheck checkstyleMain
    生产构建
    ./gradlew build -x test
    Java 项目中 AI 代理常见踩坑
    • 01每次改一个类都跑全量 ./gradlew build 耗费数分钟,必须注明 --tests '*ClassName'。
    • 02AI 倾向注释掉 Spotless/Checkstyle 校验,而不是直接运行 ./gradlew spotlessApply。

    更多实证验证模板