Two useful ways you can use AOT with Spring Boot:
Using a docker image
Besides of having Java >= 17 and Gradle >= 7.3 installed, in your build.gradle you will add the following plugin:
id 'org.graalvm.buildtools.native' version '0.9.23'
An initial build.gradle would look like this:
plugins {
id 'java'
id 'org.springframework.boot' version '3.1.2'
id 'io.spring.dependency-management' version '1.1.2'
id 'org.graalvm.buildtools.native' version '0.9.23'
}
group = 'com.test'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = '17'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
That's it. Now you can build your docker image running the follwing command:
gradle bootBuildImage
Now you can run docker images
to see the image you just created and running it:
docker run --rm <image_id>
Compiling it to run as a native app
In this case, you should apply the same plugin shown in the example above. Besides, you will have to have GraalVM >= 17 and Gradle >= 7.3 installed. Then, you can run the follwoing command to build it:
gradle nativeCompile
and to run your application, you can just run:
/<app_name>/build/native/nativeCompile/<app_name>
And that'all. It's important to mention that some Spring features may not work properly due to Closed World assumption, which can make reflection harder to implement.