Sub topic 8
Building native images with Maven and Gradle
This chapter assumes you can build a Java project with Maven or Gradle, that you have a GraalVM JDK with native-image installed, and that you know what reachability metadata is from the earlier chapters. Running native-image by hand works for a single class. Real projects have dependencies, test suites and a CI pipeline, so you will normally let the build tool drive Native Image.
What the build plugin does
The GraalVM Native Build Tools provide a plugin for each build tool. The Maven plugin has the coordinates org.graalvm.buildtools:native-maven-plugin. The Gradle plugin has the id org.graalvm.buildtools.native. Both do the same jobs:
- They compute the classpath from your project and its dependencies.
- They call
native-imagewith that classpath and your main class. - They pick up metadata from
META-INF/native-image/in your project and in your dependencies. - They can run your JUnit tests as a native executable.
The plugin finds native-image through the GRAALVM_HOME or JAVA_HOME environment variable, so point one of them at a GraalVM JDK.
Maven
The usual approach is a separate native profile, so normal builds stay fast and do not need GraalVM.
<profile>
<id>native</id>
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<version>${native.maven.plugin.version}</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
</profile>Set native.maven.plugin.version in <properties> to a released version of the plugin. Then build with:
mvn -Pnative native:compileThe executable appears in target/. To run the project's tests as a native executable, use mvn -Pnative test. The plugin needs a configured mainClass, either through the maven-jar-plugin manifest or in the plugin's <configuration> block.
Gradle
In Gradle, you apply the plugin and configure the graalvmNative extension.
plugins {
id 'application'
id 'org.graalvm.buildtools.native' version '0.10.2'
}
application {
mainClass = 'com.example.App'
}
graalvmNative {
binaries {
main {
imageName = 'app'
buildArgs.add('--no-fallback')
}
}
}Build with ./gradlew nativeCompile and run with ./gradlew nativeRun. The executable is written to build/native/nativeCompile/. Check the plugin's release page for the current version, because 0.10.2 is only an example.
Passing options to native-image
Both plugins let you pass the same flags you would use on the command line. In Gradle you use buildArgs, as shown above. In Maven you use <buildArgs> inside <configuration>:
<configuration>
<imageName>app</imageName>
<buildArgs>
<buildArg>--no-fallback</buildArg>
<buildArg>-O2</buildArg>
</buildArgs>
</configuration>--no-fallback makes the build fail when the image cannot be built properly. Without it, Native Image may produce a fallback image that needs a JVM, which hides problems.
Metadata in the build
Put hand-written or agent-generated files in src/main/resources/META-INF/native-image/. The build copies them into the JAR, and the plugin finds them. Libraries can ship their own metadata the same way. The plugin can also download a shared metadata repository for popular libraries. In Gradle this is the metadataRepository setting. In Maven it is enabled by default in recent plugin versions.
The tracing agent can be run through the build tool too. In Gradle, ./gradlew -Pagent run runs the application with the agent attached, and ./gradlew metadataCopy --task run --dir src/main/resources/META-INF/native-image copies the result into your sources.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `native-image` not found | `JAVA_HOME` points to a regular JDK | Use a GraalVM JDK or set `GRAALVM_HOME` |
| No main class | `mainClass` not set | Set it in the plugin or manifest |
| Image builds but fails at run time with a missing class | Reflection metadata missing | Add metadata or run the agent |
| Build runs out of memory | Analysis needs a large heap | Pass `-J-Xmx` through `buildArgs` |
Exercise
Why is it a good idea to put the Maven native plugin in a profile, and what command builds the executable?
Show answer
A profile keeps ordinary builds from needing GraalVM and avoids the long native compile on every build. You activate it with mvn -Pnative native:compile.
Exercise
What does --no-fallback change, and why is it useful in a build?
Show answer
It makes the build fail instead of producing a fallback image that still needs a JVM. That way, problems such as missing metadata show up during the build and are not hidden.