# Snow-Lang GraalVM AOT 打包指南 ## 1. 概述 本文档介绍如何使用 GraalVM 的 AOT(Ahead-of-Time)编译功能将一个 Snow-Lang 项目打包成原生可执行文件(Native Image)。 ## 2. 前置条件 1. 操作系统: Linux/macOS/Windows 2. Java 项目(Maven) 3. GraalVM(建议 24+ 版本) ## 3. 环境准备 ### 3.1 安装 GraalVM 1. 下载对应平台的 GraalVM Community 版本: [https://www.graalvm.org/downloads/](https://www.graalvm.org/downloads/) 2. 解压并配置环境变量: 3. 验证安装: ```bash java -version # 应显示 GraalVM 版本信息 java version "24.0.1" 2025-04-15 Java(TM) SE Runtime Environment Oracle GraalVM 24.0.1+9.1 (build 24.0.1+9-jvmci-b01) Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 24.0.1+9.1 (build 24.0.1+9-jvmci-b01, mixed mode, sharing) ``` ### 3.2 Windows 上 Native Image 的先决条件 在 Windows 上,Native Image 需要 Visual Studio 和 Microsoft Visual C++(MSVC)。 1. 从 [visualstudio.microsoft.com](https://visualstudio.microsoft.com/zh-hans/vs/) 下载 Visual Studio Build Tools 2022 或更高版本(C 开发环境)。 2. 通过打开下载的文件来启动安装,然后单击 **继续** ![IMG_VS_1.png](img/IMG_VS_1.png) 3. 在主窗口中选择 **使用 C++ 进行桌面开发** 复选框。在右侧的“安装详细信息”下,确保选择了两个要求,**Windows 11 SDK** 和 **MSVC (…) C++ x64/x86 构建工具**。单击 **安装** 继续。 ![IMG_VS_2.png](img/IMG_VS_2.png) 您现在能够使用 GraalVM Native Image 进行构建。 ## 4. Maven 项目配置文件 通过将以下配置文件添加到 `pom.xml` 中,为 Native Image 启用 Maven 插件: ```xml native-linux unix org.graalvm.buildtools native-maven-plugin ${native.maven.plugin.version} true build-native compile-no-fork package test-native test test --static --libc=musl --emit build-report -O2 /opt/musl-1.2.5/bin:${env.PATH} /opt/musl-1.2.5/include /opt/musl-1.2.5/lib native-windows Windows org.graalvm.buildtools native-maven-plugin ${native.maven.plugin.version} true build-native compile-no-fork package test-native test test --emit build-report -O2 ``` ## 5. 构建 Native Image 1. 确保项目已在 JVM 下通过测试。 2. 点击 `Maven` `生命周期` `package` ![IMG_Maven_Package_1.png](img/IMG_Maven_Package_1.png) 3. 等待 Native Image 构建完成: 这个过程可能较慢(数分钟)。 4. 可执行文件即可直接运行,无需 JVM。 > 生成的可执行文件位于 target/ 目录。