You're on the right track using dependencies, but Maven needs a little more help to reuse the downloaded dependencies from the build job. Here's a breakdown of the problem and how to solve it, along with a complete, improved .gitlab-ci.yml file:
Understanding the Problem
- Maven's Dependency Management: Maven, by default, stores downloaded dependencies in the
.m2/repository directory within the user's home directory. Each job in GitLab CI runs in a separate Docker container. This means the .m2/repository is not persisted between jobs, and the test job has to download everything again.
dependencies Keyword: The dependencies keyword in GitLab CI makes artifacts from a previous job available in the current job. However, simply having the target/ directory available doesn't automatically make Maven aware of the already-downloaded dependencies in the previous job's .m2/repository.
Solution
-
Cache the .m2/repository: Use GitLab's caching feature to persist the .m2/repository between jobs. This is the most effective solution.
-
(Less Ideal, but Sometimes Necessary) Pass the .m2/repository as an Artifact (Avoid if possible): This involves archiving the .m2/repository from the build job and making it available to the test job. This is generally discouraged because:
.m2/repository can be very large.
- Transferring it adds significant overhead to the pipeline.
- Caching is a more efficient solution.
Improved .gitlab-ci.yml (Using Caching - Recommended):
image: maven:3.9.9-eclipse-temurin-21-alpine
stages:
- build
- test
- package
variables:
MAVEN_CLI_OPTS: "--batch-mode --errors --fail-at-end --show-version"
MAVEN_OPTS: "-Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository" # Important for caching
cache:
paths:
- .m2/repository/
build-job:
stage: build
script:
- mvn $MAVEN_CLI_OPTS clean package -DskipTests
artifacts:
paths:
- target/
expire_in: 1 hour
test-job:
stage: test
dependencies:
- build-job
script:
- mvn $MAVEN_CLI_OPTS test
package-job:
stage: package
dependencies:
- build-job
script:
- mvn $MAVEN_CLI_OPTS package
artifacts:
paths:
- target/*.jar
expire_in: 1 hour
Explanation of Changes (Caching Approach):
-
cache: Section:
paths: - .m2/repository/: This tells GitLab to cache the .m2/repository directory. GitLab will automatically try to restore this cache at the beginning of each job and save it again at the end of the job. Critically, the cache key is implicitly based on the image definition (e.g., maven:3.9.9-eclipse-temurin-21-alpine), so if you change the image, the cache will be invalidated, which is usually what you want.
-
MAVEN_OPTS Variable:
-Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository: This crucial line tells Maven to use a .m2/repository within your project directory instead of the default location in the user's home directory. This is essential for caching to work correctly in GitLab. Without this, Maven would still try to use /root/.m2/repository, which is not cached.
-
MAVEN_CLI_OPTS Variable:
--batch-mode --errors --fail-at-end --show-version: These are useful Maven command-line options:
--batch-mode: Runs Maven in non-interactive (batch) mode.
--errors: Shows errors.
--fail-at-end: Fails the build at the end if there are errors.
--show-version: Prints the Maven version.
How the Caching Approach Works:
-
Build Job:
- The build job starts, and GitLab tries to restore the
.m2/repository cache. If this is the first time the pipeline has run (or the cache has expired or been invalidated), the cache will be empty.
- Maven downloads dependencies as needed and stores them in
$CI_PROJECT_DIR/.m2/repository.
- After the build job completes, GitLab saves the contents of
$CI_PROJECT_DIR/.m2/repository as the cache.
- The
target/ directory is archived as an artifact.
-
Test Job:
- The test job starts, and GitLab restores the
.m2/repository cache from the previous build job (if available).
- The
target/ directory artifact is downloaded from the build job (due to the dependencies keyword).
- Because the
.m2/repository is cached and Maven is configured to use it, Maven doesn't need to download dependencies again. It can use the already-downloaded dependencies in $CI_PROJECT_DIR/.m2/repository.
mvn test is executed.
-
Package Job:
- The package job starts, and GitLab restores the
.m2/repository cache from the previous build job (if available).
- The
target/ directory artifact is downloaded from the build job (due to the dependencies keyword).
- Because the
.m2/repository is cached and Maven is configured to use it, Maven doesn't need to download dependencies again. It can use the already-downloaded dependencies in $CI_PROJECT_DIR/.m2/repository.
mvn package is executed.
- The
target/*.jar are archived as artifacts.
Important Considerations
-
Cache Invalidation: If your pom.xml changes (e.g., you add or update a dependency), the cache will still be used. This could cause problems if the cache contains outdated dependencies. Consider ways to invalidate the cache when your dependencies change. One approach is to include the pom.xml's hash in the cache key (advanced):
cache:
key:
files:
- pom.xml
paths:
- .m2/repository/
This makes the cache specific to the contents of pom.xml, so any change to the pom.xml will invalidate the cache. This is more reliable but might lead to more frequent dependency downloads.
-
Maven Wrapper: Consider using the Maven Wrapper (mvnw). This ensures that everyone uses the same Maven version, regardless of what's installed on their local machine or the CI environment. It also improves reproducibility.
Example of passing the .m2 as an artifact, not recommended
image: maven:3.9.9-eclipse-temurin-21-alpine
stages:
- build
- test
build-job:
stage: build
script:
- mvn clean package -DskipTests
artifacts:
paths:
- target/
- .m2/repository/ # Include the .m2 repository in the artifacts
expire_in: 1 hour
test-job:
stage: test
script:
- cp -r .m2/repository/* /root/.m2/repository/ # Copy the .m2 repository to the default location
- mvn test
dependencies:
- build-job
Why the Artifact Approach is Less Desirable
- Size: The
.m2/repository can be very large, especially in projects with many dependencies. Transferring it as an artifact will significantly increase pipeline execution time.
- Overhead: Even if the artifact transfer is relatively fast, it still adds overhead compared to caching.
- Configuration: You need to copy the
.m2/repository to the correct location (/root/.m2/repository/ in this case) within the test job.
In summary, use the caching approach (the first example) to reuse downloaded dependencies between GitLab CI jobs. It's the most efficient and recommended solution.