Maven caching does not work

Sandhya Banwarie June 12, 2023

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

1 answer

1 accepted

0 votes
Answer accepted
Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 13, 2023

Hello @Sandhya Banwarie  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 , 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 : 

Thank you, @Sandhya Banwarie !

Patrik S

Sandhya Banwarie June 13, 2023

Hello @Patrik S 

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

image: maven:3.6.3


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 

Kind regards, Sandhya

Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 14, 2023

Hello @Sandhya Banwarie 

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) ? 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 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 .

Patrik S

Sandhya Banwarie June 14, 2023

Hi @Patrik S 

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

Like Patrik S likes this

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
TAGS
AUG Leaders

Atlassian Community Events