如何将现有单模块Maven自动化项目重构为多模块结构?
多模块项目重构方案:整合依赖、分离并行/串行、实现重试逻辑
一、项目结构设计
先梳理出职责清晰的多模块目录结构,从根源上拆分重复配置与核心逻辑:
automation-framework/ ├── pom.xml # 父POM:统一管理所有公共依赖、插件版本及基础配置 ├── framework-core/ # 核心业务模块:存放Selenium工具类、Cucumber步骤定义、公共配置文件 │ └── pom.xml ├── runner-parallel/ # 并行运行模块:封装原并行执行的插件配置 │ └── pom.xml └── runner-serial/ # 串行运行+重试模块:处理串行测试及失败用例重试 └── pom.xml
二、父POM:提取公共依赖与配置
把原来两个POM里的重复依赖、插件配置统一放到父POM中,子模块直接继承即可,彻底消除重复维护的麻烦:
<project> <modelVersion>4.0.0</modelVersion> <groupId>com.yourcompany.automation</groupId> <artifactId>automation-framework</artifactId> <version>1.0.0</version> <packaging>pom</packaging> <modules> <module>framework-core</module> <module>runner-parallel</module> <module>runner-serial</module> </modules> <!-- 统一管理依赖版本,子模块直接引用无需指定version --> <dependencyManagement> <dependencies> <!-- Selenium --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.15.0</version> </dependency> <!-- Cucumber-JVM --> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-java</artifactId> <version>7.14.0</version> </dependency> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-junit</artifactId> <version>7.14.0</version> <scope>test</scope> </dependency> <!-- 其他公共依赖... --> </dependencies> </dependencyManagement> <!-- 公共插件配置,子模块可继承或按需覆盖 --> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.2.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
三、并行运行模块(runner-parallel)
这个模块专门负责原并行执行逻辑,继承父POM后只需配置cucumber-jvm-parallel-plugin即可:
<project> <parent> <groupId>com.yourcompany.automation</groupId> <artifactId>automation-framework</artifactId> <version>1.0.0</version> </parent> <artifactId>runner-parallel</artifactId> <packaging>jar</packaging> <dependencies> <!-- 依赖核心业务模块 --> <dependency> <groupId>com.yourcompany.automation</groupId> <artifactId>framework-core</artifactId> <version>1.0.0</version> </dependency> <!-- 并行插件依赖 --> <dependency> <groupId>com.github.temyers</groupId> <artifactId>cucumber-jvm-parallel-plugin</artifactId> <version>5.0.0</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>com.github.temyers</groupId> <artifactId>cucumber-jvm-parallel-plugin</artifactId> <version>5.0.0</version> <executions> <execution> <id>generateRunners</id> <phase>generate-test-sources</phase> <goals> <goal>generateRunners</goal> </goals> <configuration> <glue>com.yourcompany.steps</glue> <featuresDirectory>src/test/resources/features</featuresDirectory> <parallelScheme>SCENARIO</parallelScheme> <!-- 其他并行配置... --> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <forkCount>5</forkCount> <!-- 并行线程数 --> <reuseForks>true</reuseForks> </configuration> </plugin> </plugins> </build> </project>
执行并行测试的命令调整为:
mvn -f runner-parallel/pom.xml clean verify -Dcucumber.options="--tags @ios_e2e,@android_e2e" -DtargetEnv="cloudStackEnv"
四、串行运行+重试模块(runner-serial)
针对串行运行和重试需求,我们用Maven Profile分离两个阶段,解决maven-surefire-plugin无法保证执行顺序的问题:
<project> <parent> <groupId>com.yourcompany.automation</groupId> <artifactId>automation-framework</artifactId> <version>1.0.0</version> </parent> <artifactId>runner-serial</artifactId> <packaging>jar</packaging> <dependencies> <dependency> <groupId>com.yourcompany.automation</groupId> <artifactId>framework-core</artifactId> <version>1.0.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <!-- 指定你的串行Cucumber Runner类 --> <includes> <include>com/yourcompany/runners/SerialCucumberRunner.java</include> </includes> <!-- 生成失败用例报告,供重试使用 --> <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory> <failIfNoTests>false</failIfNoTests> </configuration> </plugin> </plugins> </build> <!-- 重试专用Profile --> <profiles> <profile> <id>retry-failed</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <!-- 读取之前生成的失败用例报告 --> <systemPropertyVariables> <cucumber.options>--rerun ${project.build.directory}/surefire-reports/rerun.txt</cucumber.options> </systemPropertyVariables> <!-- 可选:指定重试专用Runner --> <includes> <include>com/yourcompany/runners/RetryCucumberRunner.java</include> </includes> </configuration> </plugin> </plugins> </build> </profile> </profiles> </project>
执行命令
- 正常串行测试:
mvn -f runner-serial/pom.xml clean verify
- 重试失败用例(需在串行测试执行后运行):
mvn -f runner-serial/pom.xml clean verify -Pretry-failed
如果想一键完成“串行测试+自动重试”,可以在串行模块中添加maven-invoker-plugin,让重试阶段依赖于串行测试的执行结果。
五、方案优势
- 依赖统一维护:父POM集中管理所有依赖版本,避免重复配置
- 职责清晰拆分:核心逻辑、并行、串行模块各司其职,便于团队协作
- 重试逻辑可控:通过Profile隔离正常测试与重试流程,解决插件执行顺序问题
内容的提问来源于stack exchange,提问作者user2451016
相关产品推荐
相关产品推荐

