1

这是关于 spring native 的,在我正在处理的每个 web Flux 项目中,我都面临着同样的问题。我无法生成本机图像。有人可以帮忙吗?

我正在使用 Mac (Catalina 10.15.6 (19G73)) 并使用 OpenJDK 运行时环境 GraalVM CE 21.0.0.2 (build 11.0.10+8-jvmci-21.0-b06)

我得到的错误是:

致命错误:java.lang.NoClassDefFoundError: org/springframework/web/servlet/config/annotation/WebMvcConfigurer

错误:图像构建请求失败,退出状态为 1

这是我的依赖项:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath />
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.experimental</groupId>
            <artifactId>spring-native</artifactId>
        </dependency>

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

        <dependency>
            <groupId>com.nimbusds</groupId>
            <artifactId>nimbus-jose-jwt</artifactId>
            <version>9.9</version>
        </dependency>

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

    <properties>
        <!-- Customize Spring Boot classifier, useful in order to avoid a clash between Spring Boot repackaging and native-image-maven-plugin -->
        <classifier/>
        <!-- Customize native-image arguments with this property -->
        <native.build.args></native.build.args>
        <!-- `tiny` builder allows small footprint and reduced surface attack, you can also use `base` (the default) or `full` builders to have more tooling available in the image for an improved developer experience -->
        <!-- TODO Bring back "paketobuildpacks/builder:tiny" before 0.10.0 release -->
        <builder>dmikusa/graalvm-tiny</builder>
        <spring-native.version>0.9.2</spring-native.version>

        <project.encondig>UTF-8</project.encondig>
        <project.build.sourceEncoding>${project.encondig}</project.build.sourceEncoding>
        <project.reporting.outputEncoding>${project.encondig}</project.reporting.outputEncoding>

        <java.version>11</java.version>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <maven.compiler.source>${java.version}</maven.compiler.source>

        <spring-cloud.version>2020.0.2</spring-cloud.version>
        <maven.compiler.plugin.version>2.3.2</maven.compiler.plugin.version>
        <maven.resources.plugin.version>2.6</maven.resources.plugin.version>
        <maven.dependency.plugin.version>2.8</maven.dependency.plugin.version>
        <maven.deploy.plugin.version>2.7</maven.deploy.plugin.version>
        <maven.release.plugin.version>2.5.3</maven.release.plugin.version>
        <maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>

        <jackson.version>2.12.0</jackson.version>
        <jackson-bom.version>${jackson.version}</jackson-bom.version>
        <jacoco.version>0.8.5</jacoco.version>
        <kotlin.version>1.4.20-RC</kotlin.version>
    </properties>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <classifier>${classifier}</classifier>
                        <image>
                            <builder>${builder}</builder>
                            <env>
                                <BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>
                                <BP_NATIVE_IMAGE_BUILD_ARGUMENTS>${native.build.args}</BP_NATIVE_IMAGE_BUILD_ARGUMENTS>
                            </env>
                        </image>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.springframework.experimental</groupId>
                    <artifactId>spring-aot-maven-plugin</artifactId>
                    <version>${spring-native.version}</version>
                    <executions>
                        <execution>
                            <id>test-generate</id>
                            <goals>
                                <goal>test-generate</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>generate</id>
                            <goals>
                                <goal>generate</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.graalvm.nativeimage</groupId>
                    <artifactId>native-image-maven-plugin</artifactId>
                    <version>21.1.0</version>
                    <configuration>
                        <imageName>${project.artifactId}</imageName>
                        <buildArgs>${native.build.args}</buildArgs>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>native-image</goal>
                            </goals>
                            <phase>package</phase>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.experimental</groupId>
                <artifactId>spring-native</artifactId>
                <version>${spring-native.version}</version>
            </dependency>
            <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>

    <profiles>
        <!-- Enable building a native image using a local installation of native-image with GraalVM native-image-maven-plugin -->
        <profile>
            <id>native-image</id>
            <properties>
                <!-- Avoid a clash between Spring Boot repackaging and native-image-maven-plugin -->
                <classifier>exec</classifier>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.graalvm.nativeimage</groupId>
                        <artifactId>native-image-maven-plugin</artifactId>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
4

1 回答 1

2

我对此不满意,但这是一种解决方法。在 pom.xml 文件中添加以下代码:

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

并在属性文件中添加以下属性:

spring.main.web-application-type=REACTIVE

于 2021-05-06T21:31:33.333 回答