Ahead-of-Time Processing

Spring AOT is a process that analyzes your application at build-time and generate an optimized version of it. It is a mandatory step to run a Spring ApplicationContext in a native image.spring-doc.cn

For an overview of GraalVM Native Images support in Spring Boot, check the reference documentation.

The Spring Boot Maven plugin offers goals that can be used to perform AOT processing on both application and test code.spring-doc.cn

Processing Applications

To configure your application to use this feature, add an execution for the process-aot goal, as shown in the following example:spring-doc.cn

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

As the BeanFactory is fully prepared at build-time, conditions are also evaluated. This has an important difference compared to what a regular Spring Boot application does at runtime. For instance, if you want to opt-in or opt-out for certain features, you need to configure the environment used at build time to do so. The process-aot goal shares a number of properties with the run goal for that reason.spring-doc.cn

Using the Native Profile

If you use spring-boot-starter-parent as the parent of your project, a native profile can be used to streamline the steps required to build a native image.spring-doc.cn

The native profile configures the following:spring-doc.cn

The use of the raw classpath means that native image does not know about the generated MANIFEST. If you need to read the content of the manifest in a native image, for instance to get the implementation version of your application, configure the classesDirectory option to use the regular jar.spring-doc.cn

To benefit from the native profile, a module that represents an application should define two plugins, as shown in the following example:spring-doc.cn

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

A single project can trigger the generation of a native image on the command-line using either Cloud Native Buildpacks or Native Image Build Tools.spring-doc.cn

To use the native profile with a multi-modules project, you can create a customization of the native profile so that it invokes your preferred technique.spring-doc.cn

To bind Cloud Native Buildpacks during the package phase, add the following to the root POM of your multi-modules project:spring-doc.cn

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

The example below does the same for Native Build Tools:spring-doc.cn

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

Once the above is in place, you can build your multi-modules project and generate a native image in the relevant sub-modules, as shown in the following example:spring-doc.cn

$ mvn package -Pnative
A "relevant" sub-module is a module that represents a Spring Boot application. Such module must define the Native Build Tools and Spring Boot plugins as described above.

spring-boot:process-aot

org.springframework.boot:spring-boot-maven-plugin:3.4.0spring-doc.cn

Invoke the AOT engine on the application.spring-doc.cn

Required parameters

Name Type Default

classesDirectoryspring-doc.cn

Filespring-doc.cn

${project.build.outputDirectory}spring-doc.cn

generatedClassesspring-doc.cn

Filespring-doc.cn

${project.build.directory}/spring-aot/main/classesspring-doc.cn

generatedResourcesspring-doc.cn

Filespring-doc.cn

${project.build.directory}/spring-aot/main/resourcesspring-doc.cn

generatedSourcesspring-doc.cn

Filespring-doc.cn

${project.build.directory}/spring-aot/main/sourcesspring-doc.cn

Parameter details

arguments

Application arguments that should be taken into account for AOT processing.spring-doc.cn

Namespring-doc.cn

argumentsspring-doc.cn

Typespring-doc.cn

java.lang.String[]spring-doc.cn

Default valuespring-doc.cn

User propertyspring-doc.cn

Sincespring-doc.cn

classesDirectory

Directory containing the classes and resource files that should be packaged into the archive.spring-doc.cn

Namespring-doc.cn

classesDirectoryspring-doc.cn

Typespring-doc.cn

java.io.Filespring-doc.cn

Default valuespring-doc.cn

${project.build.outputDirectory}spring-doc.cn

User propertyspring-doc.cn

Sincespring-doc.cn

compilerArguments

Arguments that should be provided to the AOT compile process. On command line, make sure to wrap multiple values between quotes.spring-doc.cn

Namespring-doc.cn

compilerArgumentsspring-doc.cn

Typespring-doc.cn

java.lang.Stringspring-doc.cn

Default valuespring-doc.cn

User propertyspring-doc.cn

spring-boot.aot.compilerArgumentsspring-doc.cn

Sincespring-doc.cn

excludeGroupIds

Comma separated list of groupId names to exclude (exact match).spring-doc.cn

Namespring-doc.cn

excludeGroupIdsspring-doc.cn

Typespring-doc.cn

java.lang.Stringspring-doc.cn

Default valuespring-doc.cn

User propertyspring-doc.cn

spring-boot.excludeGroupIdsspring-doc.cn

Sincespring-doc.cn

1.1.0spring-doc.cn

excludes

Collection of artifact definitions to exclude. The Exclude element defines mandatory groupId and artifactId components and an optional classifier component. When configured as a property, values should be comma-separated with colon-separated components: groupId:artifactId,groupId:artifactId:classifierspring-doc.cn

Namespring-doc.cn

excludesspring-doc.cn

Typespring-doc.cn

java.util.Listspring-doc.cn

Default valuespring-doc.cn

User propertyspring-doc.cn

spring-boot.excludesspring-doc.cn

Sincespring-doc.cn

1.1.0spring-doc.cn

generatedClasses

Directory containing the generated classes.spring-doc.cn

Namespring-doc.cn

generatedClassesspring-doc.cn

