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