Maven cache sometimes not working
Asked by Mark Gortzak:

I have a pipeline that does a Maven install, and then a SonarCloud analysis using Maven. I have a m2 repository cache configured in the pipeline. I see the cache being used during the Maven install, but not in the Maven SonarCloud analysis. This is constant after three builds.

I have a private Nexus repository where dependencies are downloaded.  
The cache is configured as follows:

```
definitions:
? caches:
? ? custom-maven:
? ? ? key:
? ? ? ? files:
? ? ? ? ? - pom.xml
? ? ? path: /usr/share/maven/ref/repository
```

The Maven install step is as follows:

```
- step: &build-test
? ? name: Build & test
? ? caches:
? ? ? - custom-maven
? ? script:
? ? ? - mvn -B -T2C install -s ./settings.xml
? ? artifacts:
? ? ? - target/*.jar*
```

The SonarCloud analysis:

```
- step: &sonarcloud
? ? name: Static code analysis using SonarCloud
? ? image:
? ? ? name: [some private image]
? ? ? username: $DOCKER_USERNAME
? ? ? password: $DOCKER_PASSWORD
? ? caches:
? ? ? - custom-maven
? ? ? - custom-sonar
? ? script:
? ? ? - mvn -B -T2C verify sonar:sonar -s ./settings.xml
```

The (I think) relevant bit from settings.xml:

```
<localRepository>/usr/share/maven/ref/repository</localRepository>
```

The logs of the Maven install:
> + mvn -B -T2C install -s ./settings.xml  
> \[INFO\] Scanning for projects...  
> \[INFO\]  
> \[INFO\] Using the MultiThreadedBuilder implementation with a thread count of 8  
> \[INFO\]  
> \[INFO\] ----------------------\< package:artifact \>----------------------  
> \[INFO\] Building artifact 1.2.3.BUILD-SNAPSHOT  
> \[INFO\] --------------------------------\[ jar \]---------------------------------

The logs of the SonarCloud analysis:
> + mvn -B -T2C verify sonar:sonar -s ./settings.xml  
> \[INFO\] Scanning for projects...  
> \[INFO\] Downloading from private-nexus: <https://private.nexus.com/repository/maven-public/com/spotify/dockerfile-maven-plugin/1.4.10/dockerfile-maven-plugin-1.4.10.pom>  
> \[INFO\] Downloaded from private-nexus: <https://private.nexus.com/repository/maven-public/com/spotify/dockerfile-maven-plugin/1.4.10/dockerfile-maven-plugin-1.4.10.pom> (5.6 kB at 5.7 kB/s)  
> \[INFO\] Downloading from private-nexus: <https://private.nexus.com/repository/maven-public/com/spotify/dockerfile-maven/1.4.10/dockerfile-maven-1.4.10.pom>  
> \[INFO\] Downloaded from private-nexus: <https://private.nexus.com/repository/maven-public/com/spotify/dockerfile-maven/1.4.10/dockerfile-maven-1.4.10.pom> (2.4 kB at 8.1 kB/s)  
> \[INFO\] Downloading from private-nexus: <https://private.nexus.com/repository/maven-public/com/spotify/foss-root/6/foss-root-6.pom>  
> ...  
> many more downloads  
> ...  
> \[INFO\] Downloading from private-nexus: <https://private.nexus.com/repository/maven-public/org/sonarsource/scanner/maven/sonar-maven-plugin/3.9.1.2184/sonar-maven-plugin-3.9.1.2184.jar>  
> \[INFO\] Downloaded from private-nexus: <https://private.nexus.com/repository/maven-public/org/sonarsource/scanner/maven/sonar-maven-plugin/3.9.1.2184/sonar-maven-plugin-3.9.1.2184.jar> (50 kB at 177 kB/s)  
> \[INFO\]  
> \[INFO\] Using the MultiThreadedBuilder implementation with a thread count of 8  
> \[INFO\]  
> \[INFO\] ----------------------\< package:artifact \>----------------------  
> \[INFO\] Building artifact 1.2.3.BUILD-SNAPSHOT  
> \[INFO\] --------------------------------\[ jar \]---------------------------------

The cache is available when I look in the Bitbucket UI:

![cache.png](/t5/image/serverpage/image-id/274146iA2DCC0509B9757AD/image-size/large?v=v2&px=999 "cache.png")

What I want is for the caching to work during the SonarCloud analysis as well. What is going on here? What can I do to fix this?


Answers:
author: Mark Gortzak
body: I talked with support, the cache is being used. The build time is faster. Maven seems to sometimes/always download certain dependencies. This might be a problem with our Maven configuration. So it has nothing to do with Bitbucket.


author: Syahrul
body: G'day, [@Mark Gortzak](/t5/user/viewprofilepage/user-id/5288572)

We require to review your pipeline build to investigate the issue further. Since an in-depth investigation is required, I have proceeded to raise a support ticket on your behalf.

Please find your ticket at our support portal:

<https://support.atlassian.com/requests/>

Regards,  
Syahrul


Tags: bitbucket-cloud

cloud