Docker caching of .m2 doesn't work locally

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 12:07:06

问题


Consider this of .gitlab-ci.yml:

variables:
     MAVEN_OPTS: "-Dmaven.repo.local=/root/.m2/repository"

cache:
     key: "M2"
     paths:
        - /root/.m2/repository

This works correctly when running directly in gitlab, but it doesn't work locally when running as:

gitlab-runner exec docker test

If I run it locally like this, I can see messages like Successfully extracted cache, but then it re-downloads all the maven dependencies, which in my case meaning downloading hundreds of megabytes.

I cannot see the difference between running it locally and remotely, both of them should work the same I guess?

Where is this cache stored? Can I somehow verify there is actually something stored?


回答1:


As in this issue, you need to put your m2 cache folder into a volume, or the container would start from scratch (empty cache) every time, promting maven to download everything all over again.

That issue also reports, instead of using volumes:

The cache.paths value doesn't need to be specified if you point the repo to something in /cache; the version I'm actually using currently is below, and I don't see the 'Removing..' line for .m2:

image:
  maven:3.3.9-jdk-8

variables:
  MAVEN_OPTS: -Dmaven.repo.local=/cache/maven.repository

test:
  script: "mvn test" 

But using a volume remains the most portable solution:

We edit our config to include a local docker host volume named host-cache, and we just replace /cache references above with /host-cache which doesn't depend on any undocumented runner defaults.



来源:https://stackoverflow.com/questions/48654123/docker-caching-of-m2-doesnt-work-locally

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!