jar运行时找不到后端

jar运行时找不到后端

本文介绍了黄瓜:从Spring Boot jar运行时找不到后端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个小型测试框架,该框架应同时使用Cucumber和Spring Boot平台.这样做的目的是让整个应用程序打包为一个jar,然后在对BDD功能进行了适当的参数设置后运行.

I am creating a small testing framework that should utilize both Cucumber and the Spring Boot platform. The idea is to let the whole application be packaged as a single jar and run after the BDD features have been properly parametrized.

该框架从命令行运行器模式开始,如下所示:

The framework starts in command line runner mode like this:

public class FwApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(FwApplication.class, args);
    }

    @Override
    public void run(String... arg0) throws Exception {
        JUnitCore.main(CucumberIntegration.class.getCanonicalName());
    }
}

然后是CucumberIntegration类:

Then there is the CucumberIntegration class:

@RunWith(Cucumber.class)
@CucumberOptions(features = "config/features")
@ContextConfiguration(classes= AppConfiguration.class)
public class CucumberIntegration {
}

我也有一些简单的测试,这些测试可以在我的IDE下很好地运行,但是当我尝试打包应用程序并通过java -jar fw-0.0.1-SNAPSHOT.jar运行它时,会看到以下内容:

I have also some simple tests which run fine under my IDE, but when I try to package the application and run it over java -jar fw-0.0.1-SNAPSHOT.jar I get to see following:

There was 1 failure:
1) initializationError(com.fmr.bddfw.test.CucumberIntegration)
cucumber.runtime.CucumberException: No backends were found. Please make sure you have a backend module on your CLASSPATH.
        at cucumber.runtime.Runtime.<init>(Runtime.java:81)
        at cucumber.runtime.Runtime.<init>(Runtime.java:70)
        (...)

所有必需的jar都已经在maven创建的一个jar中,并且在我的IDE下可以正常工作.

All necessary jars are already in the one jar created by maven and it works fine under my IDE.

有什么想法可以帮助您吗?

Any ideas what could help?

编辑:这是我的pom文件.

EDIT: Here my pom file.

<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>

    <groupId>com.xxx</groupId>
    <artifactId>fw</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>fw</name>
    <description>BDD Testing Framework</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <cucumber.version>1.2.5</cucumber.version>
    </properties>

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

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

        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>${cucumber.version}</version>
        </dependency>

        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-spring</artifactId>
            <version>${cucumber.version}</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

推荐答案

使用:

第一步:创建自定义的MultiLoader类:

Step1: Create your custom MultiLoader class:

package cucumber.runtime.io;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

public class CustomMultiLoader implements ResourceLoader {
    public static final String CLASSPATH_SCHEME = "classpath*:";
    public static final String CLASSPATH_SCHEME_TO_REPLACE = "classpath:";
    private final ClasspathResourceLoader classpath;
    private final FileResourceLoader fs;
    public CustomMultiLoader(ClassLoader classLoader) {
        classpath = new ClasspathResourceLoader(classLoader);
        fs = new FileResourceLoader();
    }
    @Override
    public Iterable<Resource> resources(String path, String suffix) {
        if (isClasspathPath(path)) {
            PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            String locationPattern = path.replace(CLASSPATH_SCHEME_TO_REPLACE, CLASSPATH_SCHEME) + "/**/*" + suffix;
            org.springframework.core.io.Resource[] resources;
            try {
                resources = resolver.getResources(locationPattern);
            } catch (IOException e) {
                resources = null;
                e.printStackTrace();
            }
            return convertToCucumberIterator(resources);
        } else {
            return fs.resources(path, suffix);
        }
    }
    private Iterable<Resource> convertToCucumberIterator(org.springframework.core.io.Resource[] resources) {
        List<Resource> results = new ArrayList<Resource>();
        for (org.springframework.core.io.Resource resource : resources) {
            results.add(new ResourceAdapter(resource));
        }
        return results;
    }

