无法运行Karate Gatling场景,scala-maven-plugin编译失败求助
问题排查:Karate Gatling编译失败修复
问题描述
执行命令
mvn clean test-compile gatling:test运行Karate Gatling场景时出现错误:
[ERROR] Failed to execute goal net.alchim31.maven:scala-maven-plugin:4.8.1:testCompile (default) on project karate-template: Execution default of goal net.alchim31.maven:scala-maven-plugin:4.8.1:testCompile failed.: CompileFailed -> [Help 1]
项目pom.xml配置如下:
<groupId>io.karatelabs</groupId> <artifactId>karate-template</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>11</java.version> <maven.compiler.version>3.11.0</maven.compiler.version> <maven.surefire.version>3.0.0</maven.surefire.version> <karate.version>1.4.1</karate.version> <gatling.plugin.version>4.8.2</gatling.plugin.version> </properties> <dependencies> <dependency> <groupId>com.intuit.karate</groupId> <artifactId>karate-junit5</artifactId> <version>${karate.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>com.github.javafaker</groupId> <artifactId>javafaker</artifactId> <version>1.0.2</version> </dependency> <dependency> <groupId>net.masterthought</groupId> <artifactId>cucumber-reporting</artifactId> <version>5.6.1</version> </dependency> <dependency> <groupId>com.intuit.karate</groupId> <artifactId>karate-gatling</artifactId> <version>${karate.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <testResources> <testResource> <directory>src/test/java</directory> <excludes> <exclude>**/*.java</exclude> </excludes> </testResource> </testResources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven.compiler.version}</version> <configuration> <encoding>UTF-8</encoding> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>${maven.surefire.version}</version> <configuration> <argLine>-Dfile.encoding=UTF-8</argLine> </configuration> </plugin> <plugin> <groupId>io.gatling</groupId> <artifactId>gatling-maven-plugin</artifactId> <version>${gatling.plugin.version}</version> <configuration> <simulationsFolder>src/test/java/conduitApp/performance</simulationsFolder> <configFolder>src/test/java/conduitApp/performance/data</configFolder> </configuration> </plugin> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>4.8.1</version> <executions> <execution> <goals> <goal>testCompile</goal> </goals> <configuration> <args> <arg>-Jbackend:GenBCode</arg> <arg>-Jdelambdafy:method</arg> <arg>-target:jvm-1.8</arg> <arg>-deprecation</arg> <arg>-feature</arg> <arg>-unchecked</arg> <arg>-language:implicitConversions</arg> <arg>-language:postfixOps</arg> </args> </configuration> </execution> </executions> </plugin> </plugins> </build>
排查与解决方案
修正JDK编译目标版本:
scala-maven-plugin配置中指定了<arg>-target:jvm-1.8</arg>,但项目JDK版本为11,版本不匹配会导致编译失败。将该参数修改为<arg>-target:jvm-11</arg>,或直接移除该参数,让插件自动适配项目JDK版本。统一插件版本:
当前scala-maven-plugin版本(4.8.1)与gatling-maven-plugin版本(4.8.2)不一致,存在兼容风险。将scala-maven-plugin版本升级为4.8.2,保持版本统一:
<plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>4.8.2</version> <!-- 其余配置保持不变 --> </plugin>
- 添加Scala核心依赖:
Karate Gatling依赖Scala库,但当前pom未显式声明,导致编译时缺少依赖。在<dependencies>中添加Scala依赖:
<dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>2.13.10</version> <!-- Gatling 4.x对应Scala 2.13.x系列版本 --> <scope>test</scope> </dependency>
- 检查Scala仿真文件语法:
确认src/test/java/conduitApp/performance目录下的Gatling仿真文件(.scala)无语法错误,无效的Scala代码会直接导致testCompile阶段失败。
内容的提问来源于stack exchange,提问作者Prabhuraj Kulkarni
相关产品推荐
相关产品推荐

