micronaut-core icon indicating copy to clipboard operation
micronaut-core copied to clipboard

Unable to compile simple Hello World app to native image

Open Paullo612 opened this issue 2 years ago • 3 comments

Expected Behavior

Should compile fine. Compiles fine with 3.4.x branch.

Actual Behaviour

Simple Hello World app fails to compile to native image if I add micronaut-inject Maven dependency:

Error: Classes that should be initialized at run time got initialized during image building:
 io.micronaut.core.io.service.SoftServiceLoader was unintentionally initialized at build time. To see why io.micronaut.core.io.service.SoftServiceLoader got initialized use --trace-class-initialization=io.micronaut.core.io.service.SoftServiceLoader
io.micronaut.core.optim.StaticOptimizations was unintentionally initialized at build time. To see why io.micronaut.core.optim.StaticOptimizations got initialized use --trace-class-initialization=io.micronaut.core.optim.StaticOptimizations

However, adding micronaut-http-netty dependency solves compilation problems. But I do not need HTTP server in my app at all.

Steps To Reproduce

Try to compile example app to native image: ./mvnw clean package -Dpackaging=native-image

Environment Information

  • Operation System: Ubuntu 20.04
  • JDK Version: GraalVM CE 22.1.0 (build 17.0.3+7-jvmci-22.1-b06)

Example Application

https://github.com/Paullo612/micronaut-native-image-test

Version

3.5.3

Paullo612 avatar Jul 13 '22 11:07 Paullo612

Thanks for workaround for Gradle, but what about Maven? Tried

<project>
    ...
    <dependencies>
        ...
        <dependency>
            <groupId>io.micronaut</groupId>
            <artifactId>micronaut-http-server</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>
<project>
    ...
    <build>
        <plugins>
            ...
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <dependencies>
                    <dependency>
                        <groupId>io.micronaut</groupId>
                        <artifactId>micronaut-http-server</artifactId>
                        <version>${micronaut.core.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>
<project>
    ...
    <build>
        <plugins>
            ...
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <annotationProcessorPaths combine.children="append">
                        <annotationProcessorPath>
                            <groupId>io.micronaut</groupId>
                            <artifactId>micronaut-http-server</artifactId>
                            <version>${micronaut.core.version}</version>
                        </annotationProcessorPath>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

And still no luck :(

Paullo612 avatar Aug 17 '22 09:08 Paullo612

no i'm an idiot and commented on the wrong issue. sorry.

yawkat avatar Aug 17 '22 09:08 yawkat

ok ive looked at this, it's caused by a missing --initialize-at-build-time=io.micronaut. this is configured by micronaut-runtime, so when that dependency is present, it works fine.

I'm confused by why this is only a problem in 3.5.0+, though. I've bisected core and the regression appears with #7195. This makes some sense since that PR touched service initialization, but I don't really see an obvious issue in that PR relating to native image config.

My proposed fix would be to simply move the --initialize-at-build-time=io.micronaut setting from micronaut-runtime to micronaut-core. What do you think @graemerocher ?

yawkat avatar Sep 06 '22 09:09 yawkat