|
此版本仍在开发中,尚未被视为稳定版本。如需最新稳定版本,请使用 Spring Boot 4.0.4! |
提前处理
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 生成本地镜像。
-
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:4.1.0-SNAPSHOT
在应用程序上调用 AOT 引擎。
必需参数
| 名称 | 类型 | 默认 |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
参数详情
classesDirectory
包含应打包到归档文件中的类和资源文件的目录。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
compilerArguments
应提供给 AOT 编译过程的参数。在命令行中,请确保将多个值用引号括起来。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
excludeGroupIds
要排除的 groupId 名称列表(精确匹配),以逗号分隔。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
|
excludes
要排除的构件定义集合。第 Exclude 个元素定义了必需的 groupId 和 artifactId 组件,以及一个可选的 classifier 组件。当配置为属性时,值应以逗号分隔,组件之间以冒号分隔:groupId:artifactId,groupId:artifactId:classifier
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
|
generatedClasses
包含生成类的目录。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
generatedResources
包含生成资源的目录。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
generatedSources
包含生成源代码的目录。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
includes
要包含的构件定义集合。第 Include 个元素定义了必需的 groupId 和 artifactId 组件,以及一个可选的 classifier 组件。当配置为属性时,值应以逗号分隔,组件之间以冒号分隔:groupId:artifactId,groupId:artifactId:classifier
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
|
jvmArguments
应与 AOT 进程关联的 JVM 参数。在命令行上,请确保将多个值用引号括起来。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
处理测试
AOT 引擎可以应用于使用 Spring 测试上下文框架的 JUnit 5 测试。 这些测试会由 AOT 引擎处理,然后在原生镜像中执行。
当使用 maven.test.skip 属性跳过常规测试时,这些测试不会被处理。 |
与 生产代码 一样,spring-boot-starter-parent 定义了一个 nativeTest 配置文件,可用于简化在原生镜像中执行测试所需的步骤。
nativeTest 配置文件配置了以下内容:
-
在项目中应用 Spring Boot Maven 插件时,
process-test-aot的执行。 -
当在项目中应用 Native Build Tools Maven Plugin 时,执行
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.1.0-SNAPSHOT
在测试中调用 AOT 引擎。
必需参数
| 名称 | 类型 | 默认 |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
参数详情
classesDirectory
包含用于运行测试的类和资源文件的目录。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
compilerArguments
应提供给 AOT 编译过程的参数。在命令行中,请确保将多个值用引号括起来。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
excludeGroupIds
要排除的 groupId 名称列表(精确匹配),以逗号分隔。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
|
excludes
要排除的构件定义集合。第 Exclude 个元素定义了必需的 groupId 和 artifactId 组件,以及一个可选的 classifier 组件。当配置为属性时,值应以逗号分隔,组件之间以冒号分隔:groupId:artifactId,groupId:artifactId:classifier
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
|
generatedClasses
包含生成的测试类的目录。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
generatedResources
包含生成的测试资源的目录。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
generatedSources
包含生成源代码的目录。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
generatedTestClasses
包含生成的测试类的目录。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
includes
要包含的构件定义集合。第 Include 个元素定义了必需的 groupId 和 artifactId 组件,以及一个可选的 classifier 组件。当配置为属性时,值应以逗号分隔,组件之间以冒号分隔:groupId:artifactId,groupId:artifactId:classifier
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |
|
jvmArguments
应与 AOT 进程关联的 JVM 参数。在命令行上,请确保将多个值用引号括起来。
名称 |
|
|---|---|
类型 |
|
默认值 |
|
用户属性 |
|
自从 |