Skip to content

Latest commit

 

History

History
142 lines (108 loc) · 7.32 KB

building-and-testing-java-with-gradle.md

File metadata and controls

142 lines (108 loc) · 7.32 KB
title intro redirect_from versions type topics shortTitle
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.
/actions/language-and-framework-guides/building-and-testing-java-with-gradle
/actions/guides/building-and-testing-java-with-gradle
fpt ghes ghae ghec
*
*
*
*
tutorial
CI
Java
Gradle
Build & test Java & Gradle

{% data reusables.actions.enterprise-beta %} {% data reusables.actions.enterprise-github-hosted-runners %}

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.

{% ifversion ghae %} {% data reusables.actions.self-hosted-runners-software %} {% else %} {% data variables.product.prodname_dotcom %}-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 "Specifications for {% data variables.product.prodname_dotcom %}-hosted runners". {% endif %}

Prerequisites

You should be familiar with YAML and the syntax for {% data variables.product.prodname_actions %}. For more information, see:

We recommend that you have a basic understanding of Java and the Gradle framework. For more information, see Getting Started in the Gradle documentation.

{% data reusables.actions.enterprise-setup-prereq %}

Using the Gradle starter workflow

{% data variables.product.prodname_dotcom %} provides a Gradle starter workflow that will work for most Gradle-based Java projects. For more information, see the Gradle starter workflow.

To get started quickly, you can choose the preconfigured Gradle starter workflow when you create a new workflow. For more information, see the "{% data variables.product.prodname_actions %} quickstart."

You can also add this workflow manually by creating a new file in the .github/workflows directory of your repository.

{% data reusables.actions.actions-not-certified-by-github-comment %}

name: Java CI

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: {% data reusables.actions.action-checkout %}
      - name: Set up JDK 11
        uses: {% data reusables.actions.action-setup-java %}
        with:
          java-version: '11'
          distribution: 'adopt'
      - name: Validate Gradle wrapper
        uses: gradle/wrapper-validation-action@e6e38bacfdf1a337459f332974bb2327a31aaf4b
      - name: Build with Gradle
        uses: gradle/gradle-build-action@0d13054264b0bb894ded474f08ebb30921341cee
        with:
          arguments: build

This workflow performs the following steps:

  1. The checkout step downloads a copy of your repository on the runner.
  2. The setup-java step configures the Java 11 JDK by Adoptium.
  3. The "Validate Gradle wrapper" step validates the checksums of Gradle Wrapper JAR files present in the source tree.
  4. The "Build with Gradle" step does a build using the gradle/gradle-build-action action provided by the Gradle organization on {% data variables.product.prodname_dotcom %}. The action takes care of invoking Gradle, collecting results, and caching state between jobs. For more information see gradle/gradle-build-action.

The default starter workflows are excellent starting points when creating your build and test workflow, and you can customize the starter workflow to suit your project’s needs.

{% data reusables.actions.example-github-runner %}

{% data reusables.actions.java-jvm-architecture %}

Building and testing your code

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

The starter workflow 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.

steps:
  - uses: {% data reusables.actions.action-checkout %}
  - uses: {% data reusables.actions.action-setup-java %}
    with:
      java-version: '11'
      distribution: 'adopt'
  - name: Validate Gradle wrapper
    uses: gradle/wrapper-validation-action@e6e38bacfdf1a337459f332974bb2327a31aaf4b
  - name: Run the Gradle package task
    uses: gradle/gradle-build-action@0d13054264b0bb894ded474f08ebb30921341cee
    with:
      arguments: -b ci.gradle package

Caching dependencies

When using {% data variables.product.prodname_dotcom %}-hosted runners, your build dependencies can be cached to speed up your workflow runs. After a successful run, the gradle/gradle-build-action 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/gradle-build-action action. For more information, see gradle/gradle-build-action.

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 "Persisting workflow data using artifacts."

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.

steps:
  - uses: {% data reusables.actions.action-checkout %}
  - uses: {% data reusables.actions.action-setup-java %}
    with:
      java-version: '11'
      distribution: 'adopt'
  - name: Validate Gradle wrapper
    uses: gradle/wrapper-validation-action@e6e38bacfdf1a337459f332974bb2327a31aaf4b
  - name: Build with Gradle
    uses: gradle/gradle-build-action@0d13054264b0bb894ded474f08ebb30921341cee
    with:
      arguments: build
  - uses: {% data reusables.actions.action-upload-artifact %}
    with:
      name: Package
      path: build/libs