'Store maven dependencies in Docker image for Gitlab CI test job

Current setting in my testing job is to run a "mvn clean verify" script to execute an integration test for my application.

But the problem is the mvn clean verify will rebuild and re-download dependencies everytime it run so it will be really time consuming.

I'm currently using shared runner and can't have dedicate runner for my test. Is there anyway for me to cache the maven downloaded dependencies and plugins?

currently I'm thinking about build a docker that store m2 folder and put it in the pipeline like this

test:
  stage: test
  image: my.registry/maven.dependencies
  script:
    - mvn verify -Dtags=@TEST -Denv=default


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source