預先處理

Spring AOT 是一個在建置時期分析您的應用程式並產生最佳化版本的程序。這是在本機映像檔中執行 Spring ApplicationContext 的必要步驟。

如需 Spring Boot 中 GraalVM Native Images 支援的概觀,請查看參考文件

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 目標共享許多屬性。

使用 Native Profile

如果您使用 spring-boot-starter-parent 作為專案的 parent,則可以使用 native Profile 來簡化建置 Native Image 所需的步驟。

native Profile 組態了以下內容

  • 當 Spring Boot Maven 外掛程式應用於專案時,執行 process-aot

  • 適當的設定,以便 build-image 產生 Native Image。

  • Native Build Tools Maven 外掛程式的合理預設值,特別是

    • 確保外掛程式使用原始類別路徑,而不是主 jar 檔案,因為它不理解我們的重新打包 jar 格式。

    • 驗證可用的 GraalVM 版本是否合適。

    • 下載第三方可達性元數據。

使用原始類別路徑表示 Native Image 不知道產生的 MANIFEST。如果您需要在 Native Image 中讀取 manifest 的內容,例如獲取應用程式的實作版本,請組態 classesDirectory 選項以使用常規 jar。

若要受益於 native Profile,代表應用程式的模組應定義兩個外掛程式,如下列範例所示

<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 Image 的產生。

若要在多模組專案中使用 native Profile,您可以建立 native Profile 的自訂,使其調用您偏好的技術。

若要在 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>

以下範例針對 Native Build Tools 執行相同的操作

<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>

一旦完成上述操作,您就可以建置多模組專案,並在相關的子模組中產生 Native Image,如下列範例所示

$ mvn package -Pnative
「相關」子模組是代表 Spring Boot 應用程式的模組。此類模組必須如上所述定義 Native Build Tools 和 Spring Boot 外掛程式。

spring-boot:process-aot

org.springframework.boot:spring-boot-maven-plugin:3.3.5

在應用程式上調用 AOT 引擎。

必要參數

名稱 類型 預設值

classesDirectory

File

${project.build.outputDirectory}

generatedClasses

File

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

generatedResources

File

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

generatedSources

File

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

可選參數

名稱 類型 預設值

arguments

String[]

compilerArguments

String

excludeGroupIds

String

excludes

List

includes

List

jvmArguments

String

mainClass

String

profiles

String[]

skip

boolean

false

systemPropertyVariables

Map

參數詳細資訊

arguments

應用程式參數,應在 AOT 處理中考慮。

名稱

arguments

類型

java.lang.String[]

預設值

使用者屬性

Since

classesDirectory

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

名稱

classesDirectory

類型

java.io.File

預設值

${project.build.outputDirectory}

使用者屬性

Since

compilerArguments

應提供給 AOT 編譯程序的參數。在命令列上,請確保將多個值括在引號之間。

名稱

compilerArguments

類型

java.lang.String

預設值

使用者屬性

spring-boot.aot.compilerArguments

Since

excludeGroupIds

要排除的 groupId 名稱的逗號分隔清單 (完全匹配)。

名稱

excludeGroupIds

類型

java.lang.String

預設值

使用者屬性

spring-boot.excludeGroupIds

Since

1.1.0

excludes

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

名稱

excludes

類型

java.util.List

預設值

使用者屬性

spring-boot.excludes

Since

1.1.0

generatedClasses

包含產生的類別的目錄。

名稱

generatedClasses

類型

java.io.File

預設值

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

使用者屬性

Since

generatedResources

包含產生的資源的目錄。

名稱

generatedResources

類型

java.io.File

預設值

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

使用者屬性

Since

generatedSources

包含產生的原始碼的目錄。

名稱

generatedSources

類型

java.io.File

預設值

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

使用者屬性

Since

includes

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

名稱

includes

類型

java.util.List

預設值

使用者屬性

spring-boot.includes

Since

1.2.0

jvmArguments

應與 AOT 程序相關聯的 JVM 參數。在命令列上,請確保將多個值括在引號之間。

名稱

jvmArguments

類型

java.lang.String

預設值

使用者屬性

spring-boot.aot.jvmArguments

