Add a section on AOT to the Gradle plugin's docs

Closes gh-32750
pull/33505/head
Andy Wilkinson 2 years ago
parent e32c6cde72
commit 3082b0c5bf

@ -97,6 +97,10 @@ task asciidoctorPdf(type: org.asciidoctor.gradle.jvm.AsciidoctorTask) {
} }
} }
tasks.withType(org.asciidoctor.gradle.jvm.AbstractAsciidoctorTask) {
attributes "native-build-tools-version": nativeBuildToolsVersion
}
javadoc { javadoc {
options { options {
author = true author = true

@ -0,0 +1,43 @@
[[aot]]
= Ahead-of-Time Processing
Spring AOT is a process that analyzes your code at build-time in order to generate an optimized version of it.
It is most often used to help generate GraalVM native images.
The Spring Boot Gradle plugin provides tasks that can be used to perform AOT processing on both application and test code.
The tasks are configured automatically when the {nbt-gradle-plugin}[GraalVM Native Image plugin] is applied:
[source,groovy,indent=0,subs="verbatim,attributes",role="primary"]
.Groovy
----
include::../gradle/aot/apply-native-image-plugin.gradle[]
----
[source,kotlin,indent=0,subs="verbatim,attributes",role="secondary"]
.Kotlin
----
include::../gradle/aot/apply-native-image-plugin.gradle.kts[]
----
[[aot.processing-applications]]
== Processing Applications
Based on your `@SpringBootApplication`-annotated main class, the `processAot` task generates a persistent view of the beans that are going to be contributed at runtime in a way that bean instantiation is as straightforward as possible.
Additional post-processing of the factory is possible using callbacks.
For instance, these are used to generate the necessary reflection configuration that GraalVM needs to initialize the context in a native image.
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.
To this end, the `processAot` task is a {gradle-dsl}/org.gradle.api.tasks.JavaExec.html[`JavaExec`] task and can be configured with environment variables, system properties, and arguments as needed.
The `nativeCompile` task of the GraalVM Native Image plugin is automatically configured to use the output of the `processAot` task.
[[aot.processing-tests]]
== 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 `processTestAot` task to generate `ApplicationContextInitialzer` code.
As with application AOT processing, the `BeanFactory` is fully prepared at build-time.
As with `processAot`, the `processTestAot` task is `JavaExec` sub-class and can be configured as needed to influence this processing.
The `nativeTest` task of the GraalVM Native Image plugin is automatically configured to use the output of the `processAot` and `processTestAot` tasks.

@ -39,7 +39,7 @@ v{gradle-project-version}
:buildpacks-reference: https://buildpacks.io/docs :buildpacks-reference: https://buildpacks.io/docs
:paketo-reference: https://paketo.io/docs :paketo-reference: https://paketo.io/docs
:paketo-java-reference: {paketo-reference}/buildpacks/language-family-buildpacks/java :paketo-java-reference: {paketo-reference}/buildpacks/language-family-buildpacks/java
:nbt-gradle-plugin: https://graalvm.github.io/native-build-tools/latest/gradle-plugin.html :nbt-gradle-plugin: https://graalvm.github.io/native-build-tools/{native-build-tools-version}/gradle-plugin.html
@ -57,6 +57,8 @@ include::publishing.adoc[leveloffset=+1]
include::running.adoc[leveloffset=+1] include::running.adoc[leveloffset=+1]
include::aot.adoc[leveloffset=+1]
include::integrating-with-actuator.adoc[leveloffset=+1] include::integrating-with-actuator.adoc[leveloffset=+1]
include::reacting.adoc[leveloffset=+1] include::reacting.adoc[leveloffset=+1]

@ -70,9 +70,13 @@ When Gradle's {application-plugin}[`application` plugin] is applied to a project
When the {nbt-gradle-plugin}[GraalVM Native Image plugin] is applied to a project, the Spring Boot plugin: When the {nbt-gradle-plugin}[GraalVM Native Image plugin] is applied to a project, the Spring Boot plugin:
. Applies the `org.springframework.boot.aot` plugin that: . Applies the `org.springframework.boot.aot` plugin that:
.. Registers a `ProcessAot` task named `processAot` that will generate AOT-optimized source code for the application. .. Registers `aot` and `aotTest` source sets.
.. Registers a `ProcessAot` task named `processAot` that will generate AOT-optimized source for the application in the `aot` source set.
.. Configures the Java compilation and process resources tasks for the `aot` source set to depend upon `processAot`. .. Configures the Java compilation and process resources tasks for the `aot` source set to depend upon `processAot`.
. Adds the output of the `aot` source set to the classpath of the `nativeCompile` task. .. Registers a `ProcessTestAot` task named `processTestAot` that will generated AOT-optimized source for the application's tests in the `aotTest` source set.
.. Configures the Java compilation and process resources tasks for the `aotTest` source set to depend upon `processTestAot`.
. Adds the output of the `aot` source set to the classpath of the `main` GraalVM native binary.
. Adds the output of the `aotTest` source set to the classpath of the `test` GraalVM native binary.
. Configures the GraalVM extension to disable Toolchain detection. . Configures the GraalVM extension to disable Toolchain detection.

@ -0,0 +1,5 @@
plugins {
id 'org.springframework.boot' version '{gradle-project-version}'
id 'org.graalvm.buildtools.native' version '{native-build-tools-version}'
id 'java'
}

@ -0,0 +1,5 @@
plugins {
id("org.springframework.boot") version "{gradle-project-version}"
id("org.graalvm.buildtools.native") version "{native-build-tools-version}"
java
}
Loading…
Cancel
Save