|
| 1 | +[[executable-jar-to-native]] |
| 2 | +== Executable JAR to native |
| 3 | + |
| 4 | +It is possible to turn a https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-executable-jar-format.html[Spring Boot executable JAR] to a native executable or a container image containing a native executable. This can be useful for various use cases: |
| 5 | + |
| 6 | +* Keep the regular JVM pipeline and turn the JVM Spring Boot application to native on the CI/CD platform. |
| 7 | +* Keep an architecture neutral deployment artifact (`native-image` https://github.com/oracle/graal/issues/407[does not support cross-compilation]). |
| 8 | + |
| 9 | +WARNING: A mandatory pre-requisite is to use <<spring-aot>> Maven or Gradle upstream to build the Spring Boot executable JAR. |
| 10 | + |
| 11 | +=== With Buildpacks |
| 12 | + |
| 13 | +With Spring Boot, https://buildpacks.io/[Buildpacks] is usually used with the Maven (`mvn spring-boot:build-image`) or Gradle (`gradle bootBuilImage`) integration. But you can also use directly https://buildpacks.io/docs/tools/pack/[the `pack` CLI] to turn a Spring Boot executable JAR built with <<spring-aot>> into an optimized container image: |
| 14 | + |
| 15 | +* Prerequisite: a Docker daemon needs to be available, https://hub.docker.com/search?type=edition&offering=community[locally] or https://buildpacks.io/docs/app-developer-guide/build-a-windows-app/#using-remote-docker-hosts[remotely]. |
| 16 | +* https://buildpacks.io/docs/tools/pack/[Install `pack`]. |
| 17 | +* Adapt to your project and run `pack build --builder paketobuildpacks/builder:tiny --path target/my-app-0.0.1-SNAPSHOT.jar --env 'BP_NATIVE_IMAGE=true' my-app:0.0.1` |
| 18 | + |
| 19 | +NOTE: This does not require a local `native-image` installation. |
| 20 | + |
| 21 | +=== With `native-image` |
| 22 | + |
| 23 | +Another option is to turn a Spring Boot executable JAR built with <<spring-aot>> into a native executable using GraalVM `native-image` compiler: |
| 24 | + |
| 25 | +* <<getting-started-native-image-system-requirements,Install native-image>>. |
| 26 | +* Adapt to your project and run: |
| 27 | + |
| 28 | +``` |
| 29 | +#!/usr/bin/env bash |
| 30 | + |
| 31 | +rm -rf target/native-image |
| 32 | +mkdir -p target/native-image |
| 33 | +cd target/native-image |
| 34 | +jar -xvf ../my-app-0.0.1-SNAPSHOT-exec.jar >/dev/null 2>&1 |
| 35 | +cp -R META-INF BOOT-INF/classes |
| 36 | +native-image -H:Name=my-app -cp BOOT-INF/classes:`find BOOT-INF/lib | tr '\n' ':'` |
| 37 | +mv my-app ../ |
| 38 | +``` |
| 39 | + |
| 40 | +NOTE: This is documented as a simple bash script but can be adapted to whatever is suitable to you environment. |
0 commit comments