Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Cette page explique comment utiliser Cloud Build pour compiler et tester des applications Java, stocker des artefacts créés dans un dépôt Maven dans Artifact Registry et générer des informations sur la provenance de la compilation.
Avant de commencer
Familiarisez-vous avec la création d'applications Java.
disposer d'un dépôt Maven dans Artifact Registry ; Si vous n'en avez pas, créez un dépôt.
Pour exécuter les commandes gcloud sur cette page, installez Google Cloud CLI.
Utiliser l'image maven
Vous pouvez configurer Cloud Build pour qu'il compile les applications Java à l'aide de l'image maven de Docker Hub.
Pour exécuter vos tâches dans l'image maven, ajoutez une étape à votre configuration de compilation avec les champs suivants:
name: définissez la valeur de ce champ sur maven ou maven:<tag>, où le tag représente la version. Si vous ne spécifiez pas le tag d'image, Cloud Build utilise par défaut l'image latest.
entrypoint: la définition de ce champ remplace le point d'entrée par défaut de l'image référencée dans name. Définissez la valeur de ce champ sur mvn pour appeler mvn en tant que point d'entrée de l'étape de compilation et exécuter les commandes mvn.
args : Le champ args d'une étape de compilation prend une liste d'arguments et la transmet à l'image à laquelle le champ name fait référence.
L'étape de compilation suivante spécifie le entrypoint de l'image maven taguée comme 3.3-jdk-8 et affiche la version de l'outil de compilation:
Dans le répertoire racine de votre projet, créez un fichier de configuration de compilation nommé cloudbuild.yaml.
Exécuter des tests: maven fournit maven test, qui télécharge les dépendances, compile les applications et exécute tous les tests spécifiés dans votre code source. Le champ args d'une étape de compilation prend une liste d'arguments et la transmet à l'image à laquelle le champ name fait référence.
Dans votre fichier de configuration de compilation, ajoutez test au champ args pour appeler test dans maven:
Empaqueter l'application : pour empaqueter votre application dans un fichier JAR pour votre image maven, spécifiez la commande package dans le champ args.
La commande package génère un fichier JAR dans /workspace/target/.
L'étape de compilation suivante permet d'empaqueter votre application Java :
Dans votre fichier de configuration de compilation, utilisez le champ mavenArtifacts pour spécifier le chemin d'accès à votre application et votre dépôt Maven dans Artifact Registry:
version: numéro de version de votre application, mis en forme avec des chiffres et des points, comme 1.0.1.
Facultatif: Activer la génération de provenance
Cloud Build peut générer des métadonnées de provenance de compilation SLSA (Supply chain Levels for Software Artifacts) vérifiables pour vous aider à sécuriser votre pipeline d'intégration continue.
Pour activer la génération de provenance, ajoutez requestedVerifyOption: VERIFIED à la section options de votre fichier de configuration.
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/04 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 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)."]]