Typespring-doc.cn

java.io.Filespring-doc.cn

Default valuespring-doc.cn

${project.build.directory}/spring-aot/main/classesspring-doc.cn

User propertyspring-doc.cn

Sincespring-doc.cn

generatedResources

Directory containing the generated resources.spring-doc.cn

Namespring-doc.cn

generatedResourcesspring-doc.cn

Typespring-doc.cn

java.io.Filespring-doc.cn

Default valuespring-doc.cn

${project.build.directory}/spring-aot/main/resourcesspring-doc.cn

User propertyspring-doc.cn

Sincespring-doc.cn

generatedSources

Directory containing the generated sources.spring-doc.cn

Namespring-doc.cn

generatedSourcesspring-doc.cn

Typespring-doc.cn

java.io.Filespring-doc.cn

Default valuespring-doc.cn

${project.build.directory}/spring-aot/main/sourcesspring-doc.cn

User propertyspring-doc.cn

Sincespring-doc.cn

includes

Collection of artifact definitions to include. The Include element defines mandatory groupId and artifactId components and an optional classifier component. When configured as a property, values should be comma-separated with colon-separated components: groupId:artifactId,groupId:artifactId:classifierspring-doc.cn

Namespring-doc.cn

includesspring-doc.cn

Typespring-doc.cn

java.util.Listspring-doc.cn

Default valuespring-doc.cn

User propertyspring-doc.cn

spring-boot.includesspring-doc.cn

Sincespring-doc.cn

1.2.0spring-doc.cn

jvmArguments

JVM arguments that should be associated with the AOT process. On command line, make sure to wrap multiple values between quotes.spring-doc.cn

Namespring-doc.cn

jvmArgumentsspring-doc.cn

Typespring-doc.cn

java.lang.Stringspring-doc.cn

Default valuespring-doc.cn

User propertyspring-doc.cn

spring-boot.aot.jvmArgumentsspring-doc.cn

Sincespring-doc.cn

mainClass

Name of the main class to use as the source for the AOT process. If not specified the first compiled class found that contains a 'main' method will be used.spring-doc.cn

Namespring-doc.cn

mainClassspring-doc.cn

Typespring-doc.cn

java.lang.Stringspring-doc.cn

Default valuespring-doc.cn

User propertyspring-doc.cn

spring-boot.aot.main-classspring-doc.cn

Sincespring-doc.cn

profiles

Spring profiles to take into account for AOT processing.spring-doc.cn

Namespring-doc.cn

profilesspring-doc.cn

Typespring-doc.cn

java.lang.String[]spring-doc.cn

Default valuespring-doc.cn

User propertyspring-doc.cn

Sincespring-doc.cn

skip

Skip the execution.spring-doc.cn

Namespring-doc.cn

skipspring-doc.cn

Typespring-doc.cn

booleanspring-doc.cn

Default valuespring-doc.cn

falsespring-doc.cn

User propertyspring-doc.cn

spring-boot.aot.skipspring-doc.cn

Sincespring-doc.cn

systemPropertyVariables

List of JVM system properties to pass to the AOT process.spring-doc.cn

Namespring-doc.cn

systemPropertyVariablesspring-doc.cn

Typespring-doc.cn

java.util.Mapspring-doc.cn

Default valuespring-doc.cn

User propertyspring-doc.cn

Sincespring-doc.cn

Processing Tests

The AOT engine can be applied to JUnit 5 tests that use Spring’s Test Context Framework. Suitable tests are processed by the AOT engine in order to generate ApplicationContextInitializer code.spring-doc.cn

To configure your application to use this feature, add an execution for the process-test-aot goal, as shown in the following example:spring-doc.cn

<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>
If you are using spring-boot-starter-parent, this execution is automatically configured if you enable the nativeTest profile.

As with application AOT processing, the BeanFactory is fully prepared at build-time.spring-doc.cn

spring-boot:process-test-aot

org.springframework.boot:spring-boot-maven-plugin:3.4.0spring-doc.cn

Invoke the AOT engine on tests.spring-doc.cn

Required parameters

Name Type Default

classesDirectoryspring-doc.cn

Filespring-doc.cn

${project.build.outputDirectory}spring-doc.cn

generatedClassesspring-doc.cn

Filespring-doc.cn

${project.build.directory}/spring-aot/main/classesspring-doc.cn

generatedResourcesspring-doc.cn

Filespring-doc.cn

${project.build.directory}/spring-aot/test/resourcesspring-doc.cn

generatedSourcesspring-doc.cn

Filespring-doc.cn

${project.build.directory}/spring-aot/test/sourcesspring-doc.cn

generatedTestClassesspring-doc.cn

Filespring-doc.cn

${project.build.directory}/spring-aot/test/classesspring-doc.cn

testClassesDirectoryspring-doc.cn

Filespring-doc.cn

${project.build.testOutputDirectory}spring-doc.cn

Parameter details

classesDirectory

Directory containing the classes and resource files that should be used to run the tests.spring-doc.cn

Namespring-doc.cn

classesDirectoryspring-doc.cn

Typespring-doc.cn

java.io.Filespring-doc.cn

