跳到主要内容

预编译处理

DeepSeek V3 中英对照 Ahead-of-Time Processing

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>
xml

由于 BeanFactory 在构建时已经完全准备好,条件也会被评估。这与常规 Spring Boot 应用程序在运行时所做的操作有重要区别。例如,如果你想选择启用或禁用某些功能,你需要配置在构建时使用的环境来实现这一点。出于这个原因,process-aot 目标与 run 目标 共享了许多属性。

使用原生配置文件

如果你使用 spring-boot-starter-parent 作为项目的 parent,可以使用 native profile 来简化构建原生镜像所需的步骤。

native 配置文件配置了以下内容:

  • 在项目上应用 Spring Boot Maven 插件时,执行 process-aot

  • 合适的设置,以便 build-image 生成原生镜像。

  • Native Build Tools Maven Plugin 提供合理的默认值,特别是:

    • 确保插件使用原始类路径,而不是主 jar 文件,因为它无法理解我们的重新打包的 jar 格式。

    • 验证是否有合适的 GraalVM 版本可用。

    • 下载第三方可达性元数据。

注意

使用原始类路径意味着原生镜像无法识别生成的 MANIFEST.MF 文件。如果你需要在原生镜像中读取清单文件的内容,例如获取应用程序的实现版本,请配置 classesDirectory 选项以使用常规的 jar 文件。

要从 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>
xml

单个项目可以通过命令行使用 Cloud Native BuildpacksNative 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>
xml

下面的示例对 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>
xml

在完成上述步骤后,您可以构建多模块项目并在相关子模块中生成本地镜像,如下例所示:

$ mvn package -Pnative
shell
备注

一个“相关的”子模块是指代表 Spring Boot 应用程序的模块。此类模块必须按照上述方式定义 Native Build Tools 和 Spring Boot 插件。

spring-boot:process-aot

org.springframework.boot:spring-boot-maven-plugin:3.4.2

在应用程序上调用 AOT 引擎。

必填参数

名称类型默认值
classesDirectoryFile${project.build.outputDirectory}
generatedClassesFile${project.build.directory}/spring-aot/main/classes
generatedResourcesFile${project.build.directory}/spring-aot/main/resources
generatedSourcesFile${project.build.directory}/spring-aot/main/sources

可选参数

名称类型默认值
argumentsString[]
compilerArgumentsString
excludeGroupIdsString
excludesList
includesList
jvmArgumentsString
mainClassString
profilesString[]
skipbooleanfalse
systemPropertyVariablesMap

参数详情

arguments

在 AOT(Ahead-of-Time)处理过程中应考虑的应用程序参数。

名称arguments
类型java.lang.String[]
默认值
用户属性
自版本

classesDirectory

包含应打包到存档中的类和资源文件的目录。

名称classesDirectory
类型java.io.File
默认值${project.build.outputDirectory}
用户属性
起始版本

compilerArguments

应提供给 AOT 编译过程的参数。在命令行中,请确保将多个值用引号括起来。

名称compilerArguments
类型java.lang.String
默认值
用户属性spring-boot.aot.compilerArguments
自版本

excludeGroupIds

要排除的 groupId 名称的逗号分隔列表(精确匹配)。

名称excludeGroupIds
类型java.lang.String
默认值
用户属性spring-boot.excludeGroupIds
自版本1.1.0

excludes

要排除的构件定义集合。Exclude 元素定义了必需的 groupIdartifactId 组件以及可选的 classifier 组件。当配置为属性时,值应以逗号分隔,组件之间以冒号分隔:groupId:artifactId,groupId:artifactId:classifier

名称excludes
类型java.util.List
默认值
用户属性spring-boot.excludes
自版本1.1.0

generatedClasses

包含生成的类的目录。

名称generatedClasses
类型java.io.File
默认值${project.build.directory}/spring-aot/main/classes
用户属性
起始版本

generatedResources

包含生成资源的目录。

名称generatedResources
类型java.io.File
默认值${project.build.directory}/spring-aot/main/resources
用户属性
自版本

generatedSources

包含已生成源代码的目录。

名称generatedSources
类型java.io.File
默认值${project.build.directory}/spring-aot/main/sources
用户属性
自版本

includes

要包含的构件定义的集合。Include 元素定义了必需的 groupIdartifactId 组件,以及一个可选的 classifier 组件。当配置为属性时,值应以逗号分隔,组件之间以冒号分隔:groupId:artifactId,groupId:artifactId:classifier

名称includes
类型java.util.List
默认值
用户属性spring-boot.includes
自版本1.2.0

jvmArguments

