Skip to main content

Building and testing Java with Maven

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

Introduction

This guide shows you how to create a workflow that performs continuous integration (CI) for your Java project using the Maven software project management tool. 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 Maven. For a list of software and the pre-installed versions for JDK and Maven, 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 Maven framework. For more information, see the Maven Getting Started Guide in the Maven documentation.

Using a Maven workflow template

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

GitHub provides a workflow template for Maven that should work for most Java with Maven 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 Maven".

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

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

  7. Click Commit changes.

    The maven.yml workflow file is added to the .github/workflows directory of your repository.

指定 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 package target by default. In the default Maven 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 target, you can specify those. For example, you may want to run the verify target that's configured in a pom-ci.xml file.

YAML
steps:
  - uses: actions/checkout@v4
  - uses: actions/setup-java@v4
    with:
      java-version: '17'
      distribution: 'temurin'
  - name: Run the Maven verify phase
    run: mvn --batch-mode --update-snapshots verify

Caching dependencies

You can cache your dependencies to speed up your workflow runs. After a successful run, your local Maven repository will be stored in a cache. In future workflow runs, the cache will be restored so that dependencies don't need to be downloaded from remote Maven repositories. You can cache dependencies simply using the setup-java action or can use cache action for custom and more advanced configuration.

YAML
steps:
  - uses: actions/checkout@v4
  - name: Set up JDK 17
    uses: actions/setup-java@v4
    with:
      java-version: '17'
      distribution: 'temurin'
      cache: maven
  - name: Build with Maven
    run: mvn --batch-mode --update-snapshots verify

This workflow will save the contents of your local Maven repository, located in the .m2 directory of the runner's home directory. The cache key will be the hashed contents of pom.xml, so changes to pom.xml will invalidate the cache.

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.

Maven will usually create output files like JARs, EARs, or WARs in the target directory. To upload those as artifacts, you can copy them into a new directory that contains artifacts to upload. For example, you can create a directory called staging. Then 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'
  - run: mvn --batch-mode --update-snapshots verify
  - run: mkdir staging && cp target/*.jar staging
  - uses: actions/upload-artifact@v4
    with:
      name: Package
      path: staging