如何生成 HTML 报告并上传到 GitHub Pages?
解读
在国内企业面试中,这道题表面问“报告+部署”,实则考察候选人是否能把 Maven 生命周期、插件体系与 GitHub Actions 国内镜像源、合规网络环境打通。面试官希望听到:
- 用 Maven 官方插件生成标准化 HTML 报告(surefire、site、jacoco 等);
- 把报告目录精准定位到 GitHub Pages 要求的 /docs 或 gh-pages 分支;
- 通过 gh-actions 国内缓存+Token 最小权限 完成上传,而不是手动 ftp 或 rsync;
- 兼顾 Gitee Pages 等国内托管的差异化配置,体现“一套脚本,多处运行”的工程思维。
知识点
- maven-surefire-report-plugin:把单元测试结果转为 HTML,中文编码无乱码。
- maven-site-plugin:聚合多模块报告,支持自定义 skin 与企业 Logo。
- jacoco-maven-plugin:生成代码覆盖率 HTML,阈值不达标可中断 CI。
- maven-resources-plugin:把报告目录拷贝到 ${project.build.directory}/report-bundle,方便后续上传。
- github-actions 环境变量:GITHUB_TOKEN 默认只读,需显式授予 contents:write、pages:write、id-token:write 权限。
- actions/upload-pages-artifact 与 actions/deploy-pages:官方提供的两步交付,避免直接 push 到 gh-pages 历史膨胀。
- 国内网络加速:在 actions 里把 actions 仓库源 替换为 hub.fastgit.org 或自建缓存,防止 maven-central 连接超时。
- Gitee Pages 强制实名:需提前在仓库设置里绑定 Gitee ID,否则 403。
答案
- 在 pom.xml 中声明报告插件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<outputDirectory>${project.build.directory}/site/surefire</outputDirectory>
<charset>UTF-8</charset>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.11</version>
<executions>
<execution>
<goals><goal>prepare-agent</goal></goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals><goal>report</goal></goals>
<configuration>
<outputDirectory>${project.build.directory}/site/jacoco</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
- 统一聚合到 site 阶段
mvn clean test site -DgenerateReports=true
生成的 target/site/ 目录即包含 index.html、surefire.html、jacoco/index.html 等完整报告。
- 本地验证后,把报告目录拷贝到 docs/ 并推送到主分支,或走 gh-pages 分支策略。
cp -r target/site/* docs/
git add docs && git commit -m "chore: update maven report"
git push origin main
-
在 GitHub 仓库 Settings → Pages → Source 选择 Branch: main /docs,Save 后 Pages 地址即刻生效,访问 https://{user}.github.io/{repo}/ 即可在线浏览。
-
若使用 GitHub Actions 全自动,创建 .github/workflows/report.yml
name: Build-Report-Deploy
on:
push:
branches: [ main ]
jobs:
report:
runs-on: ubuntu-latest
permissions:
contents: read
pages: write
id-token: write
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'
cache: 'maven'
- name: Generate Report
run: mvn -B clean test site
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: target/site
- name: Deploy to GitHub Pages
uses: actions/deploy-pages@v4
合并代码后,Actions 自动跑完即可在 Pages 看到最新报告,全程 零手动、零 ftp、零公网 IP。
拓展思考
- 多模块聚合:在父 pom 里用 <site><locales>zh_CN</locales></site> 统一中文导航,子模块通过 <distributionManagement><siteURL>...</siteURL> 继承,避免每个子模块重复上传。
- 质量门禁:在 jacoco 插件里配置 <rules><rule><limits><limit><counter>LINE</counter><value>COVEREDRATIO</value><minimum>0.80</minimum></limit></limits></rule></rules>,覆盖率低于 80% 直接失败,保证 Pages 上的报告永远是“绿色”状态。
- 增量报告:利用 git diff 拿到变更文件列表,通过 jacoco:report-aggregate 只生成增量覆盖率,减少 Pages 存储占用,适合大型遗留项目。
- 国内合规:若代码托管在私有 GitLab 或 Gitee 企业版,可把 actions/deploy-pages 替换为 rsync 到自有 OSS+CDN,Maven 侧只需把报告目录输出为静态文件即可,实现“一次 mvn site,处处发布”。