提前處理

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 版本。

    • 下載第三方可達性元資料。

使用原始類路徑意味著原生映象不知道生成的 MANIFEST.MF。如果需要在原生映象中讀取清單內容,例如獲取應用程式的實現版本,請配置 classesDirectory 選項以使用常規 jar。

為了利用 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 BuildpacksNative 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

檔案

${project.build.outputDirectory}

generatedClasses

檔案

${project.build.directory}/spring-aot/main/classes

generatedResources

檔案

${project.build.directory}/spring-aot/main/resources

generatedSources

檔案

${project.build.directory}/spring-aot/main/sources

可選引數

名稱 型別 預設值

arguments

String[]

compilerArguments

字串

excludeGroupIds

字串

excludes

List

includes

List

jvmArguments

字串

mainClass

字串

profiles

String[]

skip

布林值

systemPropertyVariables

Map

引數詳情

arguments

應在 AOT 處理中考慮的應用程式引數。

名稱

arguments

型別

java.lang.String[]

預設值

使用者屬性

classesDirectory

包含應打包到歸檔中的類檔案和資原始檔的目錄。

名稱

classesDirectory

型別

java.io.File

預設值

${project.build.outputDirectory}

使用者屬性

compilerArguments

應提供給 AOT 編譯過程的引數。在命令列上,請務必將多個值用引號括起來。

名稱

compilerArguments

型別

java.lang.String

預設值

使用者屬性

spring-boot.aot.compilerArguments

excludeGroupIds

要排除的 groupId 名稱的逗號分隔列表(精確匹配)。

名稱

excludeGroupIds

型別

java.lang.String

預設值

使用者屬性

spring-boot.excludeGroupIds

1.1.0

excludes

要排除的 artifact 定義集合。Exclude 元素定義了強制的 groupIdartifactId 元件以及可選的 classifier 元件。當配置為屬性時,值應以逗號分隔,元件以冒號分隔:groupId:artifactId,groupId:artifactId:classifier

名稱

excludes

型別

java.util.List

預設值

使用者屬性

spring-boot.excludes

1.1.0

generatedClasses

包含生成類的目錄。

名稱

generatedClasses

型別

java.io.File

預設值

${project.build.directory}/spring-aot/main/classes

使用者屬性

generatedResources

包含生成資源的目錄。

名稱

generatedResources

型別

java.io.File

預設值

${project.build.directory}/spring-aot/main/resources

使用者屬性

generatedSources

包含生成源的目錄。

名稱

generatedSources

型別

java.io.File

預設值

${project.build.directory}/spring-aot/main/sources

使用者屬性

includes

要包含的 artifact 定義集合。Include 元素定義了強制的 groupIdartifactId 元件以及可選的 classifier 元件。當配置為屬性時,值應以逗號分隔,元件以冒號分隔:groupId:artifactId,groupId:artifactId:classifier

名稱

includes

型別

java.util.List

預設值

使用者屬性

spring-boot.includes

1.2.0

jvmArguments

應與 AOT 過程關聯的 JVM 引數。在命令列上,請務必將多個值用引號括起來。

名稱

jvmArguments

型別

java.lang.String

預設值

使用者屬性

spring-boot.aot.jvmArguments

mainClass

用作 AOT 過程源的主類的名稱。如果未指定,則將使用找到的第一個包含“main”方法的已編譯類。

名稱

mainClass

型別

java.lang.String

預設值

使用者屬性

spring-boot.aot.main-class

profiles

AOT 處理時要考慮的 Spring 配置檔案。

名稱

profiles

型別

java.lang.String[]

預設值

使用者屬性

skip

跳過執行。

名稱

skip

型別

布林值

預設值

使用者屬性

spring-boot.aot.skip

systemPropertyVariables

要傳遞給 AOT 程序的 JVM 系統屬性列表。

名稱

systemPropertyVariables

型別

java.util.Map

預設值

使用者屬性

處理測試

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

檔案

${project.build.outputDirectory}

generatedClasses

檔案

${project.build.directory}/spring-aot/main/classes

generatedResources

檔案

${project.build.directory}/spring-aot/test/resources

generatedSources

檔案

${project.build.directory}/spring-aot/test/sources

generatedTestClasses

檔案

${project.build.directory}/spring-aot/test/classes

testClassesDirectory

檔案

${project.build.testOutputDirectory}

可選引數

名稱 型別 預設值

compilerArguments

字串

excludeGroupIds

字串

excludes

List

includes

List

jvmArguments

字串

skip

布林值

systemPropertyVariables

Map

引數詳情

classesDirectory

包含應用於執行測試的類檔案和資原始檔的目錄。

名稱

classesDirectory

型別

java.io.File

預設值

${project.build.outputDirectory}

使用者屬性

compilerArguments

應提供給 AOT 編譯過程的引數。在命令列上,請務必將多個值用引號括起來。

名稱

compilerArguments

型別

java.lang.String

預設值

使用者屬性

spring-boot.aot.compilerArguments

excludeGroupIds

要排除的 groupId 名稱的逗號分隔列表(精確匹配)。

名稱

excludeGroupIds

型別

java.lang.String

預設值

使用者屬性

spring-boot.excludeGroupIds

1.1.0

excludes

要排除的 artifact 定義集合。Exclude 元素定義了強制的 groupIdartifactId 元件以及可選的 classifier 元件。當配置為屬性時,值應以逗號分隔,元件以冒號分隔:groupId:artifactId,groupId:artifactId:classifier

名稱

excludes

型別

java.util.List

預設值

使用者屬性

spring-boot.excludes

1.1.0

generatedClasses

包含生成測試類的目錄。

名稱

generatedClasses

型別

java.io.File

預設值

${project.build.directory}/spring-aot/main/classes

使用者屬性

generatedResources

包含生成測試資源的目錄。

名稱

generatedResources

型別

java.io.File

預設值

${project.build.directory}/spring-aot/test/resources

使用者屬性

generatedSources

包含生成源的目錄。

名稱

generatedSources

型別

java.io.File

預設值

${project.build.directory}/spring-aot/test/sources

使用者屬性

generatedTestClasses

包含生成測試類的目錄。

名稱

generatedTestClasses

型別

java.io.File

預設值

${project.build.directory}/spring-aot/test/classes

使用者屬性

includes

要包含的 artifact 定義集合。Include 元素定義了強制的 groupIdartifactId 元件以及可選的 classifier 元件。當配置為屬性時,值應以逗號分隔,元件以冒號分隔:groupId:artifactId,groupId:artifactId:classifier

名稱

includes

型別

java.util.List

預設值

使用者屬性

spring-boot.includes

1.2.0

jvmArguments

應與 AOT 過程關聯的 JVM 引數。在命令列上,請務必將多個值用引號括起來。

名稱

jvmArguments

型別

java.lang.String

預設值

使用者屬性

spring-boot.aot.jvmArguments

skip

跳過執行。

名稱

skip

型別

布林值

預設值

使用者屬性

spring-boot.aot.skip

systemPropertyVariables

要傳遞給 AOT 程序的 JVM 系統屬性列表。

名稱

systemPropertyVariables

型別

java.util.Map

預設值

使用者屬性

testClassesDirectory

包含應打包到歸檔中的類檔案和資原始檔的目錄。

名稱

testClassesDirectory

型別

java.io.File

預設值

${project.build.testOutputDirectory}

使用者屬性

© . This site is unofficial and not affiliated with VMware.