应与 AOT 进程关联的 JVM 参数。在命令行中,请确保用引号包裹多个值。

名称jvmArguments
类型java.lang.String
默认值
用户属性spring-boot.aot.jvmArguments

mainClass

用于 AOT 过程的主要类的名称。如果未指定,将使用第一个包含 main 方法的已编译类。

名称mainClass
类型java.lang.String
默认值
用户属性spring-boot.aot.main-class
起始版本

profiles

AOT 处理需要考虑的 Spring profiles。

名称profiles
类型java.lang.String[]
默认值
用户属性
自版本

skip

跳过执行。

名称skip
类型boolean
默认值false
用户属性spring-boot.aot.skip

systemPropertyVariables

传递给 AOT 进程的 JVM 系统属性列表。

名称systemPropertyVariables
类型java.util.Map
默认值
用户属性
自版本

处理测试

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>
xml
提示

如果您使用的是 spring-boot-starter-parent,在启用 nativeTest 配置文件时,此执行将自动配置。

与应用程序 AOT 处理一样,BeanFactory 在构建时已完全准备好。

spring-boot:process-test-aot

org.springframework.boot:spring-boot-maven-plugin:3.4.2

在测试中调用 AOT 引擎。

必填参数

名称类型默认值
classesDirectoryFile${project.build.outputDirectory}
generatedClassesFile${project.build.directory}/spring-aot/main/classes
generatedResourcesFile${project.build.directory}/spring-aot/test/resources
generatedSourcesFile${project.build.directory}/spring-aot/test/sources
generatedTestClassesFile${project.build.directory}/spring-aot/test/classes
testClassesDirectoryFile${project.build.testOutputDirectory}

可选参数

名称类型默认值
compilerArgumentsString
excludeGroupIdsString
excludesList
includesList
jvmArgumentsString
skipbooleanfalse
systemPropertyVariablesMap

参数详情

classesDirectory

包含用于运行测试的类和资源文件的目录。

名称classesDirectory
类型java.io.File
默认值${project.build.outputDirectory}
用户属性
引入版本

compilerArguments

应该提供给 AOT 编译过程的参数。在命令行中,请确保将多个值用引号括起来。

名称compilerArguments
类型java.lang.String
默认值
用户属性spring-boot.aot.compilerArguments
自版本

excludeGroupIds

排除的 groupId 名称的逗号分隔列表(精确匹配)。

名称excludeGroupIds
类型java.lang.String
默认值
用户属性spring-boot.excludeGroupIds
自版本1.1.0

excludes

要排除的构件定义集合。Exclude 元素定义了必需的 groupIdartifactId 组件,以及可选的 classifier 组件。当配置为属性时,值应以逗号分隔,组件之间用冒号分隔:groupId:artifactId,groupId:artifactId:classifier

名称excludes
类型java.util.List
默认值
用户属性spring-boot.excludes
自版本1.1.0

generatedClasses

包含生成的测试类的目录。

名称generatedClasses
类型java.io.File
默认值${project.build.directory}/spring-aot/main/classes
用户属性
自版本

generatedResources

包含生成的测试资源的目录。

名称generatedResources
类型java.io.File
默认值${project.build.directory}/spring-aot/test/resources
用户属性
自版本

generatedSources

包含生成源代码的目录。

名称generatedSources
类型java.io.File
默认值${project.build.directory}/spring-aot/test/sources
用户属性
自版本

generatedTestClasses

包含生成的测试类的目录。

名称generatedTestClasses
类型java.io.File
默认值${project.build.directory}/spring-aot/test/classes
用户属性

includes

要包含的工件定义集合。Include 元素定义了必需的 groupIdartifactId 组件,以及可选的 classifier 组件。当作为属性配置时,值应以逗号分隔,组件之间用冒号分隔:groupId:artifactId,groupId:artifactId:classifier

名称includes
类型java.util.List
默认值
用户属性spring-boot.includes
1.2.0

jvmArguments

应与 AOT 进程关联的 JVM 参数。在命令行中,请确保将多个值用引号括起来。

名称jvmArguments
类型java.lang.String
默认值
用户属性spring-boot.aot.jvmArguments

skip

跳过执行。

名称skip
类型boolean
默认值false
用户属性spring-boot.aot.skip

systemPropertyVariables

传递给 AOT 进程的 JVM 系统属性列表。

名称systemPropertyVariables
类型java.util.Map
默认值
用户属性
起始版本

testClassesDirectory

包含应打包到存档中的类和资源文件的目录。

名称testClassesDirectory
类型java.io.File
默认值${project.build.testOutputDirectory}
用户属性