    public static String packageName(String gluePath) {
        if (isClasspathPath(gluePath)) {
            gluePath = stripClasspathPrefix(gluePath);
        }
        return gluePath.replace('/', '.').replace('\\', '.');
    }

    private static boolean isClasspathPath(String path) {
        if (path.startsWith(CLASSPATH_SCHEME_TO_REPLACE)) {
            path = path.replace(CLASSPATH_SCHEME_TO_REPLACE, CLASSPATH_SCHEME);
        }
        return path.startsWith(CLASSPATH_SCHEME);
    }

    private static String stripClasspathPrefix(String path) {
        if (path.startsWith(CLASSPATH_SCHEME_TO_REPLACE)) {
            path = path.replace(CLASSPATH_SCHEME_TO_REPLACE, CLASSPATH_SCHEME);
        }
        return path.substring(CLASSPATH_SCHEME.length());
    }

}

步骤2::在org.springframework.core.io.Resource和Cucumber.runtime.io.Resource之间创建一个适配器:

Step2: Create an adapter between org.springframework.core.io.Resource and cucumber.runtime.io.Resource:

package cucumber.runtime.io;

import java.io.IOException;
import java.io.InputStream;

public class ResourceAdapter implements Resource {
    org.springframework.core.io.Resource springResource;

    public ResourceAdapter(org.springframework.core.io.Resource springResource) {
        this.springResource = springResource;
    }

    public String getPath() {
        try {
            return springResource.getFile().getPath();
        } catch (IOException e) {
            try {
                return springResource.getURL().toString();
            } catch (IOException e1) {
                e1.printStackTrace();
                return "";
            }
        }
    }

    public String getAbsolutePath() {
        try {
            return springResource.getFile().getAbsolutePath();
        } catch (IOException e) {
            return null;
        }
    }

    public InputStream getInputStream() throws IOException {
        return this.springResource.getInputStream();
    }

    public String getClassName(String extension) {

        String path = this.getPath();
        if (path.startsWith("jar:")) {
            path = path.substring(path.lastIndexOf("!") + 2);
            return path.substring(0, path.length() - extension.length()).replace('/', '.');
        } else {
            path = path.substring(path.lastIndexOf("classes") + 8);
            return path.substring(0, path.length() - extension.length()).replace('\\', '.');
        }

    }
}

步骤3:创建使用CustomMultiLoader的自定义主类:

Step3: Create your custom main class that uses your CustomMultiLoader:

package cucumber.runtime.io;

import static java.util.Arrays.asList;

import java.io.IOException;
import java.util.ArrayList;

import cucumber.runtime.ClassFinder;
import cucumber.runtime.Runtime;
import cucumber.runtime.RuntimeOptions;

public class CucumberStaticRunner {

    public static void startTests(String[] argv) throws Throwable {
        byte exitstatus = run(argv, Thread.currentThread().getContextClassLoader());
        System.exit(exitstatus);
    }

    public static byte run(String[] argv, ClassLoader classLoader) throws IOException {
        RuntimeOptions runtimeOptions = new RuntimeOptions(new ArrayList<String>(asList(argv)));

        ResourceLoader resourceLoader = new CustomMultiLoader(classLoader);
        ClassFinder classFinder = new ResourceLoaderClassFinder(resourceLoader, classLoader);
        Runtime runtime = new Runtime(resourceLoader, classFinder, classLoader, runtimeOptions);
        runtime.run();
        return runtime.exitStatus();
    }
}

第4步:调用自定义的主类,而不是cucumber.api.cli.Main.main:

Step4: Call your custom main class instead of cucumber.api.cli.Main.main:

String[] cucumberOptions = { "--glue", "my.test.pack", "--no-dry-run", "--monochrome", "classpath:features" };
CucumberStaticRunner.startTests(cucumberOptions);

这篇关于黄瓜:从Spring Boot jar运行时找不到后端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 23:29