Maven caching does not work
Asked by Sandhya Banwarie:

I have created a simple bitbucket-pipeline.yaml

The caching maven does not work. Build time is almost 1 hour. What am i doing wrong here? Adter build is succesful i don't see any cache in pipeline created?

image: maven:3.6.3

pipelines:  
branches:  
CSO_pipelines:  
- step:  
name: Docker build and clean install  
caches:  
- maven  
script:  
- mkdir -p "$BITBUCKET_CLONE_DIR/.m2"  
- mvn -B -s settings.xml compile  
definitions:  
caches:  
maven: .m2


Answers:
author: Patrik S
body: Hello [@Sandhya Banwarie](/t5/user/viewprofilepage/user-id/5240945) and welcome to Atlassian Community!

I see that in your cache definition you configured the maven cache to the folder **.m2** , however, when you provide the folder in that format, it will be relative to the clone directory (the directory where your repository is cloned inside the container) and maven is not saving the dependencies in that folder.

In this case, I would suggest changing the path in your cache definition to **\~/.m2/repository**which is the default location where Maven will download them :

```
definitions:
  caches:
    maven: \~/.m2/repository
```

If you haven't changed the maven default dependencies path, the definition above should create a cache from the dependencies downloaded during your build.

Also, since maven is one of the available [pre-defined caches in bitbucket pipelines](https://support.atlassian.com/bitbucket-cloud/docs/cache-dependencies/#Pre-defined-caches) , you don't actually need the definition above when using the maven default location, because when you configure a cache named **maven** in your step, it will already point to the **\~/.m2/repository**directory. You can check the other pre-defined caches for common language tools at the following link :

* <https://support.atlassian.com/bitbucket-cloud/docs/cache-dependencies/#Pre-defined-caches>

Thank you, [@Sandhya Banwarie](/t5/user/viewprofilepage/user-id/5240945) !

Patrik S

comments: author: Sandhya Banwarie
body: Hello [@Patrik S](/t5/user/viewprofilepage/user-id/4603309)

Thank for your reply. The way you described I started my pipeline as simple as this:

image: maven:3.6.3

<br />

pipelines:  
branches:  
XXX_pipelines:  
- step:  
name: Clean install

caches:  
- maven  
script:  
- mvn -B -s settings.xml clean install

But then it took the first time almost 55 minutes. The second time I expected that it would run faster. Then i looked in the caches and the dependency is empty.

What am I doing wrong here?

Thank you [@Patrik S](/t5/user/viewprofilepage/user-id/4603309)

Kind regards, Sandhya


author: Patrik S
body: Hello [@Sandhya Banwarie](/t5/user/viewprofilepage/user-id/5240945)

In the repository where you are running the build, if you access the Pipelines page and click on the **Caches** button in the top right corner, does it have any cache entry? This is where the currently existing caches are shown.

Also, I see you are providing a custom settings.xml file to the maven command. By any chance in that file you are changing the **localRepository** attribute to a different directory (see [maven setting.xml reference](https://maven.apache.org/settings.html)) ? If that is the case, then you need to provide a cache definition in your YML file pointing to that directory.

I would also suggest [debugging your build locally with docker](https://confluence.atlassian.com/bbkb/debug-pipelines-locally-with-docker-1167698072.html) and running the same mvn command and check if the dependencies are being correctly downloaded at the local repository folder (default **\~/.m2/repository**) or any custom folder you configured as the localRepository in the setting.xml file.

Thank you, [@Sandhya Banwarie](/t5/user/viewprofilepage/user-id/5240945) .

Patrik S


author: Sandhya Banwarie
body: Hi [@Patrik S](/t5/user/viewprofilepage/user-id/4603309)

Thanks for replying. Everything is correct in my settings.xml file. I do not change the repository there. I changed my script and now I do **mvn -B -s settings.xml clean package**. Then I see the caches are filled when I click on the button Caches in the corner.

Kind regards,

Sandhya


Tags: bitbucket-cloud

cloud