diff --git a/Dockerfile b/Dockerfile index f45ce0f..b7601a7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,12 +9,16 @@ COPY gradlew . COPY gradle gradle COPY build.gradle . COPY settings.gradle . + +# Gradle and every dependency are downloaded in their own layer, before the source is copied in, so the layer +# is rebuilt only when the build files change. Local builds and CI's layer cache (cache-to: type=gha) reuse it, +# so a code change downloads nothing (no Maven Central rate limits). This build stage is not in the final image. +RUN ./gradlew --no-daemon downloadDependencies + COPY src src -# The Gradle home (wrapper distribution + downloaded dependencies) is kept in a BuildKit cache that survives -# between builds, so only the first build downloads everything. It is not part of the image. -RUN --mount=type=cache,target=/root/.gradle \ - ./gradlew --no-daemon clean build -x test +# --offline: everything needed was downloaded above +RUN ./gradlew --no-daemon --offline clean build -x test # Stage 2: Runtime with Java 21 JRE FROM eclipse-temurin:21-jre-alpine diff --git a/README.md b/README.md index ec52172..5c894a7 100644 --- a/README.md +++ b/README.md @@ -379,7 +379,7 @@ java -jar build/libs/mailengine-0.0.1-SNAPSHOT.jar ### Docker -The `Dockerfile` builds the app with JDK 21 and runs it on a JRE 21 image as a non-root user. Gradle downloads are kept in a BuildKit cache, so rebuilds are quick. +The `Dockerfile` builds the app with JDK 21 and runs it on a JRE 21 image as a non-root user. Gradle and all dependencies are downloaded in their own layer (`./gradlew downloadDependencies`) before the source is copied in, so after a code change only the compile step runs. CI keeps that layer in its build cache, so it doesn't download from Maven Central on every run. ```bash docker build -t mailengine . diff --git a/build.gradle b/build.gradle index edc77fc..2992312 100644 --- a/build.gradle +++ b/build.gradle @@ -147,4 +147,13 @@ tasks.register('scaleTest', Test) { showStandardStreams = true exceptionFormat = 'full' } -} \ No newline at end of file +} +// Downloads every dependency without compiling anything. The Dockerfile runs it before copying the source, so +// the downloads sit in their own (cacheable) layer. +tasks.register('downloadDependencies') { + description = 'Downloads all dependencies into the Gradle cache.' + def resolvable = configurations.matching { it.canBeResolved } + doLast { + resolvable.each { it.resolve() } + } +}