Skip to content

Commit 07c1ef9

Browse files
committed
docker: bake Maven dependencies into an image layer instead of a cache mount
The dependency-resolution steps wrote the local Maven repository to a BuildKit cache mount (`--mount=type=cache,target=/tmp/m2_repo`). A cache mount is builder-local scratch space that is deliberately excluded from image layers, so it was never captured by layer caching. On a fresh builder the mount was always empty and every build re-downloaded the full dependency tree (~3,600 artifacts, ~7 minutes), while the actual offline compile took only ~24 seconds. Drop the cache mount from the three dependency steps and the offline install so the populated `/tmp/m2_repo` lands in the build stage's image layer. That layer depends only on the pom.xml files (via the poms stage), so it stays cached until a POM changes: source-only changes now reuse the downloaded dependencies instead of fetching them again. Effect on produced images: none. The Maven repository lives only in the intermediate build stage; the runtime stage still copies out just distribution-base and entrypoint.sh, so the final image is byte-for-byte equivalent. The build stage's cached layer grows by the size of the Maven repository, which is the intended trade for skipping the downloads.
1 parent a089b94 commit 07c1ef9

1 file changed

Lines changed: 10 additions & 19 deletions

File tree

Dockerfile

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
# This stage extracts all the pom.xml files.
2-
# It'll get rebuilt with any source change, but that's OK.
3-
# It doesn't matter what image we're using, really, so we may as well use one of the same images as elsewhere.
4-
FROM eclipse-temurin:17-jre AS poms
5-
WORKDIR /usr/src
6-
COPY . .
7-
# Wipe any files not called pom.xml or *.jar
8-
RUN find . -type f -and \! -name pom.xml -and \! -name '*.jar' -delete
9-
# Clear up any (now) empty diretories
10-
RUN find . -type d -empty -delete
11-
121
# Now we build:
132
FROM eclipse-temurin:17 AS build
143
WORKDIR /tmp/
@@ -18,24 +7,26 @@ RUN chmod +x mvnw
187
RUN mkdir -p .mvn
198
COPY .mvn/wrapper .mvn/wrapper
209

21-
# First, copy in just the pom.xml files and fetch the dependencies:
10+
# First, copy in just the pom.xml files and fetch the dependencies into a real image layer. Because this
11+
# layer depends only on the pom.xml files, it stays stable until a POM changes, so dependency downloads
12+
# are reused whenever the source changes but the dependencies don't.
2213
COPY --from=poms /usr/src/ .
2314
# I don't know why we need all three either.
24-
RUN --mount=type=cache,target=/tmp/m2_repo,id=openfire_build ./mvnw -e -B dependency:resolve-plugins -Dmaven.test.skip -Dmaven.repo.local=/tmp/m2_repo
25-
RUN --mount=type=cache,target=/tmp/m2_repo,id=openfire_build ./mvnw -e -B de.qaware.maven:go-offline-maven-plugin:resolve-dependencies -Dmaven.repo.local=/tmp/m2_repo
15+
RUN ./mvnw -e -B dependency:resolve-plugins -Dmaven.test.skip -Dmaven.repo.local=/tmp/m2_repo
16+
RUN ./mvnw -e -B de.qaware.maven:go-offline-maven-plugin:resolve-dependencies -Dmaven.repo.local=/tmp/m2_repo
2617
# The go-offline plugin and dependency:resolve-plugins do not reliably resolve BOM POMs referenced
2718
# via <scope>import</scope> in <dependencyManagement>. Fetch them explicitly so they are present
28-
# in the cache before the offline build runs. Add any newly introduced BOMs here in the same way.
29-
RUN --mount=type=cache,target=/tmp/m2_repo,id=openfire_build \
30-
./mvnw -e -B dependency:get -DgroupId=org.codehaus.plexus -DartifactId=plexus-utils -Dpackaging=jar -Dversion=1.1 -Dmaven.repo.local=/tmp/m2_repo && \
19+
# in the repository before the offline build runs. Add any newly introduced BOMs here in the same way.
20+
RUN ./mvnw -e -B dependency:get -DgroupId=org.codehaus.plexus -DartifactId=plexus-utils -Dpackaging=jar -Dversion=1.1 -Dmaven.repo.local=/tmp/m2_repo && \
3121
./mvnw -e -B dependency:get -DgroupId=org.junit -DartifactId=junit-bom -Dpackaging=pom -Dversion=5.13.4 -Dmaven.repo.local=/tmp/m2_repo && \
3222
./mvnw -e -B dependency:get -DgroupId=org.mockito -DartifactId=mockito-bom -Dpackaging=pom -Dversion=5.22.0 -Dmaven.repo.local=/tmp/m2_repo
3323

34-
# Above here is only affected by the pom.xml files, so the cache is stable.
24+
# Above here is only affected by the pom.xml files, so the layer is stable.
3525

3626
# Now, copy in all the source, and actually build it, skipping the tests.
27+
# The offline build reads the /tmp/m2_repo that the layers above populated.
3728
COPY . .
38-
RUN --mount=type=cache,target=/tmp/m2_repo,id=openfire_build ./mvnw -o -e -B install -Dmaven.test.skip -Dmaven.repo.local=/tmp/m2_repo
29+
RUN ./mvnw -o -e -B install -Dmaven.test.skip -Dmaven.repo.local=/tmp/m2_repo
3930
# In case of Windows, break glass.
4031
RUN sed -i 's/\r//g' /usr/src/distribution/target/distribution-base/bin/openfire.sh
4132

0 commit comments

Comments
 (0)