提前處理
Spring AOT 是一個在構建時分析應用程式並生成最佳化版本的過程。它是將 Spring ApplicationContext 執行在原生映象中的必要步驟。
| 有關 Spring Boot 中 GraalVM 原生映象支援的概述,請查閱參考文件。 |
Spring Boot Maven 外掛提供了可用於對應用程式和測試程式碼執行 AOT 處理的目標。
處理應用程式
要配置您的應用程式使用此功能,請新增 process-aot 目標的執行,如以下示例所示
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>process-aot</id>
<goals>
<goal>process-aot</goal>
</goals>
</execution>
</executions>
</plugin>
由於 BeanFactory 在構建時完全準備好,條件也會被評估。這與常規 Spring Boot 應用程式在執行時所做的有重要區別。例如,如果您想選擇加入或退出某些功能,您需要在構建時配置環境。出於這個原因,process-aot 目標與 run 目標共享許多屬性。
使用原生配置檔案
如果您的專案使用 spring-boot-starter-parent 作為 parent,則可以使用 native 配置檔案來簡化構建原生映象所需的步驟。
native 配置檔案配置了以下內容
-
當 Spring Boot Maven 外掛應用於專案時,執行
process-aot。 -
適當的設定,以便 build-image 生成原生映象。
-
適用於 原生構建工具 Maven 外掛的合理預設值,特別是
-
確保外掛使用原始類路徑,而不是主 jar 檔案,因為它不理解我們重新打包的 jar 格式。
-
驗證是否存在合適的 GraalVM 版本。
-
下載第三方可達性元資料。
-
|
使用原始類路徑意味著原生映象不知道生成的 |
為了利用 native 配置檔案,表示應用程式的模組應該定義兩個外掛,如以下示例所示
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
單個專案可以使用 Cloud Native Buildpacks 或 Native Image Build Tools 在命令列上觸發原生映象的生成。
要在多模組專案中使用 native 配置檔案,您可以建立 native 配置檔案的自定義項,以便它呼叫您偏好的技術。
要在 package 階段繫結 Cloud Native Buildpacks,請將以下內容新增到您的多模組專案的根 POM 中
<profile>
<id>native</id>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>build-image</id>
<goals>
<goal>build-image-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
以下示例為原生構建工具執行相同的操作
<profile>
<id>native</id>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<executions>
<execution>
<id>build-image</id>
<goals>
<goal>compile-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
完成上述操作後,您可以構建多模組專案並在相關子模組中生成原生映象,如以下示例所示
$ mvn package -Pnative
| “相關”子模組是表示 Spring Boot 應用程式的模組。此類模組必須如上所述定義原生構建工具和 Spring Boot 外掛。 |
spring-boot:process-aot
org.springframework.boot:spring-boot-maven-plugin:4.0.0
在應用程式上呼叫 AOT 引擎。
必需引數
| 名稱 | 型別 | 預設值 |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
引數詳情
classesDirectory
包含應打包到歸檔中的類檔案和資原始檔的目錄。
名稱 |
|
|---|---|
型別 |
|
預設值 |
|
使用者屬性 |
|
自 |
compilerArguments
應提供給 AOT 編譯過程的引數。在命令列上,請務必將多個值用引號括起來。
名稱 |
|
|---|---|
型別 |
|
預設值 |
|
使用者屬性 |
|
自 |
excludeGroupIds
要排除的 groupId 名稱的逗號分隔列表(精確匹配)。
名稱 |
|
|---|---|
型別 |
|
預設值 |
|
使用者屬性 |
|
自 |
|
excludes
要排除的 artifact 定義集合。Exclude 元素定義了強制的 groupId 和 artifactId 元件以及可選的 classifier 元件。當配置為屬性時,值應以逗號分隔,元件以冒號分隔:groupId:artifactId,groupId:artifactId:classifier
名稱 |
|
|---|---|
型別 |
|
預設值 |
|
使用者屬性 |
|
自 |
|
generatedClasses
包含生成類的目錄。
名稱 |
|
|---|---|
型別 |
|
預設值 |
|
使用者屬性 |
|
自 |
generatedResources
包含生成資源的目錄。
名稱 |
|
|---|---|
型別 |
|
預設值 |
|
使用者屬性 |
|
自 |
generatedSources
包含生成源的目錄。
名稱 |
|
|---|---|
型別 |
|
預設值 |
|
使用者屬性 |
|
自 |
includes
要包含的 artifact 定義集合。Include 元素定義了強制的 groupId 和 artifactId 元件以及可選的 classifier 元件。當配置為屬性時,值應以逗號分隔,元件以冒號分隔:groupId:artifactId,groupId:artifactId:classifier
名稱 |
|
|---|---|
型別 |
|
預設值 |
|
使用者屬性 |
|
自 |
|
jvmArguments
應與 AOT 過程關聯的 JVM 引數。在命令列上,請務必將多個值用引號括起來。
名稱 |
|
|---|---|
型別 |
|
預設值 |
|
使用者屬性 |
|
自 |
處理測試
AOT 引擎可以應用於使用 Spring 測試上下文框架的 JUnit 5 測試。這些測試由 AOT 引擎處理,然後以原生映象執行。
與生產程式碼一樣,spring-boot-starter-parent 定義了一個 nativeTest 配置檔案,可用於簡化在原生映象中執行測試所需的步驟。
nativeTest 配置檔案配置了以下內容
-
當 Spring Boot Maven 外掛應用於專案時,執行
process-test-aot。 -
當 原生構建工具 Maven 外掛應用於專案時,執行
test。執行定義了合理的預設值,特別是-
確保外掛使用原始類路徑,而不是主 jar 檔案,因為它不理解我們重新打包的 jar 格式。
-
驗證是否存在合適的 GraalVM 版本。
-
下載第三方可達性元資料。
-
為了利用 nativeTest 配置檔案,表示應用程式的模組應該定義兩個外掛,如以下示例所示
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
一旦上述配置應用於每個需要此功能的模組,您就可以構建多模組專案並在相關子模組中以原生映象執行測試,如以下示例所示
$ mvn test -PnativeTest
與應用程式 AOT 處理一樣,BeanFactory 在構建時完全準備好。 |
spring-boot:process-test-aot
org.springframework.boot:spring-boot-maven-plugin:4.0.0
在測試上呼叫 AOT 引擎。
必需引數
| 名稱 | 型別 | 預設值 |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
引數詳情
classesDirectory
包含應用於執行測試的類檔案和資原始檔的目錄。
名稱 |
|
|---|---|
型別 |
|
預設值 |
|
使用者屬性 |
|
自 |
compilerArguments
應提供給 AOT 編譯過程的引數。在命令列上,請務必將多個值用引號括起來。
名稱 |
|
|---|---|
型別 |
|
預設值 |
|
使用者屬性 |
|
自 |
excludeGroupIds
要排除的 groupId 名稱的逗號分隔列表(精確匹配)。
名稱 |
|
|---|---|
型別 |
|
預設值 |
|
使用者屬性 |
|
自 |
|
excludes
要排除的 artifact 定義集合。Exclude 元素定義了強制的 groupId 和 artifactId 元件以及可選的 classifier 元件。當配置為屬性時,值應以逗號分隔,元件以冒號分隔:groupId:artifactId,groupId:artifactId:classifier
名稱 |
|
|---|---|
型別 |
|
預設值 |
|
使用者屬性 |
|
自 |
|
generatedClasses
包含生成測試類的目錄。
名稱 |
|
|---|---|
型別 |
|
預設值 |
|
使用者屬性 |
|
自 |
generatedResources
包含生成測試資源的目錄。
名稱 |
|
|---|---|
型別 |
|
預設值 |
|
使用者屬性 |
|
自 |
generatedSources
包含生成源的目錄。
名稱 |
|
|---|---|
型別 |
|
預設值 |
|
使用者屬性 |
|
自 |
generatedTestClasses
包含生成測試類的目錄。
名稱 |
|
|---|---|
型別 |
|
預設值 |
|
使用者屬性 |
|
自 |
includes
要包含的 artifact 定義集合。Include 元素定義了強制的 groupId 和 artifactId 元件以及可選的 classifier 元件。當配置為屬性時,值應以逗號分隔,元件以冒號分隔:groupId:artifactId,groupId:artifactId:classifier
名稱 |
|
|---|---|
型別 |
|
預設值 |
|
使用者屬性 |
|
自 |
|
jvmArguments
應與 AOT 過程關聯的 JVM 引數。在命令列上,請務必將多個值用引號括起來。
名稱 |
|
|---|---|
型別 |
|
預設值 |
|
使用者屬性 |
|
自 |