Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
En esta página, se explica cómo usar Cloud Build para compilar y probar aplicaciones basadas en Java, almacenar artefactos compilados en un repositorio de Maven en Artifact Registry y generar información de procedencia de la compilación.
Antes de comenzar
Familiarízate con la creación de aplicaciones basadas en Java.
Para ejecutar los comandos de gcloud de esta página, instala Google Cloud CLI.
Usa la imagen maven
Puedes configurar Cloud Build para compilar aplicaciones de Java con la imagen de maven de Docker Hub.
Para ejecutar tus tareas en la imagen maven, agrega un paso a la configuración de compilación con los siguientes campos:
name: Configura el valor de este campo como maven o maven:<tag>, donde la etiqueta representa la versión. Si no especificas la etiqueta de imagen, Cloud Build usa la imagen latest de forma predeterminada.
entrypoint: Si configuras este campo, se anula el punto de entrada predeterminado de la imagen a la que se hace referencia en name. Establece el valor de este campo como mvn para invocar a mvn como el punto de entrada del paso de compilación y ejecuta los comandos de mvn.
args: En el campo args de un paso de compilación, se toma una lista de argumentos y se los pasa a la imagen a la que se hace referencia en el campo name.
En el siguiente paso de compilación, se especifica el entrypoint para la imagen maven etiquetada como 3.3-jdk-8 y se imprime la versión de la herramienta de compilación:
En el directorio raíz del proyecto, crea un archivo de configuración de compilación llamado cloudbuild.yaml.
Ejecuta pruebas: maven proporciona maven test, que descarga dependencias, compila las aplicaciones y ejecuta las pruebas especificadas en el código fuente. El campo args de un paso de compilación toma una lista de argumentos y los pasa a la imagen a la que se hace referencia en el campo name.
En el archivo de configuración de compilación, agrega test al campo args para invocar test dentro de maven:
Empaqueta la aplicación: A fin de empaquetar la aplicación en un archivo JAR para la imagen de maven, especifica el comando package en el campo args.
El comando package compila un archivo JAR en /workspace/target/.
En el siguiente paso de compilación, se empaqueta la aplicación de Java:
En tu archivo de configuración de compilación, usa el campo mavenArtifacts para especificar la ruta de acceso de la aplicación y el repositorio de Maven en Artifact Registry:
location: Es la ubicación de tu repositorio en Artifact Registry.
project-id: Es el ID del Google Cloud proyecto que contiene tu repositorio de Artifact Registry.
repository-name: Es el nombre de tu repositorio de Maven en Artifact Registry.
app-path: Es la ruta de acceso a tu aplicación empaquetada.
build-artifact: Es el nombre de tu archivo de paquete creado a partir del paso de compilación.
group-id: Identifica de forma única tu proyecto en todos los proyectos de Maven, en el formato com.mycompany.app. Para obtener más información, consulta la guía de Maven sobre convenciones de nombres.
version: Es el número de versión de tu aplicación, con formato de números y puntos, como 1.0.1.
[[["Fácil de comprender","easyToUnderstand","thumb-up"],["Resolvió mi problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Información o código de muestra incorrectos","incorrectInformationOrSampleCode","thumb-down"],["Faltan la información o los ejemplos que necesito","missingTheInformationSamplesINeed","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Otro","otherDown","thumb-down"]],["Última actualización: 2025-08-18 (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)."]]