# syntax=docker/dockerfile:1
FROM maven:3.9.14-sapmachine-25 AS builder
WORKDIR /build
# Initialize a git repository in the build context to satisfy git-build-hook-maven-plugin
RUN apt-get update -qq && apt-get install -y -qq git
# Copy pom and sources first to leverage Docker cache
COPY pom.xml ./
COPY src ./src
# Create a local git repository and commit the current state to satisfy plugins expecting a git repo
RUN git config --global user.email "build@local" && git config --global user.name "BuildAgent" && git init && git add . && git commit -m "Initial commit for docker build"
# Build the project (wrapper not required since we rely on Maven in this image)
RUN mvn -B -DskipTests package
FROM eclipse-temurin:11-jre
WORKDIR /app
# Create a non-root user to run the application
RUN groupadd -r app && useradd -r -g app app
# Copy the built artifacts from the builder stage
COPY --from=builder /build/target /app/target
# Ensure correct ownership for the runtime user
RUN chown -R app:app /app
USER app
# Startup script to run the built jar
RUN mkdir -p /app && cat > /app/start.sh <<'EOS'
#!/bin/sh
set -e
jar=$(ls -1 /app/target/*.jar 2>/dev/null | head -n1)
if [ -z "$jar" ]; then
echo "No jar found in /app/target"
exit 1
fi
exec java -jar "$jar" "$@"
EOS
RUN chmod +x /app/start.sh
ENTRYPOINT ["/app/start.sh"]