在GraalVM上编译Spring Native失败:native-image.cmd返回非零结果
构建Spring Native项目时native-image编译失败问题排查
问题描述
按照Spring官方文档构建Spring Native项目,执行编译命令mvn -Pnative -DskipTests package -X时失败,提示native-image.cmd返回非零结果,核心错误为Fatal error: Unsupported OptionOrigin: C:\Users\ADMINI~1\AppData\Local\Temp\native-image13053469568633812036args。需确认该问题是否与GraalVM版本有关,以及Spring官方支持的适配版本。
环境信息
操作系统与系统配置
Windows 10 X64 10.0.17763 Build 17763 RAM 28GB
JDK信息
$ java -version openjdk version "17.0.3" 2022-04-19 OpenJDK Runtime Environment GraalVM CE 22.1.0 (build 17.0.3+7-jvmci-22.1-b06) OpenJDK 64-Bit Server VM GraalVM CE 22.1.0 (build 17.0.3+7-jvmci-22.1-b06, mixed mode, sharing)
Maven信息
$ mvn --version Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63) Maven home: E:\Appl\apache-maven-3.8.6 Java version: 17.0.3, vendor: GraalVM Community, runtime: C:\Program Files\Java\graalvm-ce-java17-22.1.0 Default locale: zh_CN, platform encoding: GBK OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
项目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 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>2.7.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>rest-service-complete</artifactId> <version>0.0.1-SNAPSHOT</version> <name>rest-service-complete</name> <description>Demo project for Spring Boot</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-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.experimental</groupId> <artifactId>spring-native</artifactId> <version>0.12.1</version> </dependency> </dependencies> <build> <plugins> <!-- ... --> <plugin> <groupId>org.springframework.experimental</groupId> <artifactId>spring-aot-maven-plugin</artifactId> <version>0.12.1</version> <executions> <execution> <id>generate</id> <goals> <goal>generate</goal> </goals> </execution> <execution> <id>test-generate</id> <goals> <goal>test-generate</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <profiles> <profile> <id>native</id> <dependencies> <!-- Required with Maven Surefire 2.x --> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.graalvm.buildtools</groupId> <artifactId>native-maven-plugin</artifactId> <version>0.9.13</version> <extensions>true</extensions> <executions> <execution> <id>build-native</id> <goals> <goal>build</goal> </goals> <phase>package</phase> </execution> <execution> <id>test-native</id> <goals> <goal>test</goal> </goals> <phase>test</phase> </execution> </executions> <configuration> <!-- ... --> </configuration> </plugin> <!-- Avoid a clash between Spring Boot repackaging and native-maven-plugin --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <classifier>exec</classifier> </configuration> </plugin> </plugins> </build> </profile> </profiles> <repositories> <!-- ... --> <repository> <id>spring-release</id> <name>Spring release</name> <url>https://repo.spring.io/release</url> </repository> </repositories> <pluginRepositories> <!-- ... --> <pluginRepository> <id>spring-release</id> <name>Spring release</name> <url>https://repo.spring.io/release</url> </pluginRepository> </pluginRepositories> </project>
执行命令与错误详情
执行命令
mvn -Pnative -DskipTests package -X
终端错误输出
Fatal error: Unsupported OptionOrigin: C:\Users\ADMINI~1\AppData\Local\Temp\native-image13053469568633812036args [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 15.740 s [INFO] Finished at: 2022-09-30T14:59:03+08:00 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.graalvm.buildtools:native-maven-plugin:0.9.13:build (build-native) on project rest-service-complete: Execution of C:\Program Files\Java\graalvm-ce-java17-22.1.0\bin\native-image.cmd @C:\Users\ADMINI~1\AppData\Local\Temp\native-image13053469568633812036args returned non-zero result -> [Help 1] org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.graalvm.buildtools:native-maven-plugin:0.9.13:build (build-native) on project rest-service-complete: Execution of C:\Program Files\Java\graalvm-ce-java17-22.1.0\bin\native-image.cmd @C:\Users\ADMINI~1\AppData\Local\Temp\native-image13053469568633812036args returned non-zero result at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:375) at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:351) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:171) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:163) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81) at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128) at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:294) at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192) at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105) at org.apache.maven.cli.MavenCli.execute (MavenCli.java:960) at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:293) at org.apache.maven.cli.MavenCli.main (MavenCli.java:196) at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:77) at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke (Method.java:568) at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282) at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225) at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406) at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347) Caused by: org.apache.maven.plugin.MojoExecutionException: Execution of C:\Program Files\Java\graalvm-ce-java17-22.1.0\bin\native-image.cmd @C:\Users\ADMINI~1\AppData\Local\Temp\native-image13053469568633812036args returned non-zero result at org.graalvm.buildtools.maven.AbstractNativeMojo.buildImage (AbstractNativeMojo.java:415) at org.graalvm.buildtools.maven.NativeBuildMojo.execute (NativeBuildMojo.java:91) at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137) at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:370) at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:351) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:171) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:163) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81) at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128) at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:294) at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192) at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105) at org.apache.maven.cli.MavenCli.execute (MavenCli.java:960) at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:293) at org.apache.maven.cli.MavenCli.main (MavenCli.java:196) at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:77) at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke (Method.java:568) at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282) at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225) at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406) at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347) [ERROR] [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
解决方案
1. 版本兼容性问题确认
该问题确实与GraalVM版本直接相关:
- 当前使用的Spring Native 0.12.1对应Spring Boot 2.7.x,官方要求适配的GraalVM版本为22.0.0.2(Java 17),你使用的GraalVM CE 22.1.0版本不匹配,导致了
Unsupported OptionOrigin错误。 - Spring Native与GraalVM版本绑定严格,不同版本的Spring Native仅支持特定范围的GraalVM版本:
- Spring Native 0.12.x → GraalVM 22.0.0.2
- Spring Native 0.11.x → GraalVM 21.3.x
- 集成到Spring Boot 3.x的正式Native功能,支持GraalVM 22.3+
2. 具体修复步骤
- 降级GraalVM版本:卸载当前的GraalVM CE 22.1.0,安装GraalVM CE 22.0.0.2(Java 17版本),并确保
JAVA_HOME和PATH环境变量指向该版本。 - 统一Java版本:pom.xml中
java.version设置为1.8,但使用的是Java 17的GraalVM,存在版本冲突。修改为:<properties> <java.version>17</java.version> </properties> - 重新编译:清理构建缓存后重新执行编译命令:
mvn clean -Pnative -DskipTests package -X
内容的提问来源于stack exchange,提问作者DongjianPeng
相关产品推荐
相关产品推荐

