|
1 | 1 | [[aot]]
|
2 | 2 | = Ahead-of-Time Processing
|
3 |
| -Spring AOT is a process that analyzes your code at build-time in order to generate an optimized version of it. |
4 |
| -It is most often used to help generate GraalVM native images. |
| 3 | + |
| 4 | +Spring AOT is a process that analyzes your application at build-time and generate an optimized version of it. |
| 5 | +It is a mandatory step to run a Spring `ApplicationContext` in a native image. |
| 6 | + |
| 7 | +NOTE: For an overview of GraalVM Native Images support in Spring Boot, check the {spring-boot-reference}/#native-image[reference documentation]. |
5 | 8 |
|
6 | 9 | The Spring Boot Maven plugin offers goals that can be used to perform AOT processing on both application and test code.
|
7 | 10 |
|
8 | 11 |
|
9 | 12 |
|
10 | 13 | == Processing Applications
|
11 |
| -Based on your `@SpringBootApplication`-annotated main class, the AOT engine 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. |
12 |
| -Additional post-processing of the factory is possible using callbacks. |
13 |
| -For instance, these are used to generate the necessary reflection configuration that GraalVM needs to initialize the context in a native image. |
14 |
| - |
15 | 14 | To configure your application to use this feature, add an execution for the `process-aot` goal, as shown in the following example:
|
16 | 15 |
|
17 | 16 | [source,xml,indent=0,subs="verbatim,attributes",tabsize=4]
|
18 | 17 | ----
|
19 | 18 | include::../maven/aot/pom.xml[tags=aot]
|
20 | 19 | ----
|
21 | 20 |
|
22 |
| -TIP: If you are using `spring-boot-starter-parent`, this execution is automatically configured if you enable the `native` profile. |
23 |
| - |
24 | 21 | As the `BeanFactory` is fully prepared at build-time, conditions are also evaluated.
|
25 | 22 | This has an important difference compared to what a regular Spring Boot application does at runtime.
|
26 | 23 | 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.
|
27 | 24 | The `process-aot` goal shares a number of properties with the <<run,run goal>> for that reason.
|
28 | 25 |
|
| 26 | + |
| 27 | + |
| 28 | +=== Using the Native Profile |
| 29 | +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. |
| 30 | + |
| 31 | +The `native` profile configures the following: |
| 32 | + |
| 33 | +* Execution of `process-aot` when the Spring Boot Maven Plugin is applied on a project. |
| 34 | +* Suitable settings so that <<build-image,build-image>> generates a native image. |
| 35 | +* Sensible defaults for the {nbt-reference}[Native Build Tools Maven Plugin], in particular: |
| 36 | +** Making sure the plugin uses the raw classpath, and not the main jar file as it does not understand our repackaged jar format. |
| 37 | +** Validate that a suitable GraalVM version is available. |
| 38 | +** Download third-party reachability metadata. |
| 39 | + |
| 40 | +To benefit from the `native` profile, a module that represents an application should define two plugins, as shown in the following example: |
| 41 | + |
| 42 | +[source,xml,indent=0,subs="verbatim,attributes",tabsize=4] |
| 43 | +---- |
| 44 | +include::../maven/aot-native/pom.xml[tags=aot-native] |
| 45 | +---- |
| 46 | + |
| 47 | +A single project can trigger the generation of a native image on the command-line using either {spring-boot-reference}/#native-image.developing-your-first-application.buildpacks.maven[Cloud Native Buildpacks] or {spring-boot-reference}/#native-image.developing-your-first-application.native-build-tools.maven[Native Image Build Tools]. |
| 48 | + |
| 49 | +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. |
| 50 | + |
| 51 | +To bind Cloud Native Buildpacks during the `package` phase, add the following to the root POM of your multi-modules project: |
| 52 | + |
| 53 | +[source,xml,indent=0,subs="verbatim,attributes",tabsize=4] |
| 54 | +---- |
| 55 | +include::../maven/aot-native-profile-buildpacks/pom.xml[tags=profile] |
| 56 | +---- |
| 57 | + |
| 58 | +The example below does the same for Native Build Tools: |
| 59 | + |
| 60 | +[source,xml,indent=0,subs="verbatim,attributes",tabsize=4] |
| 61 | +---- |
| 62 | +include::../maven/aot-native-profile-nbt/pom.xml[tags=profile] |
| 63 | +---- |
| 64 | + |
| 65 | +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: |
| 66 | + |
| 67 | +[indent=0] |
| 68 | +---- |
| 69 | + $ mvn package -Pnative |
| 70 | +---- |
| 71 | + |
| 72 | +NOTE: A "relevant" sub-module is a module that represents a Spring Boot application. |
| 73 | +Such module must define the Native Build Tools and Spring Boot plugins as described above. |
| 74 | + |
| 75 | + |
| 76 | + |
29 | 77 | include::goals/process-aot.adoc[leveloffset=+1]
|
30 | 78 |
|
31 | 79 |
|
|
0 commit comments