提前处理

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.