Ahead-of-Time(AOT)处理
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
目标与 运行目标 共享许多属性。
使用本机配置文件
如果您使用 spring-boot-starter-parent
作为项目的 parent
,则可以使用 native
配置文件来简化构建本机镜像所需的步骤。
native
配置文件配置如下:
-
当 Spring Boot Maven 插件应用于项目时执行
process-aot
。 -
适当的设置,以便 构建镜像 生成本机镜像。
-
为 Native Build Tools Maven Plugin 提供合理的默认值,特别是:
-
确保插件使用原始类路径,而不是主 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>
下面的示例对 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.6
Invoke the AOT engine on the application.
Required parameters
Name | Type | Default |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
Optional parameters
Name | Type | Default |
---|---|---|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
|
|
|
Parameter details
arguments
Application arguments that should be taken into account for AOT processing.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
classesDirectory
Directory containing the classes and resource files that should be packaged into the archive.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
compilerArguments
Arguments that should be provided to the AOT compile process. On command line, make sure to wrap multiple values between quotes.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
excludeGroupIds
Comma separated list of groupId names to exclude (exact match).
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
excludes
Collection of artifact definitions to exclude. The Exclude
element defines mandatory groupId
and artifactId
components and an optional classifier
component. When configured as a property, values should be comma-separated with colon-separated components: groupId:artifactId,groupId:artifactId:classifier
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
generatedClasses
Directory containing the generated classes.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
generatedResources
Directory containing the generated resources.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
generatedSources
Directory containing the generated sources.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
includes
Collection of artifact definitions to include. The Include
element defines mandatory groupId
and artifactId
components and an optional classifier
component. When configured as a property, values should be comma-separated with colon-separated components: groupId:artifactId,groupId:artifactId:classifier
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
jvmArguments
JVM arguments that should be associated with the AOT process. On command line, make sure to wrap multiple values between quotes.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
mainClass
Name of the main class to use as the source for the AOT process. If not specified the first compiled class found that contains a 'main' method will be used.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
profiles
Spring profiles to take into account for AOT processing.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
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 配置文件时会自动配置此执行。
|
与应用程序 AOT 处理一样,BeanFactory
在构建时已完全准备好。
spring-boot:process-test-aot
org.springframework.boot:spring-boot-maven-plugin:3.4.6
Invoke the AOT engine on tests.
Required parameters
Name | Type | Default |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Parameter details
classesDirectory
Directory containing the classes and resource files that should be used to run the tests.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
compilerArguments
Arguments that should be provided to the AOT compile process. On command line, make sure to wrap multiple values between quotes.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
excludeGroupIds
Comma separated list of groupId names to exclude (exact match).
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
excludes
Collection of artifact definitions to exclude. The Exclude
element defines mandatory groupId
and artifactId
components and an optional classifier
component. When configured as a property, values should be comma-separated with colon-separated components: groupId:artifactId,groupId:artifactId:classifier
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
generatedClasses
Directory containing the generated test classes.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
generatedResources
Directory containing the generated test resources.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
generatedSources
Directory containing the generated sources.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
generatedTestClasses
Directory containing the generated test classes.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
includes
Collection of artifact definitions to include. The Include
element defines mandatory groupId
and artifactId
components and an optional classifier
component. When configured as a property, values should be comma-separated with colon-separated components: groupId:artifactId,groupId:artifactId:classifier
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
jvmArguments
JVM arguments that should be associated with the AOT process. On command line, make sure to wrap multiple values between quotes.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
skip
Skip the execution.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |