I have this small springboot app (a POC), with this pom.xml
(partial):
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
and only this single class:
@SpringBootApplication
public class SpringbootHelloworldJavaApplication {
public static void main(String[] args) throws Exception {
if (System.getenv("REQUIRED_ENV") == null) {
throw new Exception("Missing required envs");
}
SpringApplication.run(SpringbootHelloworldJavaApplication.class, args);
}
}
When i try to do a native build with mvn -DskipTests -Pnative package
, the process fails because of the Exception thrown by my own code.
[INFO] Scanning for projects...
[INFO]
[INFO] --------------< foo.bar:springboot-helloworld-java >--------------
[INFO] Building springboot-helloworld-java 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.3.1:resources (default-resources) @ springboot-helloworld-java ---
[INFO] Copying 1 resource from src/main/resources to target/classes
[INFO] Copying 0 resource from src/main/resources to target/classes
[INFO]
[INFO] --- maven-compiler-plugin:3.11.0:compile (default-compile) @ springboot-helloworld-java ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:3.3.1:testResources (default-testResources) @ springboot-helloworld-java ---
[INFO] skip non existing resourceDirectory /home/andrea/dev/springboot-helloworld-java/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.11.0:testCompile (default-testCompile) @ springboot-helloworld-java ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:3.1.2:test (default-test) @ springboot-helloworld-java ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- spring-boot-maven-plugin:3.2.5:process-aot (process-aot) @ springboot-helloworld-java ---
Exception in thread "main" java.lang.reflect.UndeclaredThrowableException
at org.springframework.util.ReflectionUtils.rethrowRuntimeException(ReflectionUtils.java:147)
at org.springframework.util.ReflectionUtils.handleInvocationTargetException(ReflectionUtils.java:126)
at org.springframework.util.ReflectionUtils.handleReflectionException(ReflectionUtils.java:110)
at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:284)
at org.springframework.boot.SpringApplicationAotProcessor.lambda$prepareApplicationContext$0(SpringApplicationAotProcessor.java:62)
at org.springframework.util.function.ThrowingSupplier.get(ThrowingSupplier.java:58)
at org.springframework.util.function.ThrowingSupplier.get(ThrowingSupplier.java:46)
at org.springframework.boot.SpringApplication.withHook(SpringApplication.java:1454)
at org.springframework.boot.SpringApplicationAotProcessor$AotProcessorHook.run(SpringApplicationAotProcessor.java:109)
at org.springframework.boot.SpringApplicationAotProcessor.prepareApplicationContext(SpringApplicationAotProcessor.java:60)
at org.springframework.context.aot.ContextAotProcessor.doProcess(ContextAotProcessor.java:83)
at org.springframework.context.aot.ContextAotProcessor.doProcess(ContextAotProcessor.java:49)
at org.springframework.context.aot.AbstractAotProcessor.process(AbstractAotProcessor.java:82)
at org.springframework.boot.SpringApplicationAotProcessor.main(SpringApplicationAotProcessor.java:80)
Caused by: java.lang.Exception: Missing required envs
at foo.bar.springboothelloworldjava.SpringbootHelloworldJavaApplication.main(SpringbootHelloworldJavaApplication.java:11)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:281)
... 10 more
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.735 s
[INFO] Finished at: 2024-05-01T15:54:47+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:3.2.5:process-aot (process-aot) on project springboot-helloworld-java: Process terminated with exit code: 1 -> [Help 1]
How the aot process is behaving here?
And how to avoid the error, given the fact that envs-variables are per-environment and are not known at build-time?