提前处理
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 意味着原生镜像不知道生成的 |
为了受益于 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>
单个项目可以使用 云原生 Buildpacks 或 Native 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
包含应打包到归档中的类和资源文件的目录。
名称 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
始于 |
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 引擎会处理合适的测试,以便生成 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
包含应应用于运行测试的类和资源文件的目录。
名称 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
始于 |
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 参数。在命令行中,确保用引号将多个值括起来。
名称 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
始于 |