如何修改Surefire HTML报告的页面顶部标题?
修改Maven Surefire HTML报告的页面标题
Maven Surefire Report插件没有直接提供配置项修改报告顶部的“Surefire Report”标题,但可以通过自定义Velocity模板实现这个需求,具体步骤如下:
复制默认模板到项目资源目录
将Surefire插件内置的Velocity模板org/apache/maven/plugins/surefire/report/surefire-report.vm从插件jar包中提取出来,复制到项目的src/main/resources/surefire目录下(目录不存在则手动创建)。修改模板中的标题内容
打开复制的surefire-report.vm文件,找到两处标题相关的代码:
- 页面顶部的标题标签:
<h1>Surefire Report</h1> - HTML页面的title标签:
<title>Surefire Report</title>
将这两处的Surefire Report替换为你的自定义文本,比如和文件名一致的Results-${env} Environment。
- 在插件配置中指定自定义模板
更新maven-surefire-report-plugin的配置,添加reportTemplate参数指向自定义模板的路径:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-report-plugin</artifactId> <version>3.0.0-M7</version> <configuration> <outputName>Results-${env} Environment</outputName> <!-- 指定自定义Velocity模板路径 --> <reportTemplate>${project.basedir}/src/main/resources/surefire/surefire-report.vm</reportTemplate> </configuration> <executions> <execution> <phase>test</phase> <goals> <goal>report-only</goal> </goals> </execution> </executions> </plugin>
补充说明
- 如果需要动态替换标题(比如使用环境变量),确保模板中使用的Maven属性(如
${env})在构建时能被正确解析。 - 验证模板路径是否正确,
${project.basedir}代表项目的根目录。
内容的提问来源于stack exchange,提问作者Maddy
相关产品推荐
相关产品推荐

