Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

What is the proper way to use a Maven cache in a muilt-step build?

Deleted user April 14, 2022

I have a Bitbucket pipeline that runs Maven several times in several steps. However, it appears that most (all?) of what is needed by Maven misses the cache no matter how many times I run the build within the week. I'm hoping that the issue is one of my misunderstanding on how to use the cache. What is the proper way to use a Maven cache in a muilt-step build?

Note: this is NOT a multi-module Maven build (in case that makes a difference)

Additional Question: Does the cache get updated with additional dependencies throughout the build or does it only get picked up for one of the steps?

This is an example of one of my pipelines that exhibits the behavior (the paste into this editor ate the formatting, so I apologize if there's some mis-aligned whitespace invalidating the YAML).

definitions:
  steps:
    - step: &build-test
      name: Build and Test
      caches:
        - maven
      script:
        - mvn verify

pipelines:
  default:
    - step: *build-test

branches:
  master:
    - step:
      name: Set Version and Environment
      caches:
        - maven
      script:
        - >
          mvn build-helper:parse-version versions:set
          -DnewVersion=\${parsedVersion.majorVersion}.\${parsedVersion.minorVersion}-${BITBUCKET_BUILD_NUMBER}
        - >
          echo "export VERSION=$(mvn help:evaluate
          -Dexpression=project.version
          -q -DforceStdout)" > version
      artifacts:
        - pom.xml
        - version

    - step:
      name: Build, Test, Package, and Publish
      caches:
        - maven
      script:
        - source ./version
        - mvn verify

    - step:
      name: Update Versions and Push
      caches:
        - maven
      script:
        # Commit and tag the built release version
        - mvn scm:add@add scm:checkin scm:tag@tag -Dscm=rel

        # Set the new development version

        - >
          mvn build-helper:parse-version versions:set
          -DnewVersion=\${parsedVersion.majorVersion}.\${parsedVersion.nextMinorVersion}-SNAPSHOT

        # Commit the new development version
        - mvn scm:add@add scm:checkin -Dscm=dev

pull-requests:
  '**':
    - step:
      <<: *build-test
      after-script:
        - pipe: atlassian/checkstyle-report:0.3.0

1 answer

0 votes
Mark C
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 18, 2022

Hi @[deleted]

Thank you for reaching out to the community.

To answer your question, a Cache will only be uploaded once in a step.
Unfortunately, it's not get updated in the succeeding steps.

We do have an existing feature request for it to refresh Cache dependencies when gets updated that can be located through this link. - https://jira.atlassian.com/browse/BCLOUD-16314
You can upvote and watch it for now so that you'll be notified of any updates from our team when the feature becomes available on Bitbucket Cloud.
Please do note that we don't have an exact ETA for the feature request as all new features will be implemented according to our policy here.

As a workaround, you can refresh your cache by clearing it in a step by the use of a Pipes script called atlassian/bitbucket-clear-cache.

script:
  - pipe: atlassian/bitbucket-clear-cache:3.1.1
    variables:
      BITBUCKET_USERNAME: $BITBUCKET_USERNAME
      BITBUCKET_APP_PASSWORD: $BITBUCKET_APP_PASSWORD
      CACHES: ["maven", "node"]

Hope it helps and do let me know if you have further questions that I can help with.

Regards,
Mark C

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events