Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Halaman ini menjelaskan cara menggunakan Cloud Build untuk mem-build dan menguji
aplikasi berbasis Java, menyimpan artefak yang di-build di repositori Maven di
Artifact Registry, dan membuat informasi asal build.
Memiliki repositori Maven di Artifact Registry. Jika belum memilikinya, buat repositori baru.
Untuk menjalankan perintah gcloud di halaman ini, instal
Google Cloud CLI.
Menggunakan gambar maven
Anda dapat mengonfigurasi Cloud Build untuk mem-build aplikasi Java menggunakan image maven dari Docker Hub.
Untuk menjalankan tugas dalam image maven, tambahkan langkah ke konfigurasi build dengan kolom berikut:
name: Tetapkan nilai kolom ini ke maven atau maven:<tag>, dengan tag mewakili versi. Jika Anda tidak menentukan tag image, Cloud Build
akan menggunakan image latest secara default.
entrypoint: Menetapkan kolom ini akan mengganti titik entri default
gambar yang dirujuk di name. Tetapkan nilai kolom ini ke mvn untuk memanggil
mvn sebagai titik entri langkah build dan menjalankan perintah mvn.
args: Kolom args dari langkah build mengambil daftar argumen dan
meneruskannya ke image yang dirujuk oleh kolom name.
Langkah build berikut menentukan entrypoint untuk image maven
yang diberi tag sebagai 3.3-jdk-8 dan mencetak versi alat build:
Di direktori utama project, buat file konfigurasi build bernama
cloudbuild.yaml.
Menjalankan pengujian: maven menyediakan maven test, yang mendownload dependensi, mem-build aplikasi, dan menjalankan pengujian apa pun yang ditentukan dalam kode sumber Anda. Kolom args dari langkah build mengambil daftar argumen dan meneruskannya ke gambar yang dirujuk oleh kolom name.
Dalam file konfigurasi build, tambahkan test ke kolom args untuk memanggil test
dalam maven:
Aplikasi paket: Untuk memaketkan aplikasi ke dalam file JAR
untuk image maven, tentukan perintah package di kolom args.
Perintah package mem-build file JAR di /workspace/target/.
location: lokasi untuk repositori Anda di Artifact Registry.
project-id: ID Google Cloud project yang berisi repositori Artifact Registry Anda.
repository-name: nama repositori Maven Anda di Artifact Registry.
app-path: jalur ke aplikasi yang dipaketkan.
build-artifact: nama file paket yang dibuat dari langkah build.
group-id: mengidentifikasi project Anda secara unik di semua project Maven, dalam format com.mycompany.app. Untuk informasi selengkapnya, lihat panduan Maven untuk konvensi penamaan.
version: nomor versi untuk aplikasi Anda, diformat dalam angka dan titik seperti 1.0.1.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-09-04 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)."]]