Membangun, menguji, dan menyimpan aplikasi Java dalam container
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Halaman ini menjelaskan cara menggunakan Cloud Build untuk mem-build, menguji, dan
menyimpan aplikasi berbasis Java dalam container, mengupload image container ke
Artifact Registry, dan membuat asal build.
Untuk menjalankan perintah gcloud di halaman ini, instal
Google Cloud CLI.
Menggunakan gambar maven atau gradle
Anda dapat mengonfigurasi Cloud Build untuk mem-build aplikasi Java menggunakan image maven atau image gradle dari Docker Hub.
maven
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:
Untuk menjalankan tugas dalam image gradle, tambahkan langkah ke konfigurasi build dengan kolom berikut:
name: Tetapkan nilai kolom ini ke gradle atau gradle:<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 gradle untuk memanggil
gradle sebagai titik entri langkah build dan menjalankan perintah gradle.
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 gradle
yang diberi tag sebagai 5.6.2-jdk8 dan mencetak versi alat build:
Di direktori utama project, buat file konfigurasi build bernama
cloudbuild.yaml.
Menjalankan pengujian: maven dan gradle menyediakan maven test dan gradle 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 direferensikan oleh kolom name.
Dalam file konfigurasi build, tambahkan test ke kolom args untuk memanggil test
dalam maven dan gradle:
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/.
Untuk memaketkan aplikasi ke dalam file JAR untuk image gradle,
tentukan perintah assemble di kolom args. Perintah assemble
mem-build file JAR di workspace/build/libs.
Membuat container aplikasi: Cloud Build menyediakan
image Docker bawaan
yang dapat Anda gunakan untuk membuat container aplikasi Java. Untuk membuat penampung
aplikasi Java, dalam file konfigurasi build:
Tambahkan kolom name dan tentukan image Docker bawaan di
gcr.io/cloud-builders/docker.
Tambahkan kolom args dan tentukan argumen build, termasuk nama
image container yang akan di-build, dan jalur ke artefak build Anda.
Tambahkan kolom images untuk mengirim image container yang telah di-build ke Artifact Registry.
Langkah build berikut akan menempatkan aplikasi Anda ke dalam container, mengirim image
container ke Artifact Registry, dan membuat informasi asal build:
Jika Anda tidak menentukan config-file-path dan source-directory
dalam perintah gcloud builds submit, Cloud Build akan mengasumsikan bahwa
file konfigurasi dan kode sumber berada di direktori kerja saat ini.
Berikut beberapa contoh repositori yang dapat Anda gunakan untuk mem-build aplikasi Java, yang masing-masing
berisi aplikasi contoh dan file konfigurasi build untuk mem-build dan menguji
aplikasi tersebut:
maven-example: Aplikasi Java dan contoh file konfigurasi build untuk mem-build dan menguji
aplikasi dengan mvn.
gradle-example: Aplikasi Java dan contoh file konfigurasi build untuk mem-build dan menguji
aplikasi dengan gradle.
[[["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\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)."]]