Stay organized with collections
Save and categorize content based on your preferences.
This page explains how to use Cloud Build to build and test
Java-based applications, store built artifacts in a Maven repository in
Artifact Registry, and generate build provenance information.
Before you begin
Be familiar with creating Java-based applications.
Have a Maven repository in Artifact Registry. If you do not have one, create a new repository.
To run the gcloud commands in this page, install the
Google Cloud CLI.
Using the maven image
You can configure Cloud Build to build Java applications using the
maven image from Docker Hub.
To execute your tasks in the maven image, add a step to your build config with the following fields:
name: Set the value of this field to maven or maven:<tag>, where the
tag represents the version. If you don't specify the image tag, Cloud Build
uses the latest image by default.
entrypoint: Setting this field overrides the default entry point of the
image referenced in name. Set the value of this field to mvn to invoke
mvn as the entrypoint of the build step and run mvn commands.
args: The args field of a build step takes a list of arguments and
passes them to the image referenced by the name field.
The following build step specifies the entrypoint for the maven image
tagged as 3.3-jdk-8 and prints the build tool version:
In your project root directory, create a build config file named
cloudbuild.yaml.
Run tests: maven provides maven test, which downloads dependencies, builds the applications, and runs any tests specified in your source code. The args field of a build step takes a list of arguments and passes them to the image referenced by the name field.
In your build config file, add test to the args field to invoke test
within maven:
Package application: To package your application into a JAR file
for your maven image, specify the package command in the args field.
The package command builds a JAR file in /workspace/target/.
The following build step packages your Java application:
location: the location 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 the Maven guide to naming conventions.
version: the version number for your application, formatted in numbers and dots like 1.0.1.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-28 UTC."],[[["\u003cp\u003eThis page provides a guide on using Cloud Build to build and test Java applications, utilizing the \u003ccode\u003emaven\u003c/code\u003e image from Docker Hub.\u003c/p\u003e\n"],["\u003cp\u003eYou can run tests and package your Java application into a JAR file using \u003ccode\u003emaven\u003c/code\u003e commands within the Cloud Build configuration file.\u003c/p\u003e\n"],["\u003cp\u003eCloud Build allows you to upload your packaged application to a designated Maven repository in Artifact Registry by specifying details in the \u003ccode\u003emavenArtifacts\u003c/code\u003e field.\u003c/p\u003e\n"],["\u003cp\u003eYou have the option to enable verifiable build provenance metadata via SLSA, enhancing the security of your continuous integration pipeline.\u003c/p\u003e\n"],["\u003cp\u003eThe build process can be initiated manually or through automated build triggers, and the resulting artifacts and provenance can be viewed within Artifact Registry.\u003c/p\u003e\n"]]],[],null,["# Build and test Java applications\n\nThis page explains how to use Cloud Build to build and test\nJava-based applications, store built artifacts in a Maven repository in\nArtifact Registry, and generate build provenance information.\n\nBefore you begin\n----------------\n\n- Be familiar with creating Java-based applications.\n- Be familiar with [Maven](https://maven.apache.org/maven-features.html)\n- Have your Java project ready.\n- Be familiar with [how to write a Cloud Build configuration file](/build/docs/build-config).\n- Have a Maven repository in Artifact Registry. If you do not have one, [create a new repository](/artifact-registry/docs/repositories/create-repos).\n- To run the `gcloud` commands in this page, install the [Google Cloud CLI](/sdk).\n\nUsing the `maven` image\n-----------------------\n\nYou can configure Cloud Build to build Java applications using the\n[`maven` image](https://hub.docker.com/_/maven) from Docker Hub.\n\nTo execute your tasks in the `maven` image, add a step to your build config with the following fields:\n\n- `name`: Set the value of this field to `maven` or `maven:\u003ctag\u003e`, where the tag represents the version. If you don't specify the image tag, Cloud Build uses the `latest` image by default.\n- `entrypoint`: Setting this field overrides the default entry point of the image referenced in `name`. Set the value of this field to `mvn` to invoke `mvn` as the entrypoint of the build step and run `mvn` commands.\n- `args`: The `args` field of a build step takes a list of arguments and passes them to the image referenced by the `name` field.\n\nThe following build step specifies the `entrypoint` for the `maven` image\ntagged as `3.3-jdk-8` and prints the build tool version: \n\n steps:\n - name: maven:3.3-jdk-8\n entrypoint: mvn\n args: ['--version']\n\nConfiguring `Java` builds\n-------------------------\n\n1. In your project root directory, create a build config file named\n `cloudbuild.yaml`.\n\n2. **Run tests** : `maven` provides `maven test`, which downloads dependencies, builds the applications, and runs any tests specified in your source code. The `args` field of a build step takes a list of arguments and passes them to the image referenced by the `name` field.\n\n In your build config file, add `test` to the `args` field to invoke `test`\n within `maven`: \n\n steps:\n - name: maven:3.3-jdk-8\n entrypoint: mvn\n args: ['test']\n\n3. **Package application** : To package your application into a JAR file\n for your `maven` image, specify the `package` command in the `args` field.\n The `package` command builds a JAR file in `/workspace/target/`.\n\n The following build step packages your Java application: \n\n steps:\n - name: maven:3.3-jdk-8\n entrypoint: mvn\n args: ['package','-Dmaven.test.skip=true']\n\n | **Note:** The `package` command re-runs tests. Adding `-Dmaven.test.skip=true` to the `args` field will skip tests.\n4. **Upload to Artifact Registry**:\n\n In your build config file, use the `mavenArtifacts` field to specify your\n application path and your Maven repository in Artifact Registry: \n\n artifacts:\n mavenArtifacts:\n - repository: 'https://\u003cvar translate=\"no\"\u003elocation\u003c/var\u003e-maven.pkg.dev/\u003cvar translate=\"no\"\u003eproject-id\u003c/var\u003e/\u003cvar translate=\"no\"\u003erepository-name\u003c/var\u003e'\n path: '\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eapp\u003c/span\u003e\u003cspan class=\"devsite-syntax-o\"\u003e-\u003c/span\u003e\u003cspan class=\"devsite-syntax-n\"\u003epath\u003c/span\u003e\u003c/var\u003e'\n artifactId: '\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003ebuild\u003c/span\u003e\u003cspan class=\"devsite-syntax-o\"\u003e-\u003c/span\u003e\u003cspan class=\"devsite-syntax-n\"\u003eartifact\u003c/span\u003e\u003c/var\u003e'\n groupId: '\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003egroup\u003c/span\u003e\u003cspan class=\"devsite-syntax-o\"\u003e-\u003c/span\u003e\u003cspan class=\"devsite-syntax-n\"\u003eid\u003c/span\u003e\u003c/var\u003e'\n version: '\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eversion\u003c/span\u003e\u003c/var\u003e'\n\n Replace the following values:\n - \u003cvar translate=\"no\"\u003elocation\u003c/var\u003e: the [location](/artifact-registry/docs/repo-locations) for your repository in Artifact Registry.\n - \u003cvar translate=\"no\"\u003eproject-id\u003c/var\u003e: the ID of the Google Cloud project that contains your Artifact Registry repository.\n - \u003cvar translate=\"no\"\u003erepository-name\u003c/var\u003e: the name of your Maven repository in Artifact Registry.\n - \u003cvar translate=\"no\"\u003eapp-path\u003c/var\u003e: the path to your packaged application.\n - \u003cvar translate=\"no\"\u003ebuild-artifact\u003c/var\u003e: the name of your package file created from your build step.\n - \u003cvar translate=\"no\"\u003egroup-id\u003c/var\u003e: uniquely identifies your project across all Maven projects, in the format `com.mycompany.app`. For more information, see the [Maven guide to naming conventions](https://maven.apache.org/guides/mini/guide-naming-conventions.html).\n - \u003cvar translate=\"no\"\u003eversion\u003c/var\u003e: the version number for your application, [formatted](https://maven.apache.org/guides/mini/guide-naming-conventions.html) in numbers and dots like `1.0.1`.\n5. **Optional: Enable provenance generation**\n\n Cloud Build can generate verifiable\n [Supply chain Levels for Software Artifacts (SLSA)](https://slsa.dev/) build\n provenance metadata to help secure your continuous integration pipeline.\n\n To enable provenance generation, add\n [`requestedVerifyOption: VERIFIED`](/build/docs/build-config-file-schema#options)\n to the `options` section in your config file.\n6. **Start your build** : [manually](/build/docs/running-builds/start-build-manually) or\n [using build triggers](/build/docs/automating-builds/create-manage-triggers).\n\n Once your build completes, you can [view repository details](/artifact-registry/docs/repositories/list-repos)\n in Artifact Registry.\n\n You can also [view build provenance metadata](/build/docs/securing-builds/generate-validate-build-provenance#view) and [validate provenance](/build/docs/securing-builds/generate-validate-build-provenance#validate_provenance).\n\nWhat's next\n-----------\n\n- Learn how to [view build results](/build/docs/view-build-results).\n- Learn how to [safeguard builds](/software-supply-chain-security/docs/safeguard-builds).\n- Learn how to [perform blue/green deployments on Compute Engine](/build/docs/deploying-builds/deploy-compute-engine).\n- Learn how to [build and containerize Java applications](/build/docs/building/build-containerize-java).\n- Learn how to [deploy an application on Cloud Run](/build/docs/deploying-builds/deploy-cloud-run).\n- Learn how to [deploy an application on GKE](/build/docs/deploying-builds/deploy-gke).\n- Learn how to [troubleshoot build errors](/build/docs/troubleshooting)."]]