Maven命令本地运行正常但Jenkins无法执行指定Cucumber测试
问题现象
- 配置了profile的Maven项目,在本地终端执行如下命令时,目标测试可正常运行:
mvn clean install -Pserial-integration-test -Dcucumber.options="--tags @'QR_Scan'" -Dtest.environment=PrePRod - 相同逻辑的命令在Jenkins中执行时,无法触发预期的目标测试。当前已创建Jenkins Maven类型项目,正确指定了pom.xml文件路径,在「目标与选项」配置栏填入的执行命令为:
clean install -Pserial-integration-test -Dcucumber.options="--tags @'QR_Scan'" -Dtest.environment=PrePRod
相关配置说明
Maven Profile配置
使用的profile为serial-integration-test,对应pom.xml中插件配置如下:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.0</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>2.22.0</version> <configuration> <junitPlatformArtifactName>com.github.junit-team.junit5:junit-platform-engine</junitPlatformArtifactName> <forkCount>1</forkCount> <reuseForks>true</reuseForks> <includes> <include>**/Parallel*IT.class</include> </includes> <systemPropertyVariables> <test.environment></test.environment> </systemPropertyVariables> </configuration> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>com.github.temyers</groupId> <artifactId>cucumber-jvm-parallel-plugin</artifactId> <version>5.0.0</version> <executions> <execution> <id>generateRunners</id> <phase>validate</phase> <goals> <goal>generateRunners</goal> </goals> <configuration> <plugins> <plugin> <name>com.gm.onstar.rita.cucumber.CucumberReporter</name> </plugin> </plugins> <glue> <package>com.gm.onstar.rita.stepdefinitions</package> </glue> <featuresDirectory>src/main/resources/features</featuresDirectory> <tags> <tag>${cucumber.tag1}</tag> <tag>${cucumber.tag2}</tag> <tag>${cucumber.tag3}</tag> <tag>${cucumber.tag4}</tag> <tag>${cucumber.tag5}</tag> <tag>~@Ignore</tag> </tags> </configuration> </execution> </executions> </plugin> </plugins> </build>
已尝试的排查方案
- 调整Jenkins中cucumber.options参数写法,分别测试
-Dcucumber.options="--tags @QR_Scan"、-Dcucumber.options='--tags @QR_Scan'两种格式,均未解决问题 - 调整profile参数格式,分别测试
-Pserial-integration-test(无空格)、-P serial-integration-test(带空格)两种写法,均未解决问题 - 检查项目依赖,确认未缺失相关Maven插件
- 在Jenkins中分别执行
mvn clean install、mvn clean verify命令验证执行效果,均无法触发目标测试
初步怀疑的问题方向
- Cucumber runner类存放在main目录而非test目录,导致Jenkins执行时无法识别测试类
- Jenkins运行环境缺少必要的依赖插件
- Jenkins中配置的执行命令格式存在错误,比如缺失必要符号
排查思路与解决方案
第一优先级排查:Jenkins Maven项目的参数转义问题
Jenkins原生Maven项目类型会对「目标与选项」中传入的带引号参数做自动转义,直接传入嵌套引号的-Dcucumber.options参数时,引号会被识别为普通字符传入Maven,导致Cucumber无法正确解析tag过滤规则。
- 验证方式:在构建配置中开启Maven命令打印开关,查看实际执行的完整命令,会发现cucumber.options的参数值外层多了一层转义引号,和本地执行的命令不一致。
- 修复方案:
- 临时修复:去掉参数外层所有嵌套的单双引号,在目标栏直接写为
clean install -Pserial-integration-test -Dcucumber.options=--tags @QR_Scan -Dtest.environment=PrePRod - 永久规避:放弃Jenkins原生Maven项目类型,改用自由风格项目,在构建步骤中选择「执行Shell」(Linux节点)或「执行Windows批处理命令」(Windows节点),直接写入和本地完全一致的命令,避免Jenkins自动篡改参数格式。
- 临时修复:去掉参数外层所有嵌套的单双引号,在目标栏直接写为
第二优先级排查:failsafe插件的测试类扫描规则问题
从pom配置看,maven-failsafe-plugin配置的扫描规则是仅匹配路径下名为Parallel*IT.class的类:
- 本地如果用IDE触发测试,IDE会自动扫描所有带@Test注解的类,不受failsafe插件include规则限制,但Jenkins执行Maven命令时会严格遵守该规则。
- 检查cucumber-jvm-parallel-plugin生成的runner类:
- 如果生成的runner类命名不满足
Parallel*IT格式,failsafe插件根本不会扫描到这些测试类 - 如果runner类生成在
target/generated-test-sources目录下,确认该目录是否被标记为测试源码目录,未标记的话编译后不会放入test-classes路径,failsafe无法识别
- 如果生成的runner类命名不满足
- 修复方案:在cucumber-jvm-parallel-plugin的configuration节点补充配置:
<namingScheme>parallel</namingScheme>,指定生成的runner类名匹配failsafe的扫描规则,同时添加build-helper-maven-plugin将生成runner的目录显式加入测试源码路径。
第三优先级排查:系统参数传递问题
当前failsafe插件配置中<systemPropertyVariables>下的<test.environment>为空值,部分Jenkins Maven版本存在命令行传入的-D参数不传递给测试fork进程的问题。
- 修复方案:在failsafe-plugin的configuration节点添加
<argLine>-Dtest.environment=${test.environment}</argLine>配置,显式把命令行传入的环境参数传递给测试执行的JVM进程。
其他排查点
- 检查Jenkins节点使用的Maven版本、JDK版本是否和本地执行成功的版本完全一致,版本差异会导致插件执行逻辑不同
- runner类存放路径不是问题:如果本地命令行执行mvn命令能跑通,说明类路径扫描逻辑正常,Jenkins和本地使用相同pom文件不会出现目录识别差异,只需检查Jenkins拉取的代码是否完整,工作空间中的文件是否和本地一致即可
- 执行命令添加
-X参数开启Maven debug日志,搜索Activated profiles字段,确认serial-integration-testprofile确实被激活,避免profile路径配置错误导致插件配置未生效。
内容的提问来源于stack exchange,提问作者iDon'tDrinkJava
相关产品推荐
相关产品推荐

