diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/build.gradle b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/build.gradle index c9115ea16e..72fb53e212 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/build.gradle +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/build.gradle @@ -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 { options { author = true diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/docs/asciidoc/aot.adoc b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/docs/asciidoc/aot.adoc new file mode 100644 index 0000000000..c6c9e73f63 --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/docs/asciidoc/aot.adoc @@ -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. \ No newline at end of file diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/docs/asciidoc/index.adoc b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/docs/asciidoc/index.adoc index 94f10173b9..9d5d66f2ce 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/docs/asciidoc/index.adoc +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/docs/asciidoc/index.adoc @@ -39,7 +39,7 @@ v{gradle-project-version} :buildpacks-reference: https://buildpacks.io/docs :paketo-reference: https://paketo.io/docs :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::aot.adoc[leveloffset=+1] + include::integrating-with-actuator.adoc[leveloffset=+1] include::reacting.adoc[leveloffset=+1] diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/docs/asciidoc/reacting.adoc b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/docs/asciidoc/reacting.adoc index 9042b53840..49b124971a 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/docs/asciidoc/reacting.adoc +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/docs/asciidoc/reacting.adoc @@ -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: . 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`. -. 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. diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/docs/gradle/aot/apply-native-image-plugin.gradle b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/docs/gradle/aot/apply-native-image-plugin.gradle new file mode 100644 index 0000000000..3e25cb898c --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/docs/gradle/aot/apply-native-image-plugin.gradle @@ -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' +} \ No newline at end of file diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/docs/gradle/aot/apply-native-image-plugin.gradle.kts b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/docs/gradle/aot/apply-native-image-plugin.gradle.kts new file mode 100644 index 0000000000..27dba95925 --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/docs/gradle/aot/apply-native-image-plugin.gradle.kts @@ -0,0 +1,5 @@ +plugins { + id("org.springframework.boot") version "{gradle-project-version}" + id("org.graalvm.buildtools.native") version "{native-build-tools-version}" + java +}