Skip to content

Commit f99dc5c

Browse files
committed
Proofread End-to-end Gradle guide by D. Nestorovic
1 parent 394ab0a commit f99dc5c

File tree

2 files changed

+105
-79
lines changed

2 files changed

+105
-79
lines changed

docs/src/docs/asciidoc/end-to-end-gradle-guide.adoc

+85-62
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@ If you are an advanced user, you can skip the getting started part and go direct
1313
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.
1414
The plugin requires that you https://www.graalvm.org/downloads/[install GraalVM].
1515

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+
1621
[[add-plugin]]
1722
=== Add the Plugin
1823

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:
2025

2126
[source,groovy,subs="verbatim,attributes", role="multi-language-sample"]
2227
----
@@ -28,13 +33,27 @@ The plugin requires that you https://www.graalvm.org/downloads/[install GraalVM]
2833
id("org.graalvm.buildtools.native") version "{gradle-plugin-version}"
2934
----
3035

31-
All plugin versions are listed https://github.com/graalvm/native-build-tools/releases[here]
36+
Replace `{maven-plugin-version}` with the latest released version.
37+
All plugin versions are listed https://github.com/graalvm/native-build-tools/releases[here].
38+
39+
40+
- Provide the application main class in _build.gradle_ / _build.gradle.kts_ as shown below.
41+
Adjust the path according to your application sources.
42+
43+
[source,groovy,subs="verbatim,attributes", role="multi-language-sample"]
44+
----
45+
application {
46+
mainClass.set('org.example.Main')
47+
}
48+
----
3249

3350
[[run-your-project]]
34-
=== Build and run your project
51+
=== Build and Run Your Application
52+
53+
This plugin works with the `application` plugin and registers a number of tasks and extensions for you to configure.
3554

3655
- Build a native executable of your application.
37-
This command will compile your application and create a native executable in the _build/native/nativeCompile/_ directory.
56+
This command will compile your application and create a native executable in the _build/native/nativeCompile/_ directory:
3857

3958
[source,bash,role="multi-language-sample"]
4059
----
@@ -58,52 +77,36 @@ For advanced use cases, this guide provides instructions for configuring the bui
5877
[[configure-image-build]]
5978
=== Configure Native Image Build
6079

