提前处理

Spring AOT 是一个在构建时分析你的应用并生成其优化版本的进程。它是为了在原生镜像中运行 Spring ApplicationContext 的强制步骤。

关于 Spring Boot 中 GraalVM 原生镜像支持的概述,请查阅参考文档

Spring Boot Maven 插件提供了可用于对应用代码和测试代码执行 AOT 处理的目标(goals)。

处理应用

要配置你的应用以使用此特性,请为 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 目标共享许多属性。

使用原生 Profile

如果你使用 spring-boot-starter-parent 作为你项目的 parent,则可以使用 native profile 来简化构建原生镜像所需的步骤。

native profile 配置了以下内容

  • Spring Boot Maven 插件应用于项目时执行 process-aot

  • 合适的设置,以便 build-image 生成原生镜像。

  • Native Build Tools Maven 插件的合理默认设置,特别是

    • 确保插件使用原始 classpath,而不是主 jar 文件,因为它不理解我们重新打包的 jar 格式。

    • 验证是否安装了合适的 GraalVM 版本。

    • 下载第三方可达性元数据。

使用原始 classpath 意味着原生镜像不知道生成的 MANIFEST.MF 文件。如果你需要在原生镜像中读取 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>

单个项目可以使用 云原生 BuildpacksNative Image Build Tools 在命令行触发原生镜像的生成。

要在多模块项目中使用 native profile,你可以自定义 native profile,以便它调用你偏好的技术。

要在 package 阶段绑定云原生 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>

完成上述配置后,你可以构建你的多模块项目,并在相关的子模块中生成原生镜像,如下例所示

$ mvn package -Pnative
"相关的" 子模块是代表 Spring Boot 应用的模块。这种模块必须定义上面描述的 Native Build Tools 和 Spring Boot 插件。

spring-boot:process-aot

org.springframework.boot:spring-boot-maven-plugin:3.4.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[]

默认值

用户属性

始于

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。

名称

profiles

类型

java.lang.String[]

默认值

用户属性

始于

skip

跳过执行。

名称

skip

类型

boolean

默认值

false

用户属性

spring-boot.aot.skip

始于

systemPropertyVariables

传递给 AOT 进程的 JVM 系统属性列表。

名称

systemPropertyVariables

类型

java.util.Map

默认值

用户属性

始于

处理测试

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.4.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}

用户属性

始于

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

类型

boolean

默认值

false

用户属性

spring-boot.aot.skip

始于

systemPropertyVariables

传递给 AOT 进程的 JVM 系统属性列表。

名称

systemPropertyVariables

类型

java.util.Map

默认值

用户属性

始于

testClassesDirectory

包含应打包到归档中的类和资源文件的目录。

名称

testClassesDirectory

类型

java.io.File

默认值

${project.build.testOutputDirectory}

用户属性

始于