此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Boot 3.4.0! |
提前处理
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
因此,goal 与 run goal 共享许多属性。
使用 Native Profile
如果您使用spring-boot-starter-parent
作为parent
中,一个native
配置文件可用于简化构建本机映像所需的步骤。
这native
profile 配置以下内容:
-
执行
process-aot
当 Spring Boot Maven 插件应用于项目时。 -
适当的设置,以便 build-image 生成本机映像。
-
本机构建工具 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>
单个项目可以使用 Cloud Native Buildpacks 或 Native Image Build Tools 在命令行上触发本机映像的生成。
要使用native
配置文件,则可以创建native
配置文件,以便它调用您的首选技术。
要在package
阶段,将以下内容添加到多模块项目的根 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.3.7-SNAPSHOT
在应用程序上调用 AOT 引擎。
必需参数
名字 | 类型 | 违约 |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
参数详情
classesDirectory
包含应打包到存档中的类和资源文件的目录。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
compilerArguments
应提供给 AOT 编译过程的参数。在命令行上,确保在引号之间将多个值括起来。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
excludeGroupIds
要排除的 groupId 名称的逗号分隔列表(完全匹配)。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
excludes
要排除的项目定义的集合。这Exclude
元素定义 mandatorygroupId
和artifactId
组件和可选的classifier
元件。当配置为属性时,值应使用冒号分隔的组件进行逗号分隔:groupId:artifactId,groupId:artifactId:classifier
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
generatedClasses
包含生成的类的目录。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
generatedResources
包含生成的资源的目录。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
generatedSources
包含生成的源的目录。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
includes
要包含的项目定义的集合。这Include
元素定义 mandatorygroupId
和artifactId
组件和可选的classifier
元件。当配置为属性时,值应使用冒号分隔的组件进行逗号分隔:groupId:artifactId,groupId:artifactId:classifier
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
jvmArguments
应与 AOT 进程关联的 JVM 参数。在命令行上,确保在引号之间将多个值括起来。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
加工测试
AOT 引擎可以应用于使用 Spring 的 Test Context Framework 的 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.3.7-SNAPSHOT
在测试中调用 AOT 引擎。
必需参数
名字 | 类型 | 违约 |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
参数详情
classesDirectory
包含应该用于运行测试的类和资源文件的目录。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
compilerArguments
应提供给 AOT 编译过程的参数。在命令行上,确保在引号之间将多个值括起来。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
excludeGroupIds
要排除的 groupId 名称的逗号分隔列表(完全匹配)。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
excludes
要排除的项目定义的集合。这Exclude
元素定义 mandatorygroupId
和artifactId
组件和可选的classifier
元件。当配置为属性时,值应使用冒号分隔的组件进行逗号分隔:groupId:artifactId,groupId:artifactId:classifier
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
generatedClasses
包含生成的测试类的目录。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
generatedResources
包含生成的测试资源的目录。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
generatedSources
包含生成的源的目录。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
generatedTestClasses
包含生成的测试类的目录。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
includes
要包含的项目定义的集合。这Include
元素定义 mandatorygroupId
和artifactId
组件和可选的classifier
元件。当配置为属性时,值应使用冒号分隔的组件进行逗号分隔:groupId:artifactId,groupId:artifactId:classifier
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
jvmArguments
应与 AOT 进程关联的 JVM 参数。在命令行上,确保在引号之间将多个值括起来。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |