You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/src/docs/asciidoc/end-to-end-gradle-guide.adoc
+85-62
Original file line number
Diff line number
Diff line change
@@ -13,10 +13,15 @@ If you are an advanced user, you can skip the getting started part and go direct
13
13
To compile your application ahead of time with https://www.graalvm.org/latest/reference-manual/native-image/[GraalVM Native Image] and Gradle, enable the Gradle plugin for Native Image building.
14
14
The plugin requires that you https://www.graalvm.org/downloads/[install GraalVM].
15
15
16
+
[TIP]
17
+
====
18
+
A JDK version between 17 and 21 is required to execute Gradle (see the https://docs.gradle.org/current/userguide/compatibility.html[Gradle Compatibility Matrix]).
19
+
====
20
+
16
21
[[add-plugin]]
17
22
=== Add the Plugin
18
23
19
-
- Add the plugin declaration into your _build.gradle_ file inside `plugins` block:
24
+
- Add the plugin declaration to your _build.gradle_ / _build.gradle.kts_ file inside the `plugins` block:
// common options you want to use for both main and test binaries
69
-
}
70
-
binaries.main {
71
-
// options you want to pass only to the main binary
72
-
}
73
-
binaries.test {
74
-
// options you want to pass only to the test binary
75
-
}
76
-
}
77
-
----
78
-
80
+
The plugin supports passing options directly to Native Image inside the `graalvmNative` block in the _build.gradle_ / _build.gradle.kts_ file.
79
81
Using `buildArgs.add("<option>")`, you can pass any Native Image build option listed on https://www.graalvm.org/reference-manual/native-image/overview/Options/[this website page].
82
+
You can pass options to configure the `main` or the `test` native binary, or both at the same time.
80
83
81
84
The plugin also provides special properties to configure the build:
82
85
83
-
- `mainClass` - The main class to use, defaults to the application.mainClass
86
+
- `mainClass` - Provides the main class to use, defaults to the `application.mainClass`
84
87
- `imageName` - Specifies of the name for the native executable file. If a custom name is not supplied, the artifact ID of the project will be used by default (defaults to the project name).
85
88
- `jvmArgs` - Passes the given argument directly to the JVM running the `native-image` tool
86
89
- `quickBuild` - Enables quick build mode
87
90
- `verbose` - Enables the verbose output
88
-
- <<gradle-plugin.adoc#native-image-options,and many more...>>
91
+
- and many more https://graalvm.github.io/native-build-tools/latest/gradle-plugin.html#_native_image_options[listed here].
Step 3. **Build an optimized native image with profiles**. The last phase consists in copying the generated profile, so that it’s automatically used when building the native binary.
207
-
The conventional location for profiles is `src/pgo-profiles/<name of the binary>` (main or test).
208
-
Copy the default.iprof file into that directory, then run:
213
+
Step 3. **Build an optimized native image with profiles**.
214
+
This step involves copying the generated profile to the conventional directory, ensuring it is automatically used during the build process.
215
+
Place the _default.iprof_ file in the _src/pgo-profiles/_ directory, and then run:
209
216
210
217
[source,bash, role="multi-language-sample"]
211
218
----
212
219
./gradlew nativeCompile
213
220
----
214
221
215
-
The profile will automatically be used and the binary compiled with PGO
216
-
After that, you can run your optimized executable with:
222
+
Once the optimized image is built, run it:
217
223
218
224
[source,bash, role="multi-language-sample"]
219
225
----
@@ -239,10 +245,10 @@ When you build a native image, Native Build Tools reference this repository to a
239
245
You can find an extensive list of libraries and frameworks from the Java ecosystem tested with Native Image on https://www.graalvm.org/native-image/libraries-and-frameworks/[this page].
240
246
====
241
247
242
-
However, it may happen, that your native image crashes at run time with a missing class or missing resource error.
248
+
However, it may happen, that your native image crashes at run time with a missing class or resource.
243
249
To address this, start by checking if any required configuration is missing.
244
250
245
-
The best way to detect missing metadata is by running your native tests in a CI/CD pipeline.
251
+
The best way to detect missing metadata is by running your native tests.
246
252
Alternatively, you can **identify missing configuration manually** using the following method.
247
253
248
254
- Pass the `--exact-reachablity-metadata` option to the `native-image` tool inside `graalvmNative` block of your _build.gradle_, as shown below:
@@ -267,7 +273,7 @@ graalvmNative {
267
273
}
268
274
----
269
275
270
-
- Rerun the application with Gradle:
276
+
- Rebuild and re-run the application:
271
277
272
278
[source,bash, role="multi-language-sample"]
273
279
----
@@ -290,7 +296,7 @@ The easiest way to collect the missing metadata is by using the https://www.graa
290
296
This agent tracks all usages of dynamic features during application execution on the JVM and generates the necessary configuration.
291
297
292
298
The agent is disabled by default.
293
-
You can enable it on the command line or inside `graalvmNative` block of your _build.gradle_.
299
+
You can enable it on the command line or inside the `graalvmNative` block in _build.gradle_ / _build.gradle.kts_.
294
300
295
301
To enable the agent via the command line, pass the `-Pagent` option when running Gradle:
296
302
@@ -304,36 +310,40 @@ To enable the agent via the command line, pass the `-Pagent` option when running
304
310
Enabling the agent via the command line only attaches it for a specific run; it does not automatically run every time you build the application.
305
311
====
306
312
307
-
To enable the agent in _build.gradle_ and collect missing metadata, do the following.
313
+
To enable the agent inside the build configuration and collect missing metadata, do the following.
308
314
309
315
Step 1: **Enable the agent** by setting `agent` to `true` in the `graalvmNative` block:
From that point on, commands you execute run with the agent attached.
339
+
From that point on, commands you execute will run with the agent attached.
330
340
By default, the agent creates the metadata in the _build/native/agent-output_ directory.
331
341
332
342
Step 2: **Copy the generated metadata** from the default location, _build/native/agent-output_, to the resources directory, for example, _resources/META-INF/_.
333
343
To do that with Gradle, configure and run the `metadataCopy` task.
334
344
335
345
Add a new task named `metadataCopy` inside the `graalvmNative` block.
- `inputTaskNames` - specifies tasks previously executed with the agent attached (tasks that generated metadata in the last step).
365
-
- `outputDirectories` - location where you want to copy the generated metadata
366
-
- `mergeWithExisting` - specifies whether the metadata you want to copy, should be merged with the metadata that already exists on the give location, or not. This only makes sense when there is already some existing metadata, created before.
374
+
- `inputTaskNames` - specifies tasks previously executed with the agent attached (tasks that generated metadata in the last step)
375
+
- `outputDirectories` - specifies the location where you want to copy the generated metadata
376
+
- `mergeWithExisting` - specifies whether the metadata you want to copy should be merged with the metadata that already exists on the give location, or not. This only makes sense when there is already some existing metadata, created before.
367
377
368
-
Step 3: Now that the `metadataCopy` task is configured, **run the metadataCopy task** with:
378
+
Step 3: Now that the `metadataCopy` task is configured, **run the metadataCopy task**:
Step 4: Finally, when you build the native image again, with `./gradlew nativeRun`, metadata will be picked automatically from the resources directory.
385
+
Step 4: Finally, **build the native image with the metadata**:
386
+
387
+
[source,bash, role="multi-language-sample"]
388
+
----
389
+
./gradlew nativeCompile
390
+
----
391
+
392
+
Run it:
393
+
394
+
[source,bash, role="multi-language-sample"]
395
+
----
396
+
./gradlew nativeRun
397
+
----
376
398
377
-
If your native image is successfully build, but fails at run time, check the troubleshooting guide https://www.graalvm.org/reference-manual/native-image/guides/troubleshoot-run-time-errors/[Troubleshoot Native Image Run-Time Errors].
399
+
If your native image is successfully build, but still fails at run time, check the troubleshooting guide https://www.graalvm.org/reference-manual/native-image/guides/troubleshoot-run-time-errors/[Troubleshoot Native Image Run-Time Errors].
378
400
379
401
Learn more about how to fine-tune the agent further <<gradle-plugin.adoc#native-image-tracing-agent,here>>.
380
402
381
403
[[use-diagnostics-tools]]
382
404
=== Use Diagnostics Tools
383
405
384
-
If you need to diagnose the native applications you generate, or monitor your Java application when launched from a native executable, Native Image offers tools for debugging and analyzing the produced binary.
406
+
If you need to diagnose the native applications you build, or monitor your Java application when launched from a native executable, Native Image offers tools for debugging and analyzing the produced binary.
385
407
For example:
386
408
387
409
[source,groovy, role="multi-language-sample"]
@@ -407,7 +429,8 @@ graalvmNative {
407
429
----
408
430
409
431
- The `--emit build-report` option generates an HTML page report alongside the native executable that you can open in a browser.
410
-
It provides broad information about each build stage as well as the generated binary’s contents. You can read more about Build Report features https://www.graalvm.org/latest/reference-manual/native-image/overview/build-report/[here].
432
+
It provides broad information about each build stage as well as the generated binary’s contents.
433
+
You can read more about Build Report features https://www.graalvm.org/latest/reference-manual/native-image/overview/build-report/[here].
411
434
412
435
[TIP]
413
436
====
@@ -423,4 +446,4 @@ All the monitoring and debugging tools https://www.graalvm.org/reference-manual/
423
446
424
447
=== Learn more
425
448
426
-
To continue learning, refer to the <<changelog.adoc#,extensive reference documentation for the GraalVM Native Image Maven plugin>>.
449
+
To continue learning, refer to the <<changelog.adoc#,extensive reference documentation for the GraalVM Native Image Gradle plugin>>.
0 commit comments