Add GraalVM Native Image Support to Java

Add GraalVM Native Image Support to Java

Guides the AI to add GraalVM Native Image support to Spring Boot, Micronaut or plain Java projects, building and fixing errors until the native binary runs.

Skill files7 files

references/

SKILL.md113 lines
1
---
2
name: java-add-graalvm-native-image-support
3
description: 'GraalVM Native Image skill for Spring Boot, Micronaut and plain Java: detects the build tool and framework, adds native image support, builds the executable, analyzes errors, and iterates until the native build succeeds and the binary runs correctly. For Quarkus projects it only dispatches to the quarkus-native-and-container skill.'
4
---
5
6
# GraalVM Native Image Support
7
8
Single source of truth for adding GraalVM Native Image to a Java project, whatever the framework. Work iteratively: configure, build, read the error, fix, rebuild, until the native build succeeds and the executable runs correctly.
9
10
Detail lives in `references/`; load only what the project needs:
11
12
| Topic | File |
13
|---|---|
14
| Maven / Gradle native plugin setup | [references/build-tools.md](references/build-tools.md) |
15
| Reachability metadata and tracing agent | [references/reachability-metadata.md](references/reachability-metadata.md) |
16
| Common build/runtime errors and fixes | [references/troubleshooting.md](references/troubleshooting.md) |
17
| Spring Boot (AOT, `-Pnative`, `nativeCompile`, `build-image`) | [references/spring-boot.md](references/spring-boot.md) |
18
| Quarkus | `quarkus-native-and-container` skill (hand off); short pointer in [references/quarkus.md](references/quarkus.md) |
19
| Micronaut | [references/micronaut.md](references/micronaut.md) |
20
21
## Step 1: Analyze the project
22
23
1. Build tool: `pom.xml` (Maven) or `build.gradle` / `build.gradle.kts` (Gradle). Prefer the wrapper (`./mvnw`, `./gradlew`) when present.
24
2. Framework, from dependencies/plugins:
25
- Spring Boot: `spring-boot-starter-parent`, `spring-boot-maven-plugin`, or `org.springframework.boot` Gradle plugin
26
- Quarkus: `io.quarkus` BOM/plugin or `quarkus-*` dependencies
27
- Micronaut: `io.micronaut` parent/plugin or `micronaut-*` dependencies
28
- otherwise plain Java
29
3. Existing native configuration: native-build-tools plugin, `META-INF/native-image/`, native profile, `application.properties` native keys.
30
4. Toolchain: run `java -version` and `native-image --version`. Local native compilation needs a GraalVM JDK (with `native-image`) as the JDK used for the build. Container or buildpack builds (Spring Boot buildpacks via `bootBuildImage`/`spring-boot:build-image`, Quarkus container builds) compile inside a container and need no local GraalVM, only a container runtime. Check the project's Java level against the installed GraalVM version.
31
32
If the framework is Quarkus, stop here and hand off to the `quarkus-native-and-container` skill. Container builds there need no local GraalVM.
33
34
## Scope and JVM baseline
35
36
- Adding native support does not authorize a JDK, framework, library, build-tool, or wrapper upgrade. Preserve the existing BOM/parent-managed versions and test conventions; ask before changing versions to solve native compatibility.
37
- Run the project's existing JVM build and tests before editing. Record pre-existing failures rather than weakening checks to hide them. Keep the JVM packaging/run path working alongside native support.
38
- After any approved dependency or framework change, rerun that JVM baseline before continuing with native builds. Verify the same relevant behavior on both JVM and native paths.
39
- Ask before creating or changing CI workflows, adding native CI jobs, or changing runner resources. Suggest commands separately if CI changes were not requested.
40
41
## Step 2: Get a GraalVM toolchain
42
43
- **Oracle GraalVM** (free under the GraalVM Free Terms and Conditions) and **GraalVM Community Edition** builds provide `native-image`. Use a release matching the project's Java version (for example, GraalVM for JDK 21 or newer; Spring Boot 3.x/4.x need a Java 17+ baseline, see the Spring reference).
44
- Install with SDKMAN (`sdk list java`, then `sdk install java <identifier>` using an identifier from that list), a vendor download, or a CI action such as `graalvm/setup-graalvm`. Pick the version from the vendor's current download page rather than hard-coding one.
45
- Linux needs a C toolchain and zlib headers (`build-essential`, `zlib1g-dev`); macOS needs Xcode command line tools; Windows needs Visual Studio Build Tools. Check the current GraalVM "Native Image prerequisites" page.
46
47
## Step 3: Add native support
48
49
Per framework:
50
51
- **Spring Boot**: with Maven, `spring-boot-starter-parent` only manages the native plugins' versions and configuration in its `native` profile, so declare `native-maven-plugin` and `spring-boot-maven-plugin` in `<build><plugins>` if missing (no versions needed); with Gradle, apply `org.graalvm.buildtools.native` next to `org.springframework.boot`; a custom parent needs the plugin added by hand. Do not duplicate configuration the parent already provides. See [references/spring-boot.md](references/spring-boot.md).
52
- **Quarkus**: hand off to the `quarkus-native-and-container` skill.
53
- **Micronaut**: built in via the Micronaut build plugins. See [references/micronaut.md](references/micronaut.md).
54
- **Plain Java** (or any project without a framework plugin): add the GraalVM Native Build Tools plugin. See [references/build-tools.md](references/build-tools.md). Resolve the plugin version at run time from Maven Central (`org.graalvm.buildtools:native-maven-plugin`) or the Gradle Plugin Portal (`org.graalvm.buildtools.native`) and pin it in the build file; do not copy a version from memory.
55
56
## Step 4: Build
57
58
| Setup | Command |
59
|---|---|
60
| Plain Maven | `./mvnw -Pnative native:compile` |
61
| Plain Gradle | `./gradlew nativeCompile` |
62
| Spring Boot Maven | `./mvnw -Pnative native:compile` (or `spring-boot:build-image` for a container) |
63
| Spring Boot Gradle | `./gradlew nativeCompile` (or `bootBuildImage`) |
64
| Micronaut Maven | `./mvnw package -Dpackaging=native-image` |
65
| Micronaut Gradle | `./gradlew nativeCompile` |
66
67
Native builds are slow and memory hungry (several GB of RAM). Capture the full output to inspect it.
68
69
## Step 5: Iterate until the build succeeds
70
71
1. Read the first real error, not the final summary.
72
2. Match it against [references/troubleshooting.md](references/troubleshooting.md).
73
3. Apply the smallest fix, in this order of preference:
74
1. Check whether a compatible library/framework version has built-in native support (see the [reachability metadata repository](https://github.com/oracle/graalvm-reachability-metadata)). Propose the upgrade; apply it only with user approval and rerun the JVM baseline. If declined, try metadata or framework hints compatible with the current versions.
75
2. Use the framework's own mechanism (Spring `RuntimeHints`, Quarkus `@RegisterForReflection`, Micronaut `@Introspected`).
76
3. Collect metadata with the tracing agent (see [references/reachability-metadata.md](references/reachability-metadata.md)).
77
4. Hand-write metadata under `src/main/resources/META-INF/native-image/<groupId>/<artifactId>/`.
78
5. Adjust initialization (`--initialize-at-build-time` / `--initialize-at-run-time`) only for specific classes.
79
4. Rebuild. Repeat until the build completes with no errors.
80
81
## Step 6: Verify the executable
82
83
A successful build is not enough: many problems only appear at runtime.
84
85
- Run the binary and exercise the main paths (HTTP endpoints, DB access, serialization, resource loading, scheduled jobs).
86
- Run the native tests where the framework supports them (`native:test`, `nativeTest`, Quarkus `@QuarkusIntegrationTest`).
87
- Compare startup time and memory against the JVM run only if the user asks for it.
88
- Report the exact commands run, whether the build and the run succeeded, and any remaining warnings.
89
90
## Pitfalls
91
92
- Recommend a native build and native test run in CI with an explicit GraalVM version; add or change CI only with user consent.
93
- Mockito's default inline mock maker does not work on native image (Mockito 5.0.0 release notes); use the subclass mock maker for native tests.
94
- Do not leave `--report-unsupported-elements-at-runtime` or verbose flags in production builds unless justified.
95
96
## References
97
98
- [GraalVM Native Image documentation](https://www.graalvm.org/latest/reference-manual/native-image/)
99
- [Native Build Tools](https://graalvm.github.io/native-build-tools/latest/index.html)
100
- [GraalVM reachability metadata repository](https://github.com/oracle/graalvm-reachability-metadata)
101
- [Spring Boot GraalVM native images](https://docs.spring.io/spring-boot/reference/packaging/native-image/index.html)
102
- [Quarkus building a native executable](https://quarkus.io/guides/building-native-image)
103
- [Micronaut GraalVM guide](https://docs.micronaut.io/latest/guide/#graal)
104
105
## Upstream sources (read before upgrading GraalVM or native config)
106
107
Fetch the page for the target version; prefer it over this skill when they conflict.
108
109
- GraalVM release notes: <https://www.graalvm.org/release-notes/>
110
- Reachability metadata format: <https://www.graalvm.org/latest/reference-manual/native-image/metadata/>
111
- Build configuration options: <https://www.graalvm.org/latest/reference-manual/native-image/overview/BuildConfiguration/>
112
- Metadata repository (check your library versions are covered): <https://github.com/oracle/graalvm-reachability-metadata>
113

© GitHub, Inc. · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 14,621 prompts

More by @OpenSourceBuffGuy

See all
Sea turtle carrying floating film studio island prompt image

Sea turtle carrying floating film studio island

Draws a giant sea turtle carrying a floating island film studio through a starry sky, where fairies polish rough film strips into golden film.

image-prompt, nano-banana, 3d-render, cartoon, fantasy-world, creatures, animals, whimsical, dreamy, cinematic, dramatic-lighting, vibrant-colors, bokeh, miniature, widescreen, night-scene, illustration, concept-art, surreal, storytelling, clay-3d

Desert buggy escapes drones past industrial outpost prompt image

Desert buggy escapes drones past industrial outpost

Builds a 15-second desert chase video where a racer in an armored buggy escapes drones past an industrial outpost, using five reference images with set jobs.

video-prompt, hailuo, action, multi-shot, timed-beats, camera-movement, reference-image, character-consistency, negative-prompt, cinematic, sci-fi, cars, landscape, aerial-view, tracking-shot, photorealistic, sound-effects, structured-output, long-form, widescreen

1 view
Cozy Pixel Art Farm Island Village prompt image

Cozy Pixel Art Farm Island Village

Draws a cozy 16-bit pixel-art farming village across six small islands joined by bridges, with exact counts for buildings, animals and crops.

image-prompt, gpt-image, pixel-art, isometric, game-assets, concept-art, maps, worldbuilding, cozy, vibrant-colors, ocean, landscape, wide-shot, widescreen, game-development, video-games, structured-output, whimsical, summer, high-angle, aerial-view, animals, negative-prompt, cityscape

1 view

Reviews

Loading reviews…

Discussion

Loading comments…