如何在TeamCity中通过Maven依次运行Maven聚合项目子模块
解决方案
一、Maven聚合项目配置调整
首先确认你的项目结构,确保两个子模块在父项目下:
parent-project/ ├── pom.xml ├── automation-tests/ │ ├── pom.xml │ └── target/ │ └── surefire-reports/ # 测试报告默认生成目录 └── report-generator/ ├── pom.xml └── src/
1. 父POM指定模块执行顺序
在父项目的pom.xml中按先测试、后报告的顺序声明子模块,Maven会严格按此顺序执行各模块的生命周期:
<modules> <module>automation-tests</module> <module>report-generator</module> </modules>
2. 自动化测试模块固定报告路径
在automation-tests/pom.xml中配置maven-surefire-plugin,确保HTML报告生成到固定目录(如果用JUnit/TestNG,默认已生成HTML报告):
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.2.5</version> <configuration> <!-- 锁定报告生成目录,方便后续模块读取 --> <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory> <!-- 若使用TestNG,可添加参数强制生成HTML报告:<useDefaultListeners>true</useDefaultListeners> --> </configuration> </plugin> </plugins> </build>
3. 报告生成模块依赖文件处理与打包
在report-generator/pom.xml中配置两个插件:
- 用
maven-resources-plugin复制测试模块的HTML报告到当前模块目录 - 用
maven-assembly-plugin将报告打包为ZIP(若有发送逻辑,可一并打包执行代码)
<build> <plugins> <!-- 复制测试模块的HTML报告及相关静态资源 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.3.1</version> <executions> <execution> <id>copy-test-reports</id> <phase>process-resources</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/test-reports</outputDirectory> <resources> <resource> <directory>${project.parent.basedir}/automation-tests/target/surefire-reports/html</directory> <includes> <include>**/*.html</include> <include>**/*.css</include> <include>**/*.js</include> </includes> </resource> </resources> </configuration> </execution> </executions> </plugin> <!-- 将报告打包为ZIP文件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.6.0</version> <executions> <execution> <id>package-report-zip</id> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <descriptorRefs> <descriptorRef>zip</descriptorRef> </descriptorRefs> <includes> <include>${project.build.directory}/test-reports/**/*</include> <!-- 若有自定义的报告发送脚本/可执行JAR,也添加到这里 --> </includes> </configuration> </execution> </executions> </plugin> </plugins> </build>
如果报告生成模块有Java发送逻辑,可配合maven-shade-plugin打可执行JAR,方便后续调用。
二、TeamCity构建配置
1. 创建Maven构建任务
- 新建Build Configuration,选择Maven作为构建Runner
- 设置
Working directory为父项目根目录,例如:%teamcity.build.checkoutDir%/parent-project
2. 配置Maven命令
在Goals字段输入:clean install
clean会清理之前的构建产物,避免旧报告干扰install会按父POM的模块顺序,先执行automation-tests的test阶段(生成报告),再执行report-generator的package阶段(打包ZIP)
3. 可选:添加报告发送步骤
如果需要批量发送ZIP报告,可在Maven步骤后添加额外Build Step:
- 选择Command Line Runner,执行脚本发送
report-generator/target/report-generator-*.zip(比如用邮件命令、FTP上传脚本等) - 或使用TeamCity自带的通知插件,将ZIP作为附件发送给指定收件人
三、核心注意事项
- 模块顺序不可乱:父POM的
modules列表必须保证automation-tests在前,否则报告生成模块会找不到测试文件 - 路径一致性:所有路径使用Maven内置变量(如
${project.parent.basedir}),避免硬编码,确保在TeamCity代理环境中正常工作 - 失败终止逻辑:若自动化测试失败,Maven会自动终止
install命令,不会执行报告生成,符合预期 - 权限检查:确保TeamCity构建代理对项目目录有读写权限,避免文件复制或打包失败
内容的提问来源于stack exchange,提问作者Ivan
相关产品推荐
相关产品推荐

