'github action maven dependency caching

I introduce myself currently into github actions and several maven operations. I think I have some missunderstanding how the dependency caching is working inside my github actions. I have this action:

runs:
  using: composite
  steps:

    - name: Cache m2 dependencies
      uses: actions/cache@v2
      with:
        path: .m2/repository
        key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
        restore-keys: |
          ${{ runner.os }}-maven-
    - name: Run ${{ inputs.name }}
      run: mvn compile exec:java -Dexec.mainClass="Order" -Dexec.args="${{ inputs.order }}" -B
      shell: bash
      

This action is executed without problem 5 times within my pipeline chane.

But... In each task the dependencies are downloaded again. In my case 5 times. I thought I get rid of that with the "Cache m2 dependencies" ? The cache is recognized:

Received 46137344 of 81878765 (56.3%), 10.9 MBs/sec
73
Received 71303168 of 81878765 (87.1%), 13.5 MBs/sec
74
Received 81878765 of 81878765 (100.0%), 13.5 MBs/sec
75
Cache Size: ~78 MB (81878765 B)
76
/bin/tar -z -xf /__w/_temp/883f2a2b-30d5-4597-bd4e-230d73e83443/cache.tgz -P -C /__w/test-prj/test-prj
77
Cache restored successfully

however in the next step "Run ${{ inputs.name }}" everything is downloaded again and again.

[INFO] ----------------< test-prj >----------------
199
[INFO] Building test-prj 1.0
200
[INFO] --------------------------------[ jar ]---------------------------------
201
[INFO] Downloading from thirdparty-rep: https://thirdparty-rep.com/public/repositories/maven/io/appium/java-client/7.4.1/java-client-7.4.1.pom
202
[INFO] Downloading from central: https://repo.maven.apache.org/maven2/io/appium/java-client/7.4.1/java-client-7.4.1.pom
203
[INFO] Downloaded from central: https://repo.maven.apache.org/maven2/io/appium/java-client/7.4.1/java-client-7.4.1.pom (4.7 kB at 55 kB/s)
204
[INFO] Downloading from thirdparty-rep: https://thirdparty-rep.com/public/repositories/maven/org/seleniumhq/selenium/selenium-java/3.141.59/selenium-java-3.141.59.pom
205
[INFO] Downloading from central: https://repo.maven.apache.org/maven2/org/seleniumhq/selenium/selenium-java/3.141.59/selenium-java-3.141.59.pom

Besides of my dependencies in the pom.xml, I have additionaly placed a repository for some third-party libs:

<repositories>
    <repository>
        <id>my-third-party-repo</id>
        <url>https://thirdparty-rep.com/public/repositories/maven</url>
    </repository>
</repositories>

maybe you see my problem with the caching...



Solution 1:[1]

Two possible reasons:

  1. You changed the POM file in between. Then the cache is deleted.

  2. Maven is often downloading external POMs to evaluate dependency graphs. I do not really know why, but as long as no JAR files are downloaded, I would not worry to much because POMs are really small and downloaded super fast.

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 J Fabian Meier