61-
The plugin supports passing options directly to Native Image inside the `graalvmNative` block inside _build.gradle_ file.
62-
You can apply configuring options specific to the main or the test binary (or you can provide options that applies to all binaries) like following:
63-
64-
[source,groovy,subs="verbatim,attributes", role="multi-language-sample"]
65-
----
66-
graalvmNative {
67-
binaries.all {
68-
// 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.
7981
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.
8083

8184
The plugin also provides special properties to configure the build:
8285

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`
8487
- `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).
8588
- `jvmArgs` - Passes the given argument directly to the JVM running the `native-image` tool
8689
- `quickBuild` - Enables quick build mode
8790
- `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].
8992

9093
Here is an example of additional options usage:
9194

9295
[source,groovy,subs="verbatim,attributes", role="multi-language-sample"]
9396
----
9497
graalvmNative {
9598
binaries.all {
96-
buildArgs.add('--emit build-report')
99+
// common options
100+
jvmArgs.add('flag')
97101
}
98-
99102
binaries.main {
103+
// options to configure the main binary
100104
imageName = 'application'
101105
mainClass = 'org.test.Main'
102-
103-
buildArgs.add('-O3')
106+
buildArgs.add('-O3') // enables additional compiler optimizations
104107
}
105-
106108
binaries.test {
109+
// options to configure the test binary
107110
quickBuild = true
108111
debug = true
109112
verbose = true
@@ -115,17 +118,19 @@ graalvmNative {
115118
----
116119
graalvmNative {
117120
binaries.all {
121+
// common options
118122
buildArgs.add('--emit build-report')
119123
}
120124
121125
binaries.main {
126+
// options to configure the main binary
122127
imageName.set('application')
123128
mainClass.set('org.test.Main')
124-
125-
buildArgs.add('-O3')
129+
buildArgs.add('-O3') // enables additional compiler optimizations
126130
}
127131
128132
binaries.test {
133+
// options to configure the test binary
129134
quickBuild.set(true)
130135
debug.set(true)
131136
verbose.set(true)
@@ -136,7 +141,7 @@ graalvmNative {
136141
[TIP]
137142
====
138143
As an alternative, you can pass additional build options via the `NATIVE_IMAGE_OPTIONS` environment variable, on the command line.
139-
This works similarly to `JAVA_TOOL_OPTIONS``, where the value of the environment variable is prefixed to the options supplied to `native-image`.
144+
This works similarly to `JAVA_TOOL_OPTIONS`, where the value of the environment variable is prefixed to the options supplied to `native-image`.
140145
====
141146

142147
Learn more about Native Image build configuration https://www.graalvm.org/reference-manual/native-image/overview/BuildConfiguration/[on the website].
@@ -147,7 +152,7 @@ Learn more about Native Image build configuration https://www.graalvm.org/refere
147152
This plugin supports running tests on the JUnit Platform.
148153
The tests are compiled ahead of time and executed as native code.
149154

150-
- Add the JUnit 5 dependency to _build.gradle_ to include the testing framework. For example:
155+
- Add the JUnit 5 dependency to _build.gradle_ / _build.gradle.kts_ to include the testing framework:
151156

152157
[source,groovy,role="multi-language-sample"]
153158
----
@@ -170,6 +175,9 @@ testImplementation('junit:junit:4.13.2')
170175
./gradlew nativeTest
171176
----
172177

178+
First, the tests run on the JVM.
179+
Then, they are compiled ahead of time and executed as native code.
180+
173181
[[gather-execution-profiles]]
174182
=== Gather Execution Profiles and Build Optimized Images
175183

@@ -185,35 +193,33 @@ The PGO workflow includes three steps.
185193
PGO is available in Oracle GraalVM.
186194
====
187195

188-
Step 1: **Build an instrumented native image** by passing the `--pgo-instrument` option the compile command:
196+
Step 1: **Build an instrumented native image** by passing the `--pgo-instrument` option directly to the compile command:
189197

190198
[source,bash, role="multi-language-sample"]
191199
----
192200
./gradlew nativeCompile --pgo-instrument
193201
----
194202

195-
This will generate a binary under _build/native/nativeCompile_ with the _-instrumented_ suffix.
203+
This generates a native executable under _build/native/nativeCompile_ with the _-instrumented_ suffix.
196204

197205
Step 2: **Gather profiles** by running the instrumented executable.
198206
By default, the _default.iprof_ file, if not specified otherwise, is generated alongside the native executable.
199207

200208
[source,bash, role="multi-language-sample"]
201209
----
202-
cd build/native/nativeCompile/
203-
./my-application-instrumented`
210+
./build/native/nativeCompile/my-application-instrumented
204211
----
205212

206-
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:
209216

210217
[source,bash, role="multi-language-sample"]
211218
----
212219
./gradlew nativeCompile
213220
----
214221

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:
217223

218224
[source,bash, role="multi-language-sample"]
219225
----
@@ -239,10 +245,10 @@ When you build a native image, Native Build Tools reference this repository to a
239245
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].
240246
====
241247

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.
243249
To address this, start by checking if any required configuration is missing.
244250

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.
246252
Alternatively, you can **identify missing configuration manually** using the following method.
247253

248254
- 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 {
267273
}
268274
----
269275

270-
- Rerun the application with Gradle:
276+
- Rebuild and re-run the application:
271277

272278
[source,bash, role="multi-language-sample"]
273279
----
@@ -290,7 +296,7 @@ The easiest way to collect the missing metadata is by using the https://www.graa
290296
This agent tracks all usages of dynamic features during application execution on the JVM and generates the necessary configuration.
291297

292298
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_.
294300

295301
To enable the agent via the command line, pass the `-Pagent` option when running Gradle:
296302

@@ -304,36 +310,40 @@ To enable the agent via the command line, pass the `-Pagent` option when running
304310
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.
305311
====
306312

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.
308314

309315
Step 1: **Enable the agent** by setting `agent` to `true` in the `graalvmNative` block:
310316

311317
[source,groovy,subs="verbatim,attributes", role="multi-language-sample"]
312318
----
313319
graalvmNative {
314-
agent {
315-
enabled = true
320+
binaries.all {
321+
agent {
322+
enabled = true
323+
}
316324
}
317325
}
318326
----
319327

320328
[source,kotlin,subs="verbatim,attributes", role="multi-language-sample"]
321329
----
322330
graalvmNative {
323-
agent {
324-
enabled.set(true)
331+
binaries.all {
332+
agent {
333+
enabled.set(true)
334+
}
325335
}
326336
}
327337
----
328338

329-
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.
330340
By default, the agent creates the metadata in the _build/native/agent-output_ directory.
331341

332342
Step 2: **Copy the generated metadata** from the default location, _build/native/agent-output_, to the resources directory, for example, _resources/META-INF/_.
333343
To do that with Gradle, configure and run the `metadataCopy` task.
334344

335345
Add a new task named `metadataCopy` inside the `graalvmNative` block.
336-
Your configuration should look like this:
346+
Your `agent` configuration should look like this:
337347

338348
[source,groovy,subs="verbatim,attributes", role="multi-language-sample"]
339349
----
@@ -361,27 +371,39 @@ agent {
361371

362372
In this block:
363373

364-
- `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.
367377

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**:
369379

370380
[source,bash,subs="verbatim,attributes", role="multi-language-sample"]
371381
----
372382
./gradlew metadataCopy
373383
----
374384

375-
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+
----
376398

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].
378400

379401
Learn more about how to fine-tune the agent further <<gradle-plugin.adoc#native-image-tracing-agent,here>>.
380402

381403
[[use-diagnostics-tools]]
382404
=== Use Diagnostics Tools
383405

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.
385407
For example:
386408

387409
[source,groovy, role="multi-language-sample"]
@@ -407,7 +429,8 @@ graalvmNative {
407429
----
408430

409431
- 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].
411434

412435
[TIP]
413436
====
@@ -423,4 +446,4 @@ All the monitoring and debugging tools https://www.graalvm.org/reference-manual/
423446

424447
=== Learn more
425448

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

Comments
 (0)