Need help with maven dependency cache
Asked by bayarkhuu:

Im trying to cache my maven dependencies.

after step one succeeded (log):  
Cache "maven: /root/.m2/repository": Skipping upload for empty cache

My configs:

```

#Dockerfile
# Stage 1: Build Stage
FROM maven:3.8.2-openjdk-17-slim AS build
WORKDIR /app

# Copy the pom.xml file
COPY pom.xml .

# Create the .m2 directory and copy settings.xml
RUN mkdir -p /root/.m2
COPY settings.xml /root/.m2/

# Download dependencies
RUN mvn dependency:go-offline

# Copy the rest of the application
COPY . .

# Build the application
RUN mvn clean install -DskipTests

# Stage 2: Run Stage
FROM amazoncorretto:17.0.9-alpine3.18
WORKDIR /app

# Copy only the necessary files from the build stage
COPY --from=build /app/target/admin-api.jar admin-api.jar

ENTRYPOINT ["java", "-jar", "admin-api.jar"]
```

```
# bitbucket-pipelines.yml
image: alpine:latest

definitions:
  caches:
    maven: /root/.m2/repository
  services:
    docker:
      image: docker:dind
      memory: 3072

pipelines:
  custom:
    office-server:
      - step:
          fail-fast: true
          user: root
          runs-on:
            - self.hosted
            - linux
            - office
          name: Docker build & Push
          services:
            - docker
          caches:
            - maven
            - docker
          script:
            - echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin harbor.medsoft.care
            - TAG=$(echo $BITBUCKET_COMMIT | head -c 8) # Use the first 8 characters of the commit hash as the tag
            - echo $TAG
            - DOCKER_BUILDKIT=1 docker build -t harbor.medsoft.care/medsoft8/admin-service:$TAG .
            - docker push harbor.medsoft.care/medsoft8/admin-service:$TAG
            - docker tag harbor.medsoft.care/medsoft8/admin-service:$TAG harbor.medsoft.care/medsoft8/admin-service:latest
            - docker push harbor.medsoft.care/medsoft8/admin-service:latest
      - step:
          fail-fast: true
          user: root
          runs-on:
            - self.hosted
            - linux
            - office
          name: Deploy to Kubernetes
          script:
            - TAG=$(echo $BITBUCKET_COMMIT | head -c 8) # Use the first 8 characters of the commit hash as the tag
            - pipe: atlassian/kubectl-run:3.10.0
              variables:
                KUBE_CONFIG: $KUBE_CONFIG
                KUBECTL_COMMAND: set image deployment/admin-service admin-service=harbor.medsoft.care/medsoft8/admin-service:$TAG
```


Answers:
author: Theodora Boudale
body: Hi [@bayarkhuu](/t5/user/viewprofilepage/user-id/5419759),

Caches can be used for directories in the build container. Your build doesn't download the maven dependencies in the build container, but in the Docker image that you are building. Therefore, there is nothing in the directory /root/.m2/repository of the build container to cache.

You can use a Docker cache, but the predefined docker cache used for caching the layers produced during Docker Build operations does not cache layers produced when using BuildKit.

If Docker BuildKit is enabled and the build layers need to be cached, we recommend using the Docker Build **--cache-from** option. This allows one or more tagged images stored in an external image registry to be used as a cache source. You can find more details here:

* <https://support.atlassian.com/bitbucket-cloud/docs/run-docker-commands-in-bitbucket-pipelines/#Docker-BuildKit-caching-limitations>

Kind regards,  
Theodora


Tags: bitbucket-cloud

bitbucket-pipelines

cloud

docker

maven