Spring Boot: Native image witrh override OpenJDK distribution

160 Views Asked by At

I want to configure building a Native image with GraalVM and Spring Boot buildpacks

Spring-Boot 3.1.5
Java 21

I have configured the spring-boot-maven-plugin, and added the BP_NATIVE_IMAG environment variable to enable native image.

In order for it to use Adoptium (Eclipse Temurin) OpenJDK instead of BellSoft Liberica, I have to configure all the needed buildpacks my self in the plugin configuration.

    <build>
        <finalName>app</finalName>
        <plugins>
            <plugin>
                <groupId>org.graalvm.buildtools</groupId>
                <artifactId>native-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                    <image>
                        <builder>paketobuildpacks/builder-jammy-tiny</builder>
                        <createdDate>${createdDate}</createdDate>
                        <env>
                            <BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>
                        </env>
                        <buildpacks>
                            <buildpack>gcr.io/paketo-buildpacks/ca-certificates:latest</buildpack>
                            <buildpack>gcr.io/paketo-buildpacks/adoptium:latest</buildpack>
                            <buildpack>gcr.io/paketo-buildpacks/syft:latest</buildpack>
                            <buildpack>gcr.io/paketo-buildpacks/executable-jar:latest</buildpack>
                            <buildpack>gcr.io/paketo-buildpacks/dist-zip:latest</buildpack>
                            <buildpack>gcr.io/paketo-buildpacks/spring-boot:latest</buildpack>
                        </buildpacks>
                    </image>
                </configuration>
            </plugin>
        </plugins>
    </build>

If I remove the list of buildpacks, then it will build a native image with GraalVM.

[INFO]     [creator]     6 of 15 buildpacks participating
[INFO]     [creator]     paketo-buildpacks/ca-certificates   3.6.6
[INFO]     [creator]     paketo-buildpacks/bellsoft-liberica 10.4.2
[INFO]     [creator]     paketo-buildpacks/syft              1.39.0
[INFO]     [creator]     paketo-buildpacks/executable-jar    6.8.2
[INFO]     [creator]     paketo-buildpacks/spring-boot       5.27.5
[INFO]     [creator]     paketo-buildpacks/native-image      5.12.6

However I want to use a different OpenJDK. So I need to list all the buildpacks. I tried to add the native-image buildpack to the list of buildpacks, but it does not work. What buildpack am I missing here?

[INFO]     [creator]       no valid dependencies for native-image-svm, 21, and io.buildpacks.stacks.jammy.tiny in [(jdk, 8.0.392, [*]) (jre, 8.0.392, [*]) (jdk, 11.0.21, [*]) (jre, 11.0.21, [*]) (jdk, 17.0.9, [*]) (jre, 17.0.9, [*]) (jdk, 21.0.1, [*]) (jre, 21.0.1, [*])]

If I change adoptium:latest to bellsoft-liberica:latest, then it will work.

If I change the adoptium:latest buildpack to graalvm:latest buildpack then it is working.

When it is using BellSoft Liberica and building a native image, does it not use the BellSoft Liberica OpenJDK in the build process?

If I use GraalVM as OpenJDK distribution the docker images get a whopping 650MB, while building a native image with BellSoft Liberica the docker images is only 85MB.

0

There are 0 best solutions below