提前处理
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 版本。
-
下载第三方可达性元数据。
-
|
使用原始类路径意味着原生镜像不知道生成的 |
为了利用 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>
以下示例为原生构建工具执行相同的操作
<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
包含应打包到归档中的类文件和资源文件的目录。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
compilerArguments
应提供给 AOT 编译过程的参数。在命令行上,请务必将多个值用引号括起来。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
excludeGroupIds
要排除的 groupId 名称的逗号分隔列表(精确匹配)。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
|
excludes
要排除的 artifact 定义集合。Exclude 元素定义了强制的 groupId 和 artifactId 组件以及可选的 classifier 组件。当配置为属性时,值应以逗号分隔,组件以冒号分隔:groupId:artifactId,groupId:artifactId:classifier
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
|
generatedClasses
包含生成类的目录。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
generatedResources
包含生成资源的目录。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
generatedSources
包含生成源的目录。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
includes
要包含的 artifact 定义集合。Include 元素定义了强制的 groupId 和 artifactId 组件以及可选的 classifier 组件。当配置为属性时,值应以逗号分隔,组件以冒号分隔:groupId:artifactId,groupId:artifactId:classifier
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
|
jvmArguments
应与 AOT 过程关联的 JVM 参数。在命令行上,请务必将多个值用引号括起来。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
处理测试
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
包含应用于运行测试的类文件和资源文件的目录。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
compilerArguments
应提供给 AOT 编译过程的参数。在命令行上,请务必将多个值用引号括起来。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
excludeGroupIds
要排除的 groupId 名称的逗号分隔列表(精确匹配)。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
|
excludes
要排除的 artifact 定义集合。Exclude 元素定义了强制的 groupId 和 artifactId 组件以及可选的 classifier 组件。当配置为属性时,值应以逗号分隔,组件以冒号分隔:groupId:artifactId,groupId:artifactId:classifier
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
|
generatedClasses
包含生成测试类的目录。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
generatedResources
包含生成测试资源的目录。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
generatedSources
包含生成源的目录。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
generatedTestClasses
包含生成测试类的目录。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
includes
要包含的 artifact 定义集合。Include 元素定义了强制的 groupId 和 artifactId 组件以及可选的 classifier 组件。当配置为属性时,值应以逗号分隔,组件以冒号分隔:groupId:artifactId,groupId:artifactId:classifier
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |
|
jvmArguments
应与 AOT 过程关联的 JVM 参数。在命令行上,请务必将多个值用引号括起来。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自 |