Skip to content

Add support for GraalVM native images #522

Open
@jdubois

Description

@jdubois

It would be great to be able to use this library as part of a GraalVM native image build. This would mean less resources needed for end-users, with less cost and less CO2.

It's currently failing because you are using Jackson with Kotlin, and that uses a ton of reflection. It's near impossible to solve it by hand.

I've managed to make it work in one of my (private) projects, using the GraalVM's native image agent. For this to generate the full configuration, it needs to go through all of the execution paths (to find out all those Jackson-configured files, and there are a lot). So instead of doing the GraalVM configuration by hand (again, nearly impossible), it would be all automatic.

The trick is to use it during the test phase, as the test suite should use all of those reflection-heavy files.

Here's how to do it:

  1. Install GraalVM and use it for your build (here's the GitHub action: https://github.com/marketplace/actions/github-action-for-graalvm )
  2. Run the tests for the open-java-core module with Gradle, and add -agentlib:native-image-agent=config-output-dir=src/main/resources/META-INF/native-image to the command line
  3. This will generate the correct configuration metadata for GraalVM, in a file called openai-java-core/src/main/resources/META-INF/native-image/reachability-metadata.json
  4. Next time the application is packaged, this file containing the GraalVM configuration will be shipped with it, and projects will then be able to use it correctly when building the native executable.

If that's fine for you, I can do a PR for this, either:

  • in the .github/workflows/ci.yml action, so you'll have it correct all the time. The main downside, is that it's weird that the CI action generates a file that is needed for the build, that seems counterintuitive.
  • In a specific action, for example .github/workflows/graalvm.yml, that would run once every day. As those files shouldn't move very often, that should be good enough. The main downside would be if a new file is added, and you do a release, all in the same day before that action is triggered.

As an example, here's the reachability-metadata.json file I generated for my project (it's not complete as I don't need everything, but it gives an idea and works 100% for my use case):

reachability-metadata.json

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions