Having a simple Spring Boot application (generated on start.spring.io) with DevTools added as a dependency, if you try to build the native image:
./gradlew nativeCompile
You get an executable that if you try to run fails with:
./build/native/nativeCompile/demo
Exception in thread "main" java.lang.IllegalArgumentException: Unable to instantiate factory class [org.springframework.boot.devtools.restart.RestartScopeInitializer] for factory type [org.springframework.context.ApplicationContextInitializer]
at org.springframework.core.io.support.SpringFactoriesLoader$FailureHandler.lambda$throwing$0(SpringFactoriesLoader.java:650)
at org.springframework.core.io.support.SpringFactoriesLoader$FailureHandler.lambda$handleMessage$3(SpringFactoriesLoader.java:674)
at org.springframework.core.io.support.SpringFactoriesLoader.instantiateFactory(SpringFactoriesLoader.java:231)
at org.springframework.core.io.support.SpringFactoriesLoader.load(SpringFactoriesLoader.java:206)
at org.springframework.core.io.support.SpringFactoriesLoader.load(SpringFactoriesLoader.java:160)
at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:459)
at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:455)
at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:273)
at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:252)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1302)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1291)
at com.example.demo.DemoApplicationKt.main(DemoApplication.kt:13)
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.devtools.restart.RestartScopeInitializer
at java.lang.Class.forName(DynamicHub.java:1136)
at org.springframework.util.ClassUtils.forName(ClassUtils.java:283)
at org.springframework.core.io.support.SpringFactoriesLoader.instantiateFactory(SpringFactoriesLoader.java:224)
... 9 more
There are some issues on GH regarding this, but none of them is addressing the exact issue, any fix or workaround. Does anyone know how to exclude DevTools during nativeCompile
?
Update: It seems to be a bug with Spring Boot and they're trying to solve it. (Will update it when the issue is resolved.)