从资源中读取文件 - 路径必须存在:/app/app.jar!/BOOT-INF/classes!/features/

Reading file from resource - path must exist: /app/app.jar!/BOOT-INF/classes!/features/

我需要文件的类路径。我找到了使用 classpath

获取路径的方法
String filePath = com.google.common.io.Resources.getResource(feature).getFile();

我在我的服务器上部署了我的应用程序。用黄瓜执行我的代码时出现此错误。

path must exist: /app/app.jar!/BOOT-INF/classes!/features/attachment/heal.feature

我检查了 target 文件夹中的 app.jar,但无法提取和查看内容

我正在使用 pring boot 2,这是 pom

<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>pmc.aop.core</groupId>
<artifactId>pmc-integration-tests</artifactId>
<version>0.1.5-${BUILD_NUMBER}</version>
<packaging>jar</packaging>


<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.1.RELEASE</version>
</parent>

<properties>
    <java.version>11</java.version>
    <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
    <spring-boot.version>2.2.1.RELEASE</spring-boot.version>
    <drools.version>7.35.0.Final</drools.version>
    <kie.version>7.35.0.Final</kie.version>
    <lombok.version>1.16.22</lombok.version>
    <cucumber-version>5.7.0</cucumber-version>
</properties>

<dependencies>

    <dependency>
        <groupId>pnc.aop.core</groupId>
        <artifactId>aop-lib-kafka-crypto</artifactId>
        <version>0.4.0-1</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-stream-binder-kafka</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-stream</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.10</version>
    </dependency>

    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>3.12.4</version>
    </dependency>


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

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

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

    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>${cucumber-version}</version>
    </dependency>

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

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

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>

</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>

    <finalName>app</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${spring-boot.version}</version>
            <configuration>
                <executable>true</executable>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.jfrog.buildinfo</groupId>
            <artifactId>artifactory-maven-plugin</artifactId>
            <version>2.6.1</version>
        </plugin>

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.5</version>
            <configuration>

            </configuration>
            <executions>
                <execution>
                    <id>prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>report</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
                <execution>
                    <id>post-unit-test</id>
                    <phase>test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.0</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>

                <annotationProcessorPaths>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>${lombok.version}</version>
                    </path>
                </annotationProcessorPaths>

            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
        </plugin>


    </plugins>

</build>

看起来 jar 已损坏

I need the classpath of a file. I found this way to get the path using classpath

String filePath = >com.google.common.io.Resources.getResource(feature).getFile();

I deployed my app on my server. Executing my code with cucumber I got this error.

path must exist: /app/app.jar!/BOOT-INF/classes!/features/attachment/heal.feature

文件路径与类路径不同。如果您查看 spring executable jar format,您会看到与常规 jar 文件不同的布局:

example.jar
 |
 +-META-INF
 |  +-MANIFEST.MF
 +-org
 |  +-springframework
 |     +-boot
 |        +-loader
 |           +-<spring boot loader classes>
 +-BOOT-INF
    +-classes
    |  +-mycompany
    |     +-project
    |        +-YourClasses.class
    +-lib
       +-dependency1.jar
       +-dependency2.jar

BOOT-INF/classesBOOT-INF/lib的内容在类路径上。所以你可以使用classpath:features/attachment/heal.feature作为你的功能路径。

请注意 Cucumber 只有 limited support for scanning spring jar files