You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot Cucumber Jar运行报错No backends were found 如何解决?

Spring Boot Cucumber打包运行报错:No backends were found

错误信息

Caused by: cucumber.runtime.CucumberException: No backends were found.
Please make sure you have a backend module on your CLASSPATH.

本地通过IntelliJ运行项目正常,但使用spring-boot-maven-plugin生成jar包后,执行java -jar target/hello-springboot-cucumber-1.0.0.jar命令触发该错误。

相关配置及代码

pom.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.egj.cucumber</groupId>
    <artifactId>hello-springboot-cucumber</artifactId>
    <version>1.0.0</version>
    <name>hello-springboot-cucumber</name>
    <description>Sample project to explain how to setup Cucumber in a Spring Boot Project</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- To generate getters, setters, equals, hascode, toString methods -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.26</version>
            <optional>true</optional>
        </dependency>

        <!-- To convert DTO to Entity and back -->
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-jdk8</artifactId>
            <version>1.2.0.Final</version>
        </dependency>

        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>1.2.0.Final</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
<!--            <scope>test</scope>-->
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>4.2.0</version>
<!--            <scope>test</scope>-->
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-spring</artifactId>
            <version>4.2.0</version>
<!--            <scope>test</scope>-->
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>4.2.0</version>
<!--            <scope>test</scope>-->
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java8</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>net.masterthought</groupId>
            <artifactId>cucumber-reporting</artifactId>
            <version>4.2.3</version>
        </dependency>

        <!-- To invoke REST endpoints from Cucumber step definitions -->
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
<!--            <scope>test</scope>-->
        </dependency>

        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy</artifactId>
            <version>3.0.6</version>
        </dependency>

    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

主类代码

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(CucumberTest.class, args);
//      io.cucumber.

        final String[] arguments = new String[]{
                "--glue","com.egj.cucumber.hello.bdd.stepdefs",
                "classpath:features"
        };

        cucumber.api.cli.Main.main(arguments);
    }

}

项目目录结构

Spring Boot & Cucumber App Directory Structure

问题原因及解决方法

核心原因

Spring Boot的spring-boot-maven-plugin打包的可执行jar采用特殊的BOOT-INF目录结构,旧版本Cucumber(4.x)无法识别该结构,导致找不到后端依赖和Step定义类。

解决步骤

1. 调整Spring Boot打包配置

修改spring-boot-maven-plugin,设置layout为ZIP,让Cucumber能正确识别类路径:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
            <configuration>
                <layout>ZIP</layout>
            </configuration>
        </execution>
    </executions>
</plugin>

2. 修正Cucumber的Glue路径

确保Glue路径包含Spring配置类所在的根包,让Cucumber能加载Spring上下文:

final String[] arguments = new String[]{
        "--glue","com.egj.cucumber",
        "classpath:features"
};

3. 升级Cucumber版本(推荐)

Cucumber 4.x对Spring Boot可执行jar支持有限,升级到6.x+版本能更好兼容。修改pom.xml中Cucumber依赖版本:

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>6.11.0</version>
</dependency>
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-spring</artifactId>
    <version>6.11.0</version>
</dependency>
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>6.11.0</version>
</dependency>
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-java8</artifactId>
    <version>6.11.0</version>
</dependency>

注意:升级后cucumber.api.cli.Main会变为io.cucumber.core.cli.Main,需要同步修改主类代码。

4. 替代方案:使用maven-assembly-plugin打包

如果不想升级Cucumber,改用maven-assembly-plugin打包成包含所有依赖的标准jar:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.3.0</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <manifest>
                <mainClass>com.egj.cucumber.Application</mainClass>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

打包后执行java -jar target/hello-springboot-cucumber-1.0.0-jar-with-dependencies.jar即可。

内容的提问来源于stack exchange,提问作者Ebillson GRAND JEAN

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 00:17:03