Maven in CI and in a Dockerfile

A Maven build on a server differs from one on a laptop in two places: it starts with an empty local repository, and it should end with an artifact and no source code. Both are written down in files: a CI workflow that runs the build, and a Dockerfile that builds an image from it. Neither can run in this container, which has no network and no Docker, so this article shows the two files as files and proves the limit with one real command.

The Dockerfile

Two stages: the first one has Maven and the JDK and builds the jar, the second one has only the Java runtime and the jar. The image that ships is the second, so it carries neither Maven nor the local repository nor the sources.

Dockerfile

FROM maven:3.9-eclipse-temurin-21 AS build WORKDIR /build COPY pom.xml . RUN mvn -B -q dependency:go-offline COPY src ./src RUN mvn -B -q package -DskipTests FROM eclipse-temurin:21-jre WORKDIR /app COPY --from=build /build/target/app-1.0.0.jar ./app.jar ENTRYPOINT ["java", "-jar", "/app.jar"]

Four lines carry the Maven knowledge. COPY pom.xml . before COPY src is the cache trick: the layer that downloads the dependencies depends only on the pom, so a change in a source file does not download anything again. dependency:go-offline is the goal that downloads everything the build will need, in that layer. mvn -B -q package -DskipTests builds the jar and keeps the log quiet, which a Docker build needs because it captures it.

Two things a Dockerfile should not do: run mvn install, because the local repository of the build stage is thrown away with the stage, and copy the whole project before the first build, because that makes every layer depend on every file. The .dockerignore next to it usually excludes target/ and .git/ for the same reason.

The workflow

A GitHub Actions job of the shape a Java project uses. The cache is the part that matters: without it every run downloads the whole local repository, which for a project of this size is a minute and for a real one is several.

.github/workflows/build.yml

name: build on: push: branches: [main] pull_request: jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-java@v4 with: distribution: temurin java-version: "21" cache: maven - run: mvn -B verify - uses: actions/upload-artifact@v4 if: always() with: name: surefire-reports path: target/surefire-reports/

mvn -B verify is the phase a CI job runs, for the reason the test article gives: it has run the tests, built the jar and run any integration check a pom binds. The upload step runs if: always() so that the reports of a red build are there to read, and the path is the folder the surefire article lists.

A runner starts with an empty local repository. The first build of a project downloads every plugin and every dependency, which is what makes the cache worth configuring and what makes a build fail on a day the repository is down. This image is the other extreme: a full repository and no network, which is why every page of this course runs here.

What this image cannot show

Neither file can be exercised in the container the course was written in: there is no Docker client, and a docker build needs a daemon and a network for the two base images. The proof is one command:

docker --version 2>&1; echo "status: $?"

bash: line 1: docker: command not found
status: 127

status: 127 is the shell’s status for "command not found", and the line above it is the message bash prints; another shell would word its own. The status is what a script tests, so a Dockerfile and a workflow never see this line: they start on a machine that has the tool. The two commands the Dockerfile runs are shown as not run, with their real arguments:

docker build --tag app:1.0.0 .

not run in this environment

docker run --rm app:1.0.0

not run in this environment

The Docker section has the pages about the images themselves, and the Jenkins page the one about a build server that runs the same commands.

Where to go next

Maven in IntelliJ IDEA comes back to a desktop: the error this folder was born with, and the settings that fix it.

Article author: Arthur Isaev