Stay organized with collections
Save and categorize content based on your preferences.
This page explains how to use Cloud Build to build, test, and
containerize Java-based applications, upload your container images to
Artifact Registry, and generate build provenance.
Before you begin
Be familiar with creating Java-based applications.
Have your Java project ready, including a Dockerfile.
To run the gcloud commands in this page, install the
Google Cloud CLI.
Using the maven or gradle image
You can configure Cloud Build to build Java applications using the
maven image
or the gradle image
from Docker Hub.
maven
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:
To execute your tasks in the gradle image, add a step to your build config with the following fields:
name: Set the value of this field to gradle or gradle:<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 gradle to invoke
gradle as the entrypoint of the build step and run gradle 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 gradle image
tagged as 5.6.2-jdk8 and prints the build tool version:
In your project root directory, create a build config file named
cloudbuild.yaml.
Run tests: maven and gradle provide maven test and gradle 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 and gradle:
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/.
To package your application into a JAR file for your gradle image,
specify the assemble command in the args field. The assemble command
builds a JAR file in workspace/build/libs.
The following build step packages your Java application:
Containerize application: Cloud Build provides a
prebuilt Docker image
that you can use to containerize your Java application. To containerize your
Java application, in your build config file:
Add a name field and specify the prebuilt Docker image at
gcr.io/cloud-builders/docker.
Add an args field and specify the build arguments, including the name of
the container image to build, and the path to your build artifact.
Add an images field to push the built container image to Artifact Registry.
If you don't specify a config-file-path and source-directory
in the gcloud builds submit command, Cloud Build assumes that the
config file and the source code are in the current working directory.
Here are some example repositories you can use to build Java apps, each of
which contain a sample application and a build config file to build and test
that application:
maven-example: A Java app and an example build config file to build and test
the app with mvn.
gradle-example: A Java app and an example build config file to build and test
the app with gradle.
[[["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-07 UTC."],[[["\u003cp\u003eCloud Build can be used to build, test, and containerize Java applications using \u003ccode\u003emaven\u003c/code\u003e or \u003ccode\u003egradle\u003c/code\u003e images from Docker Hub.\u003c/p\u003e\n"],["\u003cp\u003eTo configure a Java build, a \u003ccode\u003ecloudbuild.yaml\u003c/code\u003e file should be created to define steps for testing, packaging, and containerizing the application.\u003c/p\u003e\n"],["\u003cp\u003eThe process includes using \u003ccode\u003emvn test\u003c/code\u003e or \u003ccode\u003egradle test\u003c/code\u003e for testing, \u003ccode\u003emvn package\u003c/code\u003e or \u003ccode\u003egradle assemble\u003c/code\u003e for packaging, and a prebuilt Docker image for containerization.\u003c/p\u003e\n"],["\u003cp\u003eAfter containerization, the container image can be uploaded to Artifact Registry, and build provenance can be generated for enhanced security.\u003c/p\u003e\n"],["\u003cp\u003eBuilds are initiated with the \u003ccode\u003egcloud builds submit\u003c/code\u003e command, and completed builds can be viewed in Artifact Registry, as well as detailed information on the build provenance metadata.\u003c/p\u003e\n"]]],[],null,["# Build, test, and containerize Java applications\n\nThis page explains how to use Cloud Build to build, test, and\ncontainerize Java-based applications, upload your container images to\nArtifact Registry, and generate build provenance.\n\nBefore you begin\n----------------\n\n- Be familiar with creating Java-based applications.\n- Have your Java project ready, including a `Dockerfile`.\n- Have a Docker repository in Artifact Registry, or [create a new repository](/artifact-registry/docs/repositories/create-repos).\n- Be familiar with [how to write a Cloud Build configuration file](/build/docs/build-config).\n- To run the `gcloud` commands in this page, install the [Google Cloud CLI](/sdk).\n\nUsing the `maven` or `gradle` image\n-----------------------------------\n\nYou can configure Cloud Build to build Java applications using the\n[`maven` image](https://hub.docker.com/_/maven)\nor the [`gradle` image](https://hub.docker.com/_/gradle)\nfrom Docker Hub. \n\n### maven\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\n### gradle\n\nTo execute your tasks in the `gradle` image, add a step to your build config with the following fields:\n\n- `name`: Set the value of this field to `gradle` or `gradle:\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 `gradle` to invoke `gradle` as the entrypoint of the build step and run `gradle` 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 `gradle` image\ntagged as `5.6.2-jdk8` and prints the build tool version: \n\n steps:\n - name: gradle:5.6.2-jdk8\n entrypoint: gradle\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` and `gradle` provide `maven test` and `gradle test`,\n which downloads dependencies, builds the applications, and runs any tests\n specified in your source code. The `args` field of a build step takes a\n 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` and `gradle`: \n\n ### maven\n\n steps:\n - name: maven:3.3-jdk-8\n entrypoint: mvn\n args: ['test']\n\n ### gradle\n\n steps:\n - name: gradle:5.6.2-jdk8\n entrypoint: gradle\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 To package your application into a JAR file for your `gradle` image,\n specify the `assemble` command in the `args` field. The `assemble` command\n builds a JAR file in `workspace/build/libs`.\n\n The following build step packages your Java application: \n\n ### maven\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.\n\n ### gradle\n\n steps:\n - name: gradle:5.6.2-jdk8\n entrypoint: gradle\n args: ['assemble']\n\n4. **Containerize application** : Cloud Build provides a\n [prebuilt Docker image](https://github.com/GoogleCloudPlatform/cloud-builders/tree/master/docker)\n that you can use to containerize your Java application. To containerize your\n Java application, in your build config file:\n\n - Add a `name` field and specify the prebuilt Docker image at `gcr.io/cloud-builders/docker`.\n - Add an `args` field and specify the `build` arguments, including the name of the container image to build, and the path to your build artifact.\n - Add an `images` field to push the built container image to Artifact Registry.\n - Optional: Add [`requestedVerifyOption: VERIFIED`](/build/docs/build-config-file-schema#options)\n within the `options` field in your build config file to enable\n [Supply chain Levels for Software Artifacts (SLSA)](https://slsa.dev/)\n provenance generation.\n\n | **Note:** If you use a `docker push` build step to upload images to Artifact Registry rather than using the `images` field, then your build won't have provenance information.\n\n The following build step containerizes your application, pushes your container\n image to Artifact Registry, and generates build provenance information: \n\n ### maven\n\n steps:\n - name: gcr.io/cloud-builders/docker\n args: ['build', '-t', '\u003cvar translate=\"no\"\u003elocation\u003c/var\u003e-docker.pkg.dev/\u003cvar translate=\"no\"\u003eproject-id\u003c/var\u003e/\u003cvar translate=\"no\"\u003erepository\u003c/var\u003e/\u003cvar translate=\"no\"\u003eimage\u003c/var\u003e', '--build-arg=JAR_FILE=target/\u003cvar translate=\"no\"\u003ebuild-artifact\u003c/var\u003e', '.']\n images: ['\u003cvar translate=\"no\"\u003elocation\u003c/var\u003e-docker.pkg.dev/\u003cvar translate=\"no\"\u003eproject-id\u003c/var\u003e/\u003cvar translate=\"no\"\u003erepository\u003c/var\u003e/\u003cvar translate=\"no\"\u003eimage\u003c/var\u003e']\n\n ### gradle\n\n steps:\n - name: gcr.io/cloud-builders/docker\n args: ['build', '-t', '\u003cvar translate=\"no\"\u003elocation\u003c/var\u003e-docker.pkg.dev/\u003cvar translate=\"no\"\u003eproject-id\u003c/var\u003e/\u003cvar translate=\"no\"\u003erepository\u003c/var\u003e/\u003cvar translate=\"no\"\u003eimage\u003c/var\u003e', '--build-arg=JAR_FILE=build/libs/\u003cvar translate=\"no\"\u003ebuild-artifact\u003c/var\u003e', '.']\n images: ['\u003cvar translate=\"no\"\u003elocation\u003c/var\u003e-docker.pkg.dev/\u003cvar translate=\"no\"\u003eproject-id\u003c/var\u003e/\u003cvar translate=\"no\"\u003erepository\u003c/var\u003e/\u003cvar translate=\"no\"\u003eimage\u003c/var\u003e']\n\n Where:\n - \u003cvar translate=\"no\"\u003elocation\u003c/var\u003e: the regional or multi-regional location for your repository.\n - \u003cvar translate=\"no\"\u003eproject-id\u003c/var\u003e: the ID of your Google Cloud project.\n - \u003cvar translate=\"no\"\u003erepository\u003c/var\u003e: the name of your repository Artifact Registry.\n - \u003cvar translate=\"no\"\u003eimage\u003c/var\u003e: the name of your container image.\n - \u003cvar translate=\"no\"\u003ebuild-artifact\u003c/var\u003e: the name of your JAR file created from your build step.\n5. **Start your build**: When you have your build config\n file ready, start your build by entering the following command in your\n terminal:\n\n gcloud builds submit --region=\u003cvar translate=\"no\"\u003eREGION\u003c/var\u003e --config \u003cvar translate=\"no\"\u003econfig-file-path\u003c/var\u003e \u003cvar translate=\"no\"\u003esource-directory\u003c/var\u003e\n\n Where:\n - \u003cvar translate=\"no\"\u003econfig-file-path\u003c/var\u003e: the path to your build config file. In this example, the build config file is named `cloudbuild.yaml`.\n - \u003cvar translate=\"no\"\u003esource-directory\u003c/var\u003e: the path or URL to your source code.\n - \u003cvar translate=\"no\"\u003eREGION\u003c/var\u003e: one of the [supported build regions](/build/docs/locations).\n\n If you don't specify a \u003cvar translate=\"no\"\u003econfig-file-path\u003c/var\u003e and \u003cvar translate=\"no\"\u003esource-directory\u003c/var\u003e\n in the `gcloud builds submit` command, Cloud Build assumes that the\n config file and the source code are in the current working directory.\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\nCode examples\n-------------\n\nHere are some example repositories you can use to build Java apps, each of\nwhich contain a sample application and a build config file to build and test\nthat application:\n\n- [maven-example](https://github.com/GoogleCloudPlatform/cloud-build-samples/tree/main/maven-example): A Java app and an example build config file to build and test the app with `mvn`.\n- [gradle-example](https://github.com/GoogleCloudPlatform/cloud-build-samples/tree/main/gradle-example): A Java app and an example build config file to build and test the app with `gradle`.\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 [build standalone 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/building/build/docs/deploying-builds/deploy-gke).\n- Learn how to [troubleshoot build errors](/build/docs/troubleshooting)."]]