Since

mainClass

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

名稱

mainClass

類型

java.lang.String

預設值

使用者屬性

spring-boot.aot.main-class

Since

profiles

在 AOT 處理中要考慮的 Spring Profile。

名稱

profiles

類型

java.lang.String[]

預設值

使用者屬性

Since

skip

跳過執行。

名稱

skip

類型

boolean

預設值

false

使用者屬性

spring-boot.aot.skip

Since

systemPropertyVariables

要傳遞給 AOT 程序的 JVM 系統屬性清單。

名稱

systemPropertyVariables

類型

java.util.Map

預設值

使用者屬性

Since

處理測試

AOT 引擎可以應用於使用 Spring 的測試上下文框架的 JUnit 5 測試。合適的測試由 AOT 引擎處理,以產生 ApplicationContextInitializer 程式碼。

若要組態您的應用程式以使用此功能,請為 process-test-aot 目標新增執行,如下列範例所示

<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
	<executions>
		<execution>
			<id>process-test-aot</id>
			<goals>
				<goal>process-test-aot</goal>
			</goals>
		</execution>
	</executions>
</plugin>
如果您使用 spring-boot-starter-parent,如果您啟用 nativeTest Profile,則會自動組態此執行。

與應用程式 AOT 處理一樣,BeanFactory 在建置時期已完全準備就緒。

spring-boot:process-test-aot

org.springframework.boot:spring-boot-maven-plugin:3.3.5

在測試上調用 AOT 引擎。

必要參數

名稱 類型 預設值

classesDirectory

File

${project.build.outputDirectory}

generatedClasses

File

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

generatedResources

File

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

generatedSources

File

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

generatedTestClasses

File

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

testClassesDirectory

File

${project.build.testOutputDirectory}

可選參數

名稱 類型 預設值

compilerArguments

String

excludeGroupIds

String

excludes

List

includes

List

jvmArguments

String

skip

boolean

false

systemPropertyVariables

Map

參數詳細資訊

classesDirectory

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

名稱

classesDirectory

類型

java.io.File

預設值

${project.build.outputDirectory}

使用者屬性

Since

compilerArguments

應提供給 AOT 編譯程序的參數。在命令列上,請確保將多個值括在引號之間。

名稱

compilerArguments

類型

java.lang.String

預設值

使用者屬性

spring-boot.aot.compilerArguments

Since

excludeGroupIds

要排除的 groupId 名稱的逗號分隔清單 (完全匹配)。

名稱

excludeGroupIds

類型

java.lang.String

預設值

使用者屬性

spring-boot.excludeGroupIds

Since

1.1.0

excludes

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

名稱

excludes

類型

java.util.List

預設值

使用者屬性

spring-boot.excludes

Since

1.1.0

generatedClasses

包含產生的測試類別的目錄。

名稱

generatedClasses

類型

java.io.File

預設值

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

使用者屬性

Since

generatedResources

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

名稱

generatedResources

類型

java.io.File

預設值

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

使用者屬性

Since

generatedSources

包含產生的原始碼的目錄。

名稱

generatedSources

類型

java.io.File

預設值

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

使用者屬性

Since

generatedTestClasses

包含產生的測試類別的目錄。

名稱

generatedTestClasses

類型

java.io.File

預設值

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

使用者屬性

Since

includes

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

名稱

includes

類型

java.util.List

預設值

使用者屬性

spring-boot.includes

Since

1.2.0

jvmArguments

應與 AOT 程序相關聯的 JVM 參數。在命令列上,請確保將多個值括在引號之間。

名稱

jvmArguments

類型

java.lang.String

預設值

使用者屬性

spring-boot.aot.jvmArguments

Since

skip

跳過執行。

名稱

skip

類型

boolean

預設值

false

使用者屬性

spring-boot.aot.skip

Since

systemPropertyVariables

要傳遞給 AOT 程序的 JVM 系統屬性清單。

名稱

systemPropertyVariables

類型

java.util.Map

預設值

使用者屬性

Since

testClassesDirectory

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

名稱

testClassesDirectory

類型

java.io.File

預設值

${project.build.testOutputDirectory}

使用者屬性

Since