FROM debian:bookworm AS build

RUN apt-get update && apt-get install -y openjdk-17-jdk-headless

WORKDIR /app

COPY pom.xml .
COPY .mvn/ .mvn/
COPY mvnw .
COPY api/pom.xml api/pom.xml
COPY core/pom.xml core/pom.xml
COPY view/pom.xml view/pom.xml
COPY war/pom.xml war/pom.xml
COPY daisy-integration/pom.xml daisy-integration/pom.xml

# Download dependencies in a separate layer to allow caching for future builds
RUN ./mvnw dependency:go-offline \
    --batch-mode \
    --define includeScope=compile  \
    --activate-profiles docker-dependencies

COPY api/src/ api/src/
COPY core/src/ core/src/
COPY view/src/ view/src/
COPY war/src/ war/src/
COPY daisy-integration/src/ daisy-integration/src/

RUN ./mvnw package \
    --offline \
    --define skipTests \
    --activate-profiles branch,DEV \
    --define skip.npm \
    --define skip.installnodenpm

FROM tomcat:10 AS run

COPY --from=build /app/war/target/*.war /usr/local/tomcat/webapps/ROOT.war

EXPOSE 8080