Build and test Java applications

This page explains how to use Cloud Build to build and testJava-based applications, store built artifacts in a Maven repository inArtifact Registry, and generate build provenance information.

Before you begin

Using themaven image

You can configure Cloud Build to build Java applications using themaven image from Docker Hub.

To execute your tasks in themaven image, add a step to your build config with the following fields:

  • name: Set the value of this field tomaven ormaven:<tag>, where thetag represents the version. If you don't specify the image tag, Cloud Builduses thelatest image by default.
  • entrypoint: Setting this field overrides the default entry point of theimage referenced inname. Set the value of this field tomvn to invokemvn as the entrypoint of the build step and runmvn commands.
  • args: Theargs field of a build step takes a list of arguments andpasses them to the image referenced by thename field.

The following build step specifies theentrypoint for themaven imagetagged as3.3-jdk-8 and prints the build tool version:

steps:-name:maven:3.3-jdk-8entrypoint:mvnargs:['--version']

ConfiguringJava builds

  1. In your project root directory, create a build config file namedcloudbuild.yaml.

  2. Run tests:maven providesmaven test, which downloads dependencies, builds the applications, and runs any tests specified in your source code. Theargs field of a build step takes a list of arguments and passes them to the image referenced by thename field.

    In your build config file, addtest to theargs field to invoketestwithinmaven:

    steps:-name:maven:3.3-jdk-8entrypoint:mvnargs:['test']
  3. Package application: To package your application into a JAR filefor yourmaven image, specify thepackage command in theargs field.Thepackage command builds a JAR file in/workspace/target/.

    The following build step packages your Java application:

    steps:-name:maven:3.3-jdk-8entrypoint:mvnargs:['package','-Dmaven.test.skip=true']
    Note: Thepackage command re-runs tests. Adding-Dmaven.test.skip=true to theargs field will skip tests.
  4. Upload to Artifact Registry:

    In your build config file, use themavenArtifacts field to specify yourapplication path and your Maven repository in Artifact Registry:

    artifacts:mavenArtifacts:-repository:'https://location-maven.pkg.dev/project-id/repository-name'path:'app-path'artifactId:'build-artifact'groupId:'group-id'version:'version'

    Replace the following values:

    • location: thelocation for your repository in Artifact Registry.
    • project-id: the ID of the Google Cloud project that contains your Artifact Registry repository.
    • repository-name: the name of your Maven repository in Artifact Registry.
    • app-path: the path to your packaged application.
    • build-artifact: the name of your package file created from your build step.
    • group-id: uniquely identifies your project across all Maven projects, in the formatcom.mycompany.app. For more information, see theMaven guide to naming conventions.
    • version: the version number for your application,formatted in numbers and dots like1.0.1.
  5. Optional: Enable provenance generation

    Cloud Build can generate verifiableSupply chain Levels for Software Artifacts (SLSA) buildprovenance metadata to help secure your continuous integration pipeline.

    To enable provenance generation, addrequestedVerifyOption: VERIFIEDto theoptions section in your config file.

  6. Start your build:manually orusing build triggers.

    Once your build completes, you canview repository detailsin Artifact Registry.

    You can alsoview build provenance metadata andvalidate provenance.

What's next

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026-02-19 UTC.