Default valuespring-doc.cn

${project.build.outputDirectory}spring-doc.cn

User propertyspring-doc.cn

Sincespring-doc.cn

compilerArguments

Arguments that should be provided to the AOT compile process. On command line, make sure to wrap multiple values between quotes.spring-doc.cn

Namespring-doc.cn

compilerArgumentsspring-doc.cn

Typespring-doc.cn

java.lang.Stringspring-doc.cn

Default valuespring-doc.cn

User propertyspring-doc.cn

spring-boot.aot.compilerArgumentsspring-doc.cn

Sincespring-doc.cn

excludeGroupIds

Comma separated list of groupId names to exclude (exact match).spring-doc.cn

Namespring-doc.cn

excludeGroupIdsspring-doc.cn

Typespring-doc.cn

java.lang.Stringspring-doc.cn

Default valuespring-doc.cn

User propertyspring-doc.cn

spring-boot.excludeGroupIdsspring-doc.cn

Sincespring-doc.cn

1.1.0spring-doc.cn

excludes

Collection of artifact definitions to exclude. The Exclude element defines mandatory groupId and artifactId components and an optional classifier component. When configured as a property, values should be comma-separated with colon-separated components: groupId:artifactId,groupId:artifactId:classifierspring-doc.cn

Namespring-doc.cn

excludesspring-doc.cn

Typespring-doc.cn

java.util.Listspring-doc.cn

Default valuespring-doc.cn

User propertyspring-doc.cn

spring-boot.excludesspring-doc.cn

Sincespring-doc.cn

1.1.0spring-doc.cn

generatedClasses

Directory containing the generated test classes.spring-doc.cn

Namespring-doc.cn

generatedClassesspring-doc.cn

Typespring-doc.cn

java.io.Filespring-doc.cn

Default valuespring-doc.cn

${project.build.directory}/spring-aot/main/classesspring-doc.cn

User propertyspring-doc.cn

Sincespring-doc.cn

generatedResources

Directory containing the generated test resources.spring-doc.cn

Namespring-doc.cn

generatedResourcesspring-doc.cn

Typespring-doc.cn

java.io.Filespring-doc.cn

Default valuespring-doc.cn

${project.build.directory}/spring-aot/test/resourcesspring-doc.cn

User propertyspring-doc.cn

Sincespring-doc.cn

generatedSources

Directory containing the generated sources.spring-doc.cn

Namespring-doc.cn

generatedSourcesspring-doc.cn

Typespring-doc.cn

java.io.Filespring-doc.cn

Default valuespring-doc.cn

${project.build.directory}/spring-aot/test/sourcesspring-doc.cn

User propertyspring-doc.cn

Sincespring-doc.cn

generatedTestClasses

Directory containing the generated test classes.spring-doc.cn

Namespring-doc.cn

generatedTestClassesspring-doc.cn

Typespring-doc.cn

java.io.Filespring-doc.cn

Default valuespring-doc.cn

${project.build.directory}/spring-aot/test/classesspring-doc.cn

User propertyspring-doc.cn

Sincespring-doc.cn

includes

Collection of artifact definitions to include. The Include element defines mandatory groupId and artifactId components and an optional classifier component. When configured as a property, values should be comma-separated with colon-separated components: groupId:artifactId,groupId:artifactId:classifierspring-doc.cn

Namespring-doc.cn

includesspring-doc.cn

Typespring-doc.cn

java.util.Listspring-doc.cn

Default valuespring-doc.cn

User propertyspring-doc.cn

spring-boot.includesspring-doc.cn

Sincespring-doc.cn

1.2.0spring-doc.cn

jvmArguments

JVM arguments that should be associated with the AOT process. On command line, make sure to wrap multiple values between quotes.spring-doc.cn

Namespring-doc.cn

jvmArgumentsspring-doc.cn

Typespring-doc.cn

java.lang.Stringspring-doc.cn

Default valuespring-doc.cn

User propertyspring-doc.cn

spring-boot.aot.jvmArgumentsspring-doc.cn

Sincespring-doc.cn

skip

Skip the execution.spring-doc.cn

Namespring-doc.cn

skipspring-doc.cn

Typespring-doc.cn

booleanspring-doc.cn

Default valuespring-doc.cn

falsespring-doc.cn

User propertyspring-doc.cn

spring-boot.aot.skipspring-doc.cn

Sincespring-doc.cn

systemPropertyVariables

List of JVM system properties to pass to the AOT process.spring-doc.cn

Namespring-doc.cn

systemPropertyVariablesspring-doc.cn

Typespring-doc.cn

java.util.Mapspring-doc.cn

Default valuespring-doc.cn

User propertyspring-doc.cn

Sincespring-doc.cn

testClassesDirectory

Directory containing the classes and resource files that should be packaged into the archive.spring-doc.cn

Namespring-doc.cn

testClassesDirectoryspring-doc.cn

Typespring-doc.cn

java.io.Filespring-doc.cn

Default valuespring-doc.cn

${project.build.testOutputDirectory}spring-doc.cn

User propertyspring-doc.cn

Sincespring-doc.cn