Build and test Java applications Stay organized with collections Save and categorize content based on your preferences.
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
- Be familiar with creating Java-based applications.
- Be familiar withMaven
- Have your Java project ready.
- Be familiar withhow to write a Cloud Build configuration file.
- Have a Maven repository in Artifact Registry. If you do not have one,create a new repository.
- To run the
gcloudcommands in this page, install theGoogle Cloud CLI.
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 tomavenormaven:<tag>, where thetag represents the version. If you don't specify the image tag, Cloud Builduses thelatestimage by default.entrypoint: Setting this field overrides the default entry point of theimage referenced inname. Set the value of this field tomvnto invokemvnas the entrypoint of the build step and runmvncommands.args: Theargsfield of a build step takes a list of arguments andpasses them to the image referenced by thenamefield.
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
In your project root directory, create a build config file named
cloudbuild.yaml.Run tests:
mavenprovidesmaven test, which downloads dependencies, builds the applications, and runs any tests specified in your source code. Theargsfield of a build step takes a list of arguments and passes them to the image referenced by thenamefield.In your build config file, add
testto theargsfield to invoketestwithinmaven:steps:-name:maven:3.3-jdk-8entrypoint:mvnargs:['test']Package application: To package your application into a JAR filefor your
mavenimage, specify thepackagecommand in theargsfield.Thepackagecommand builds a JAR file in/workspace/target/.The following build step packages your Java application:
Note: Thesteps:-name:maven:3.3-jdk-8entrypoint:mvnargs:['package','-Dmaven.test.skip=true']packagecommand re-runs tests. Adding-Dmaven.test.skip=trueto theargsfield will skip tests.Upload to Artifact Registry:
In your build config file, use the
mavenArtifactsfield 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 format
com.mycompany.app. For more information, see theMaven guide to naming conventions. - version: the version number for your application,formatted in numbers and dots like
1.0.1.
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, add
requestedVerifyOption: VERIFIEDto theoptionssection in your config file.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
- Learn how toview build results.
- Learn how tosafeguard builds.
- Learn how toperform blue/green deployments on Compute Engine.
- Learn how tobuild and containerize Java applications.
- Learn how todeploy an application on Cloud Run.
- Learn how todeploy an application on GKE.
- Learn how totroubleshoot build errors.
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.