Skip to main content

Building and testing Java with Gradle

You can create a continuous integration (CI) workflow in GitHub Actions to build and test your Java project with Gradle.

注意

GitHub Enterprise Server 目前不支持 GitHub 托管的运行器。 可以在 GitHub public roadmap 上查看有关未来支持计划的更多信息。

Introduction

This guide shows you how to create a workflow that performs continuous integration (CI) for your Java project using the Gradle build system. The workflow you create will allow you to see when commits to a pull request cause build or test failures against your default branch; this approach can help ensure that your code is always healthy. You can extend your CI workflow to cache files and upload artifacts from a workflow run.

GitHub-hosted runners have a tools cache with pre-installed software, which includes Java Development Kits (JDKs) and Gradle. For a list of software and the pre-installed versions for JDK and Gradle, see Using GitHub-hosted runners.

Prerequisites

You should be familiar with YAML and the syntax for GitHub Actions. For more information, see:

We recommend that you have a basic understanding of Java and the Gradle framework. For more information, see the Gradle User Manual.

在 GitHub Enterprise Server 上使用自托管的运行器

在包含自承载运行器的 GitHub Enterprise Server 上使用设置操作(例如 actions/setup-LANGUAGE)时,可能需要在无法访问 Internet 的运行器上设置工具缓存。 有关详细信息,请参阅“在未接入互联网的自托管运行器上设置工具缓存”。

Using a Gradle workflow template

若要快速开始使用,请将工作流模板添加到存储库的 .github/workflows 目录。

GitHub provides a workflow template for Gradle that should work for most Java with Gradle projects. The subsequent sections of this guide give examples of how you can customize this workflow template.

  1. 在 GitHub 上,导航到存储库的主页面。

  2. 在存储库名称下,单击 “操作”。

    “github/docs”存储库的选项卡的屏幕截图。 “操作”选项卡以橙色边框突出显示。

  3. 如果存储库中已有工作流,请单击“新建工作流”。

  4. The "Choose a workflow" page shows a selection of recommended workflow templates. Search for "Java with Gradle".

  5. On the "Java with Gradle" workflow, click Configure.

    If you don't find the "Java with Gradle" workflow template, copy the following workflow code to a new file called gradle.yml in the .github/workflows directory of your repository.

    YAML
    name: Java CI with Gradle
    
    on:
      push:
        branches: [ "main" ]
      pull_request:
        branches: [ "main" ]
    
    permissions:
      contents: read
    
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
        - uses: actions/checkout@v4
        - name: Set up JDK 17
          uses: actions/setup-java@v4
          with:
            java-version: '17'
            distribution: 'temurin'
    
        - name: Setup Gradle
          uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0
    
        - name: Build with Gradle
          run: ./gradlew build
    

此工作流程执行以下步骤:

  1. 检出项目仓库的副本。

  2. 设置 Java JDK。

  3. 设置 Gradle 环境。 gradle/actions/setup-gradle 操作负责工作流运行之间的缓存状态,并提供所有 Gradle 执行的详细摘要。

  4. The "Build with Gradle" step executes the build task using the Gradle Wrapper.

  5. Edit the workflow as required. For example, change the Java version.

    注意

    • 此工作流模板包含未通过 GitHub 认证的操作。 第三方提供的操作受单独的服务条款、隐私政策和支持文档管辖。
    • 如果使用来自第三方的操作,则应使用提交 SHA 指定的版本。 如果操作已被修改,并且需要使用较新版本,则需要更新 SHA。 可以通过引用标记或分支来指定版本,但操作可能会在没有事先警告的情况下更改。 有关详细信息,请参阅“Security hardening for GitHub Actions”。
  6. Click Commit changes.

指定 Java 版本和体系结构

工作流模板将 PATH 设置为包含 OpenJDK 8,用于 x64 平台。 如果要使用不同的 Java 版本或面向不同的体系结构(x64x86),可以使用 setup-java 操作选择不同的 Java 运行时环境。

例如,要使用由 Adoptium 提供的 11 版本的 JDK,用于 x64 平台,可以使用 setup-java 操作并将 java-versiondistributionarchitecture 参数配置为 '11''temurin'x64

YAML
steps:
  - uses: actions/checkout@v4
  - name: Set up JDK 11 for x64
    uses: actions/setup-java@v4
    with:
      java-version: '11'
      distribution: 'temurin'
      architecture: x64

有关详细信息,请参阅 setup-java 操作。

Building and testing your code

You can use the same commands that you use locally to build and test your code.

The workflow template will run the build task by default. In the default Gradle configuration, this command will download dependencies, build classes, run tests, and package classes into their distributable format, for example, a JAR file.

If you use different commands to build your project, or you want to use a different task, you can specify those. For example, you may want to run the package task that's configured in your ci.gradle file.

YAML
steps:
  - uses: actions/checkout@v4
  - uses: actions/setup-java@v4
    with:
      java-version: '17'
      distribution: 'temurin'

  - name: Setup Gradle
    uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0

  - name: Build with Gradle
    run: ./gradlew -b ci.gradle package

Caching dependencies

Your build dependencies can be cached to speed up your workflow runs. After a successful run, gradle/actions/setup-gradle caches important parts of the Gradle user home directory. In future jobs, the cache will be restored so that build scripts won't need to be recompiled and dependencies won't need to be downloaded from remote package repositories.

Caching is enabled by default when using the gradle/actions/setup-gradle action. For more information, see gradle/actions/setup-gradle.

Packaging workflow data as artifacts

After your build has succeeded and your tests have passed, you may want to upload the resulting Java packages as a build artifact. This will store the built packages as part of the workflow run, and allow you to download them. Artifacts can help you test and debug pull requests in your local environment before they're merged. For more information, see Storing and sharing data from a workflow.

Gradle will usually create output files like JARs, EARs, or WARs in the build/libs directory. You can upload the contents of that directory using the upload-artifact action.

YAML
steps:
  - uses: actions/checkout@v4
  - uses: actions/setup-java@v4
    with:
      java-version: '17'
      distribution: 'temurin'

  - name: Setup Gradle
    uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0

  - name: Build with Gradle
    run: ./gradlew build

  - name: Upload build artifacts
    uses: actions/upload-artifact@v3
    with:
      name: Package
      path: build/libs