Spring Boot 3.2.4原生构建(GraalVM)遇两类问题求助
问题描述
我正按照《Introducing GraalVM Native Images》文档,尝试将Spring Boot 3.2.4版本的spring-cloud-function-adapter-aws Lambda函数从jar包转换为x86_64原生二进制文件,但遇到了问题。目前多数教程仍使用旧的org.springframework.experimental依赖。
环境信息
- Windows 10系统
- 已安装带C++构建包的Visual Studio 2022
- GraalVM 22版本,Java版本信息:
$ java --version java 22 2024-03-19 Java(TM) SE Runtime Environment Oracle GraalVM 22+36.1 (build 22+36-jvmci-b02) Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 22+36.1 (build 22+36-jvmci-b02, mixed mode, sharing)
POM配置
<?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 https://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>3.2.4</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.bar.aws</groupId> <artifactId>native-hello</artifactId> <version>0.0.1-SNAPSHOT</version> <name>native-hello</name> <description>native-hello</description> <properties> <java.version>22</java.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-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <finalName>${artifactId}</finalName> <plugins> <plugin> <groupId>org.graalvm.buildtools</groupId> <artifactId>native-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <imageName>${artifactId}</imageName> <docker> <host>tcp://localhost:2375</host> </docker> </configuration> </plugin> </plugins> </build> </project>
遇到的错误
错误1:执行./mvnw -Pnative spring-boot:build-image报错
$ ./mvnw -Pnative spring-boot:build-image Execution default-cli of goal org.springframework.boot:spring-boot-maven-plugin:3.2.4:build-image failed: Host name must not be empty
错误2:执行mvn -Pnative native:compile报错
$ mvn -Pnative native:compile [INFO] --- native:0.9.28:compile (default-cli) @ native-hello --- [INFO] Found GraalVM installation from JAVA_HOME variable. [INFO] [graalvm reachability metadata repository for ch.qos.logback:logback-classic:1.4.14]: Configuration directory not found. Trying latest version. [INFO] [graalvm reachability metadata repository for ch.qos.logback:logback-classic:1.4.14]: Configuration directory is ch.qos.logback\logback-classic\1.4.1 [INFO] [graalvm reachability metadata repository for com.fasterxml.jackson.core:jackson-databind:2.15.4]: Configuration directory not found. Trying latest version. [INFO] [graalvm reachability metadata repository for com.fasterxml.jackson.core:jackson-databind:2.15.4]: Configuration directory is com.fasterxml.jackson.core\jackson-databind\2.15.2 [INFO] [graalvm reachability metadata repository for org.apache.tomcat.embed:tomcat-embed-core:10.1.19]: Configuration directory not found. Trying latest version. [INFO] [graalvm reachability metadata repository for org.apache.tomcat.embed:tomcat-embed-core:10.1.19]: Configuration directory is org.apache.tomcat.embed\tomcat-embed-core\10.0.20 [INFO] Executing: C:\sfw\java\graalvm-jdk\bin\native-image.cmd @target\tmp\native-image-4912974344800363771.args Warning: The option '-H:ResourceConfigurationResources=META-INF/native-image/org.apache.tomcat.embed/tomcat-embed-core/tomcat-resource.json' is experimental and must be enabled via '-H:+UnlockExperimentalVMOptions' in the future. Warning: The option '-H:ReflectionConfigurationResources=META-INF/native-image/org.apache.tomcat.embed/tomcat-embed-core/tomcat-reflection.json' is experimental and must be enabled via '-H:+UnlockExperimentalVMOptions' in the future. Warning: The option '-H:ReflectionConfigurationResources=META-INF/native-image/org.apache.tomcat.embed/tomcat-embed-websocket/tomcat-reflection.json' is experimental and must be enabled via '-H:+UnlockExperimentalVMOptions' in the future. Warning: The option '-H:ReflectionConfigurationResources=META-INF/native-image/org.apache.tomcat.embed/tomcat-embed-el/tomcat-reflection.json' is experimental and must be enabled via '-H:+UnlockExperimentalVMOptions' in the future. Warning: The option '-H:ResourceConfigurationResources=META-INF/native-image/org.apache.tomcat.embed/tomcat-embed-el/tomcat-resource.json' is experimental and must be enabled via '-H:+UnlockExperimentalVMOptions' in the future. Warning: The option '-H:ResourceConfigurationResources=META-INF/native-image/org.apache.tomcat.embed/tomcat-embed-websocket/tomcat-resource.json' is experimental and must be enabled via '-H:+UnlockExperimentalVMOptions' in the future. Warning: Please re-evaluate whether any experimental option is required, and either remove or unlock it. The build output lists all active experimental options, including where they come from and possible alternatives. If you think an experimental option should be considered as stable, please file an issue. Error: Failed to automatically set up Windows build environment. Please make sure that Visual Studio 2022 version 17.1.0 or later is installed on your system. You can download it at https://visualstudio.microsoft.com/downloads/. If this error persists, please try and run GraalVM Native Image in an x64 Native Tools Command Prompt or file a ticket. [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 20.976 s [INFO] Finished at: 2024-04-02T11:42:32-06:00 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.graalvm.buildtools:native-maven-plugin:0.9.28:compile (default-cli) on project native-hello: Execution of C:\sfw\java\graalvm-jdk\bin\native-image.cmd @target\tmp\native-image-4912974344800363771.args returned non-zero result
解决方案
针对错误1:spring-boot:build-image 主机名不能为空
这个错误源于Docker主机配置问题,解决步骤:
- 移除无效的Docker配置:如果不需要通过Docker构建镜像,直接删除POM中
spring-boot-maven-plugin下的<docker>块:<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <imageName>${artifactId}</imageName> <!-- 移除以下Docker配置 --> <!-- <docker> <host>tcp://localhost:2375</host> </docker> --> </configuration> </plugin> - 启用Docker TCP端口(如需保留Docker构建):打开Docker Desktop,进入Settings > General,勾选"Expose daemon on tcp://localhost:2375 without TLS",确保Docker正在运行后重新执行命令。
针对错误2:native:compile 无法设置Windows构建环境
GraalVM需要Visual Studio的C++工具链环境,解决步骤:
- 使用专用命令行窗口:打开开始菜单,找到Visual Studio 2022文件夹,运行"x64 Native Tools Command Prompt for VS 2022",在该窗口中切换到项目目录,重新执行
mvn -Pnative native:compile。 - 手动加载环境变量:若不想使用专用窗口,可执行Visual Studio的环境脚本:
执行后再运行Maven构建命令。"C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\VsDevCmd.bat" -arch=x64 - 验证Visual Studio组件:通过Visual Studio Installer确认已安装"Desktop development with C++"工作负载,包含MSVC编译器、Windows SDK等核心组件,缺失则补充安装。
补充:添加Lambda适配依赖
你的POM中缺少spring-cloud-function-adapter-aws依赖,需添加适配Spring Boot 3.2.x的版本:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-function-adapter-aws</artifactId> <version>4.1.1</version> </dependency>
内容的提问来源于stack exchange,提问作者B Randall
相关产品推荐
相关产品推荐

