Skip to content

Add AOT cache recording to build plugins and bootBuildImage - #51101

Closed
vpelikh wants to merge 1 commit into
spring-projects:mainfrom
vpelikh:GH-36774
Closed

Add AOT cache recording to build plugins and bootBuildImage#51101
vpelikh wants to merge 1 commit into
spring-projects:mainfrom
vpelikh:GH-36774

Conversation

@vpelikh

@vpelikh vpelikh commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Summary

Integrates Leyden AOT cache (JEP 483) recording into the Spring Boot Maven and Gradle build plugins, configured declaratively with a single switch. When enabled, the build records an AOT cache from the integration tests and bundles it into the OCI image, so the application reuses the pre-recorded cache at container startup.

Configuration

Maven — bind the aot-cache-record goal to the initialize phase and set the spring-boot.aot-cache-record property, then run tests together with the image build:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>aot-cache-record</id>
            <goals>
                <goal>aot-cache-record</goal>
            </goals>
        </execution>
    </executions>
</plugin>

<properties>
    <spring-boot.aot-cache-record>true</spring-boot.aot-cache-record>
</properties>
mvn test spring-boot:build-image

Gradle — set aotCacheRecord = true on the bootBuildImage task and run tests together with the image build:

bootBuildImage {
    aotCacheRecord = true
}
./gradlew test bootBuildImage

What it does

Recording the cache from integration tests

When enabled, the plugin injects -XX:AOTCacheOutput=<path> into the test JVM arguments, so the JVM records an AOT cache during integration test execution. Recording is gated on the image build being scheduled in the same execution:

  • Gradle — JavaPluginAction injects the flag into the test JVM args via whenReady, only when the project's bootBuildImage is in the task graph.
  • Maven — AotCacheRecordMojo (initialized in the initialize phase) injects the flag into the Surefire argLine, only when spring-boot:build-image is in the execution plan.

The test-only paths do not record a cache; the image build fails closed with actionable guidance when the cache is expected but missing.

Bundling the cache into the image

The generated aot-cache/application.aot is bundled into the OCI image and BP_JVM_AOTCACHE_ENABLED=true is set for the Paketo buildpack.

AOT cache compatibility metadata

AOT caches are JDK-version- and platform-specific. The plugins now write aot-cache/application.aot.meta alongside the cache, recording the java.version and os.arch of the JVM that built the image. The Paketo buildpack compares this against the JRE baked into the image and fails the build on a mismatch, preventing an incompatible cache from being reused.

Related

Integrate Leyden AOT cache recording into the build plugin lifecycle.
aotCacheRecord=true is the single user-facing property that handles
both recording from tests and bundling the cache into the image.

Maven:
- AotCacheRecordMojo at INITIALIZE phase reads spring-boot.aot-cache-record
  property and injects -XX:AOTCacheOutput into surefire argLine
- Only activates when spring-boot:build-image is in the execution plan
  (checks MavenSession.getGoals())
- Image.customize() bundles target/aot-cache into the image when present
- BuildImageMojo wires cacheDirectory from project.build.directory

Gradle:
- BootBuildImage exposes aotCacheRecord Property<Boolean>
- JavaPluginAction configures test JVM args in afterEvaluate, guarded by
  task graph check (graph.hasTask for bootBuildImage)
- customizeAotCache() sets BP_JVM_AOTCACHE_ENABLED and bundles the cache
  directory when aotCacheRecord=true

Buildpack:
- BuildRequest.withAdditionalContent(Path, String) merges a filesystem
  directory into the application archive
- CompositeTarArchive combines primary archive with additional content

Signed-off-by: Vasily Pelikh <2010720+vpelikh@users.noreply.github.com>
@snicoll

snicoll commented Jul 23, 2026

Copy link
Copy Markdown
Member

@vpelikh thanks for the PR but it isn't actionable right now. I'd like to make sure we reach a conclusion in the related Spring framework issue and we can then consider a contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: declined A suggestion or change that we don't feel we should currently apply

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants