Skip to content

GraalVM support for native images #103

Open
@klopfdreh

Description

@klopfdreh

Description

Because we want to use jgit in GraalVM native images it would be great if this could be achieved through this ticket.

Motivation

jgit in GraalVM native images.

Alternatives considered

N/A

Additional context

N/A

Spring Boot Workaround

@Configuration
@ImportRuntimeHints(GitHubRuntimeHints.class)
@Slf4j
public class GitHubRuntimeHints implements RuntimeHintsRegistrar {

    @Override
    public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
        Arrays.stream(System.getProperty("java.class.path").split(File.pathSeparator)).forEach(classpathEntry -> {
            // If the classpathEntry is no jar skip it
            if (!classpathEntry.endsWith(".jar")) {
                return;
            }

            try (JarInputStream is = new JarInputStream(Files.newInputStream(Path.of(classpathEntry)))) {
                JarEntry entry = is.getNextJarEntry();
                while (entry != null) {
                    String entryName = entry.getName();
                    if (entryName.endsWith(".class") && entryName.startsWith("org/eclipse/jgit") && !entryName.contains("package-info")) {
                        String githubApiClassName = entryName.replace("/", ".");
                        String githubApiClassNameWithoutClass = githubApiClassName.substring(0, githubApiClassName.length() - 6);
                        log.info("Registered class {} for reflections and serialization.", githubApiClassNameWithoutClass);
                        hints.reflection().registerType(TypeReference.of(githubApiClassNameWithoutClass), MemberCategory.values());
                        hints.serialization().registerType(TypeReference.of(githubApiClassNameWithoutClass));
                    }
                    entry = is.getNextJarEntry();
                }
            } catch (IOException e) {
                log.warn("Error while reading jars", e);
            }
        });

        hints.reflection()
            .registerType(TypeReference.of(IOException.class),
                hint -> hint.withMembers(MemberCategory.values())
            );

        hints.resources()
            .registerPattern("application.yml");
    }
}

Before the native-image command is used you have to set the environment varialbes

# Git configuration home folder
ENV XDG_CONFIG_HOME=/writablePath/
# Don't read git system configuration
ENV GIT_CONFIG_NOSYSTEM=true

Important: this is not a good solution as it enables all jgit classes for reflections and serialization.

An example of a native build can be found here: https://github.com/klopfdreh/native-cloud-config-test/blob/main/client/Dockerfile (Note: This does not use builder-image with multi stage and is only an example)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions