Skip to content

Commit

Permalink
Final snippet adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
dnestoro committed Dec 26, 2024
1 parent 450b9b1 commit f393f7a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
21 changes: 11 additions & 10 deletions docs/src/docs/asciidoc/end-to-end-gradle-guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ graalvmNative {
binaries.main {
// options to configure the main binary
imageName = 'application'
mainClass = 'org.test.Main'
mainClass = 'org.example.Main'
buildArgs.add('-O3') // enables additional compiler optimizations
}
binaries.test {
Expand All @@ -120,11 +120,10 @@ graalvmNative {
// common options
verbose.set(true)
}
binaries.main {
// options to configure the main binary
imageName.set('application')
mainClass.set('org.test.Main')
mainClass.set('org.example.Main')
buildArgs.add('-O3') // enables additional compiler optimizations
}
Expand Down Expand Up @@ -299,7 +298,7 @@ To enable the agent via the command line, pass the `-Pagent` option when running

[source,bash, role="multi-language-sample"]
----
./gradlew -Pagent nativeRun
./gradlew -Pagent run
----

[TIP]
Expand Down Expand Up @@ -329,10 +328,12 @@ graalvmNative {
}
----

From that point on, commands you execute will run with the agent attached.
From that point on, commands like `run` or `test` will be executed with the agent attached.
By default, the agent creates the metadata in the _build/native/agent-output_ directory.

Step 2: **Copy the generated metadata** from the default location, _build/native/agent-output_, to the resources directory, for example, _resources/META-INF/_.
Step 2: **Copy the generated metadata** from the default location, _build/native/agent-output_, to the resources directory, for example, _resources/META-INF/native-image_.
Native Image automatically takes metadata from that location.

To do that with Gradle, configure and run the `metadataCopy` task.

Add a new task named `metadataCopy` inside the `graalvmNative` block.
Expand All @@ -343,8 +344,8 @@ Your `agent` configuration should look like this:
agent {
enabled = true
metadataCopy {
inputTaskNames.add("test")
outputDirectories.add("src/test/resources/META-INF/native-image/org.example")
inputTaskNames.add("run")
outputDirectories.add("src/main/resources/META-INF/native-image/org.example")
mergeWithExisting = true
}
}
Expand All @@ -355,8 +356,8 @@ agent {
agent {
enabled.set(true)
metadataCopy {
inputTaskNames.add("test")
outputDirectories.add("resources/META-INF/native-image/org.example")
inputTaskNames.add("run")
outputDirectories.add("src/main/resources/META-INF/native-image/org.example")
mergeWithExisting.set(true)
}
}
Expand Down
44 changes: 35 additions & 9 deletions docs/src/docs/asciidoc/end-to-end-maven-guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ For convenience, you can create a Maven profile and add the plugin into it:
</profiles>
----

Replace `{maven-plugin-version}` with the latest released version.
Replace `maven-plugin-version` with the latest released version.
All plugin versions are listed https://github.com/graalvm/native-build-tools/releases[here].

The `<mainClass>` tag provides the path to the application main class (the main entry point).
Expand Down Expand Up @@ -182,6 +182,17 @@ The tests are compiled ahead of time and executed as native code.
</dependency>
----

- Add surefire plugin into plugins section of your profile:

[source,xml, role="multi-language-sample"]
----
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
</plugin>
----

- Run the tests:

[source,bash, role="multi-language-sample"]
Expand Down Expand Up @@ -344,14 +355,21 @@ In such cases, additional metadata is required.
The easiest way to collect the missing metadata is by using the https://www.graalvm.org/reference-manual/native-image/metadata/AutomaticMetadataCollection/[Tracing Agent].
This agent tracks all usages of dynamic features during application execution on the JVM and generates the necessary configuration.

[NOTE]
====
In this guide we will show you how to generate metadata from your tests.
Generating metadata from your main application requires <<maven-plugin.adoc#agent-support-running-application,more configuring>>.
Other than that, the guide remains the same, except that you use the `package` phase instead of the `test` phase.
====

The agent is disabled by default.
You can enable it on the command line or in _pom.xml_.

To enable the agent via the command line, pass the `-Dagent=true` option when running Maven:

[source,bash, role="multi-language-sample"]
----
mvn -Pnative -Dagent=true package
mvn -Pnative -Dagent=true test
----

[TIP]
Expand All @@ -366,7 +384,6 @@ Step 1: **Enable the agent** by setting `<agent>` to `true` in the `native` prof
[source,xml, role="multi-language-sample"]
----
<configuration>
<mainClass>org.example.Main</mainClass>
<agent>
<enabled>true</enabled>
</agent>
Expand All @@ -375,7 +392,9 @@ Step 1: **Enable the agent** by setting `<agent>` to `true` in the `native` prof
From that point on, commands you execute will run with the agent attached.
By default, the agent creates the metadata in the _target/native/agent-output_ directory.

Step 2: **Copy the generated metadata** from the default location, _target/native/agent-output/_, to the resources directory, for example, _resources/META-INF/_.
Step 2: **Copy the generated metadata** from the default location, _target/native/agent-output/_, to the resources directory, for example, _resources/META-INF/native-image_.
Native Image automatically takes metadata from that location.

To do that with Maven, configure and run the `metadataCopy` task.

Add a new task named `metadataCopy` inside the `agent` block that you added in step 1.
Expand All @@ -384,13 +403,12 @@ Your `agent` configuration should look like this:
[source,xml, role="multi-language-sample"]
----
<agent>
<enabled>true</enabled>
<metadataCopy>
<disabledStages>
<stage>main</stage>
</disabledStages>
<merge>true</merge>
<outputDirectory>resources/META-INF/</outputDirectory>
<outputDirectory>src/test/resources/META-INF/native-image</outputDirectory>
</metadataCopy>
</agent>
----
Expand All @@ -405,14 +423,15 @@ Step 3: Now that the `metadataCopy` task is configured, **run the agent to colle

[source,bash,subs="verbatim,attributes", role="multi-language-sample"]
----
mvn -Pnative test native:metadata-copy
mvn -Pnative -Dagent=true test native:metadata-copy
----

Step 4: Finally, **build the native image with the metadata** and run:
Step 4: Finally, you can proceed without the agent and **build the native image with the metadata from `META-INF` directory**.
From that point on, you can run your tests with:

[source,bash, role="multi-language-sample"]
----
mvn -Pnative package
mvn -Pnative test
----

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].
Expand Down Expand Up @@ -453,6 +472,13 @@ When running on GraalVM for JDK 21, pass the `-H:+BuildReport` option instead to

All the monitoring and debugging tools https://www.graalvm.org/reference-manual/native-image/debugging-and-diagnostics/[listed on the website], can be enabled in the plugin configuration using `<buildArgs>`.

You will see the output of these tools among generated artifacts when you run:

[source,bash, role="multi-language-sample"]
----
mvn -Pnative -DskipNativeTests package
----

=== Learn more

To continue learning, refer to the <<changelog.adoc#,extensive reference documentation for the GraalVM Native Image Maven plugin>>.

0 comments on commit f393f7a

Please sign in to comment.