# -------- Stage 1: Build the jar using Gradle --------
FROM gradle:9.2-jdk21 AS build
WORKDIR /app

# Copy only build scripts first
COPY settings.gradle.kts build.gradle.kts ./
COPY gradle.properties* ./
COPY gradle ./gradle

# Warm dependency cache
RUN gradle build -x test --no-daemon || true

# Copy source last
COPY src ./src

# Build the jar
RUN gradle bootJar --no-daemon

# -------- Runtime --------
FROM eclipse-temurin:21-jdk-alpine

EXPOSE 8080
WORKDIR /app

COPY --from=build /app/build/libs/*.jar app.jar

ENTRYPOINT ["java", "-jar", "app.jar"]