# syntax=docker/dockerfile:1

# ---- Build stage: compile & package the Spring Boot fat jar ----
FROM maven:3.9-eclipse-temurin-17 AS build
WORKDIR /build

# Cache dependencies: copy only the POM first, then resolve.
# This layer is reused as long as pom.xml doesn't change.
COPY pom.xml .
RUN mvn -B -q dependency:go-offline

# Now copy sources and build (tests need a live DB, so skip them here).
COPY src ./src
RUN mvn -B -q clean package -DskipTests

# ---- Runtime stage: small JRE-only image ----
FROM eclipse-temurin:17-jre-jammy AS runtime
WORKDIR /app

# Run as an unprivileged user.
RUN groupadd --system spring && useradd --system --gid spring spring

# Copy the built jar (there is exactly one *.jar; *.jar.original is excluded by the glob).
COPY --from=build --chown=spring:spring /build/target/*.jar /app/app.jar

USER spring:spring

# Matches server.port in application.properties.
EXPOSE 9192

# Container-aware heap sizing; extra flags can be added via JAVA_TOOL_OPTIONS.
ENTRYPOINT ["java", "-XX:MaxRAMPercentage=75.0", "-jar", "/app/app.jar"]
