# NOT BUILT OR RUN while writing the article: the sandbox has no Docker daemon. The commands inside it are the ones
# scripts/train.sh and scripts/mismatch.sh ran (extract, AOTCacheOutput, AOTCache); the layer arithmetic is in
# output/05-layers.txt. Treat the file as a starting point and measure it in your own registry.
#
# Build:  mvn -DskipTests package && docker build -f docker/Dockerfile -t aot-cache .

FROM eclipse-temurin:25-jdk AS extract
WORKDIR /build
COPY target/app.jar app.jar
RUN java -Djarmode=tools -jar app.jar extract --layers --destination extracted

# The final image is also where the cache is trained: the cache records the exact JVM build and the exact
# jar files (size and modification time), so it must be created by the JVM that will use it, after the last COPY.
FROM eclipse-temurin:25-jre
WORKDIR /application
COPY --from=extract /build/extracted/dependencies/ ./
COPY --from=extract /build/extracted/spring-boot-loader/ ./
COPY --from=extract /build/extracted/snapshot-dependencies/ ./
COPY --from=extract /build/extracted/application/ ./
RUN java -XX:AOTCacheOutput=app.aot -Dspring.context.exit=onRefresh -jar app.jar
ENTRYPOINT ["java", "-XX:AOTCache=app.aot", "-jar", "app.jar"]
