开发第一个 GraalVM 原生应用
原文:https://docs.spring.io/spring-boot/how-to/native-image/developing-your-first-application.html (Spring Boot 4.2.0)
示例应用
可以使用标准的 Spring Boot Web 应用(如 "Hello World!" 示例)作为构建原生镜像的基础。应用需要添加 AOT 处理依赖和 spring-boot-starter-web 等。
使用 Buildpacks 构建原生镜像
系统要求
需要安装 Docker 或使用支持的构建平台。需要 Java 25 或更高版本(或 GraalVM)。
使用 Maven
使用 spring-boot-maven-plugin 的 build-image 目标,并设置环境变量 BP_NATIVE_IMAGE=true:
$ mvn -Pnative spring-boot:build-image
使用 Gradle
使用 Gradle 插件的 bootBuildImage 任务并配置 buildpack 环境。
运行示例
构建完成后,可以使用 docker run 运行生成的原生镜像。
使用 Native Build Tools 构建原生镜像
前置条件
需要 GraalVM JDK 25 或更高版本,以及 native-image 工具。Linux 和 macOS 需要安装相应的 native-image 组件,Windows 需要 Visual Studio 构建工具。
使用 Maven
添加 native-maven-plugin 并使用 -Pnative profile:
$ mvn -Pnative native:compile
使用 Gradle
添加 org.graalvm.buildtools.native 插件并运行 nativeCompile 任务。