建置

本節說明如何建置 Spring Shell 應用程式。

原生支援

Spring Shell 應用程式編譯成 GraalVM 二進制檔案的支援,主要來自 Spring FrameworkSpring Boot,其中此功能稱為 AOT。「Ahead of Time」表示應用程式上下文在編譯時準備好,以便用於 GraalVM 生成。

Spring Shell 建構於框架的 AOT 功能之上,擁有自己的 GraalVM 配置,提供二進制檔案中應包含哪些內容的提示。通常問題來自尚未包含 GraalVM 相關配置或這些配置不完整的第三方函式庫。

需要使用 GraalVM Reachability Metadata Repository,它為第三方函式庫提供了一些遺失的提示。此外,您需要安裝 GraalVM 並將 JAVA_HOME 指向它。

對於 gradle,新增 graalvm 的 native plugin 並設定 metadata repository。

plugins {
	id 'org.graalvm.buildtools.native' version '0.9.16'
}

graalvmNative {
	metadataRepository {
        enabled = true
	}
}

當 gradle 建置以 ./gradlew nativeCompile 執行時,您應該會在 build/native/nativeCompile 目錄下取得二進制檔案。

對於 maven,使用 spring-boot-starter-parent 作為父項目,您將取得可用於編譯的 native 設定檔。您需要設定 metadata repository

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.graalvm.buildtools</groupId>
                <artifactId>native-maven-plugin</artifactId>
                <configuration>
                    <metadataRepository>
                        <enabled>true</enabled>
                    </metadataRepository>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
如果您依賴 spring-boot-starter-parent,它會管理 native-maven-plugin 版本,並保持在最新狀態。

當 maven 建置以 ./mvnw native:compile -Pnative 執行時,您應該會在 target 目錄下取得二進制檔案。

如果一切順利,這個二進制檔案可以直接執行,而無需透過 jvm 執行 boot 應用程式 jar 檔。