Github-actions:缓存 repo 以加速 Maven 构建

Github-actions: cache repo to speed up maven builds

每次触发构建工作流时都会下载 Maven 依赖项。

Travis CI 提供了一种缓存 Maven 存储库的方法。 Github 动作是否提供允许缓存 Maven 存储库的类似功能?

显然,自 2019 年 9 月 10 日起,Github 操作中不存在构建依赖项的缓存。 Github 工作人员已确认此功能是必要的,并回复说 "We're working on caching packages and artifacts between workflow executions, we'll have it by mid-November [2019]."

来源:https://github.community/t5/GitHub-Actions/Caching-files-between-GitHub-Action-executions/m-p/30974/highlight/true#M630

依赖缓存现已可用:

https://help.github.com/en/actions/automating-your-workflow-with-github-actions/caching-dependencies-to-speed-up-workflows

为了完整起见,这是一个如何在后续构建中缓存本地 Maven 存储库的示例:

steps:
  # Typical Java workflow steps
  - uses: actions/checkout@v1
  - name: Set up JDK 11
    uses: actions/setup-java@v1
    with:
      java-version: 11
  # Step that does that actual cache save and restore
  - uses: actions/cache@v1
    with:
      path: ~/.m2/repository
      key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
      restore-keys: |
        ${{ runner.os }}-maven-
  # Step that runs the tests
  - name: Run tests
    run: mvn test

有关详细信息,请参阅 this page

对于多模块项目,可以通过使用 https://github.com/avodonosov/hashver-maven-plugin/

仅构建受最近更改影响的模块来加速构建

如@Rob van der Leek 的回答所述,这将在缓存本地 Maven 存储库的可能性下起作用。

mvn install 每次构建后的项目工件,在下一次构建时 hashver-maven-plugin 将找到未受影响模块的工件并跳过它们。

截至 2021 年,最新的官方答案是

- name: Cache local Maven repository
  uses: actions/cache@v2
  with:
    path: ~/.m2/repository
    key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
    restore-keys: |
      ${{ runner.os }}-maven-

取自 github 的官方示例 https://github.com/actions/cache/blob/master/examples.md#java---maven

我开始使用skjolber的Maven Cache Github Action,见https://github.com/marketplace/actions/maven-cache。我的第一次测试非常积极。第一个构建从 Maven Central 下载依赖项;随后的构建在缓存中找到了依赖项。

对于 Maven,您现在可以使用 setup-java 来缓存依赖项。示例:

    - uses: actions/setup-java@v2.3.1
      name: Install Java
      with:
        java-version: 8
        distribution: 'adopt'
        cache: 'maven'

注意与其他setup-*相同的有:nodepythonruby