[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-18 (世界標準時間)。"],[[["\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)."]]