# Multi-stage Dockerfile to build and package Apache Maven from source and provide a runnable distribution
# Builder stage: use a Maven image to ensure Maven >= 3.9 and JDK 17
FROM maven:3.9-eclipse-temurin-17 AS builder
ENV DEBIAN_FRONTEND=noninteractive
# Copy the repository and build
WORKDIR /src
COPY . /src
# Build the Apache Maven distribution from source (skip tests for faster iteration)
# Skip RAT license checks to allow building from source in this container
RUN mvn -e -B -V -Drat.skip=true package -DskipTests
# Collect produced artifacts for the runtime image
RUN mkdir -p /artifacts
RUN bash -lc 'shopt -s nullglob; cp apache-maven/target/apache-maven-*.tar.gz apache-maven/target/apache-maven-*.zip /artifacts/ 2>/dev/null || true'
# Runtime stage: provide a minimal image with a Java runtime and the distribution
FROM ubuntu:jammy-20260217 AS runtime
ENV DEBIAN_FRONTEND=noninteractive
# Install runtime dependencies (Java runtime and other utilities)
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
ca-certificates \
curl \
unzip \
zip \
tar \
git \
openjdk-17-jre \
&& rm -rf /var/lib/apt/lists/*
# Prepare Maven installation directory
RUN mkdir -p /opt/maven
# Copy artifacts from builder stage and extract
COPY --from=builder /artifacts/apache-maven-*.tar.gz /artifacts/apache-maven-*.zip /opt/maven/
# Extract tarball (preferred) and zip (as fallback)
RUN set -eux; \
if ls /opt/maven/apache-maven-*.tar.gz 1> /dev/null 2>&1; then \
tar -xzf /opt/maven/apache-maven-*.tar.gz -C /opt/maven --strip-components 1; \
fi
RUN set -eux; \
if ls /opt/maven/apache-maven-*.zip 1> /dev/null 2>&1; then \
unzip -o /opt/maven/apache-maven-*.zip -d /opt/maven; \
if compgen -G "/opt/maven/apache-maven-*/bin/mvn" > /dev/null; then \
mkdir -p /opt/maven/bin; \
for f in /opt/maven/apache-maven-*/bin/*; do ln -sf "$f" "/opt/maven/bin/$(basename "$f")"; done; \
fi; \
fi
# Ensure PATH includes the Maven bin directory
ENV PATH="/opt/maven/bin:${PATH}"
# Expose a runnable default: run mvn --version
ENTRYPOINT ["mvn"]
CMD ["-version"]