Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Gradle est un outil Open Source d'automatisation de la compilation axé sur la flexibilité et les performances.
App Engine fournit un plug-in Gradle que vous pouvez utiliser pour créer et déployer votre application sur App Engine.
Si vous ne l'avez pas déjà fait, suivez la procédure de ce guide pour télécharger gcloud CLI, créer votre projet Google Cloud et initialiser votre application App Engine.
Installez le composant gcloud CLI app-engine-java :
gcloudcomponentsinstallapp-engine-java
Créer un projet Gradle
Vous pouvez créer entièrement un projet Gradle à l'aide de l'interface système. Pour essayer le plug-in, vous pouvez également télécharger, exécuter en local et déployer le projet hello world.
Pour créer un projet :
Créez un nouveau répertoire et accédez à celui-ci.
Pour initialiser un nouveau projet :
Exécutez gradle init :
gradleinit--typejava-application
Vous devrez répondre aux questions suivantes :
Saisissez la version Java cible (min : 7, par défaut : 21) : 21
Sélectionnez la structure de l'application : 1 pour un projet d'application unique
Sélectionnez le DSL du script de compilation : 2 (pour Groovy)
Sélectionnez le framework de test : 1 (pour JUnit 4)
Générez une compilation à l'aide de nouvelles API et de nouveaux comportements : non
Créez le dossier WEB-INF :
mkdir-papp/src/main/webapp/WEB-INF
Créez le dossier appengine :
mkdir-papp/src/main/appengine
Supprimez les fichiers de simulation générés par gradle init :
Insérez les lignes suivantes dans le fichier app/build.gradle afin d'ajouter des tâches Gradle pour App Engine, des dépôts Maven, le plug-in Gradle pour App Engine, des dépendances et la configuration des tâches :
applyplugin:'java'applyplugin:'war'buildscript{repositories{// gretty plugin is in Maven CentralmavenCentral()}dependencies{classpath'com.google.cloud.tools:appengine-gradle-plugin:2.8.1'classpath'org.gretty:gretty:4.1.5'}}applyplugin:'org.gretty'applyplugin:'com.google.cloud.tools.appengine'repositories{mavenCentral()}appengine{deploy{// deploy configurationstopPreviousVersion=true// default - stop the current versionpromote=true// default - & make this the current versionprojectId='GCLOUD_CONFIG'version='GCLOUD_CONFIG'}}sourceSets{// In Gradle 8, the default location is app/src/java, which does not match// Maven's directory structure.main.java.srcDirs=['src/main/java']main.resources.srcDirs=['src/main/resources','src/main/webapp']test.java.srcDirs=['src/test/java']}dependencies{implementation'com.google.appengine:appengine-api-1.0-sdk:2.0.30'implementation'jakarta.servlet:jakarta.servlet-api:6.1.0'// Test DependenciestestImplementation'com.google.appengine:appengine-testing:2.0.30'testImplementation'com.google.appengine:appengine-api-stubs:2.0.30'testImplementation'com.google.appengine:appengine-tools-sdk:2.0.30'testImplementation'com.google.truth:truth:1.1.5'testImplementation'junit:junit:4.13.2'testImplementation'org.mockito:mockito-core:4.11.0'}
Vous devez également ajouter les fichiers suivants à votre projet, à l'aide d'un éditeur de texte ou d'un environnement de développement intégré (IDE) :
Tester votre application avec un serveur de développement
Pour accéder aux ressources Google de votre projet lors de l'exécution en local, définissez les identifiants par défaut de l'application en exécutant la commande suivante :
gcloudauthapplication-defaultlogin
Accédez à la racine du répertoire de votre application.
À tout moment pendant la phase de développement, vous pouvez exécuter et tester votre application sur le serveur de développement en appelant Gradle, à l'aide de la commande suivante :
Attendez que le serveur démarre. Une fois le serveur démarré et votre application en cours d'exécution, un message semblable à celui-ci s'affiche :
:prepareInplaceWebAppFolder
:createInplaceWebAppFolder
:compileJava
:processResources UP-TO-DATE
:classes
:prepareInplaceWebAppClasses
:prepareInplaceWebApp
:jettyRun
17:40:05 INFO Jetty 9.2.15.v20160210 started and listening on port 8080
17:40:05 INFO runs at:
17:40:05 INFO http://localhost:8080/
Accédez à l'adresse http://localhost:8080 pour visualiser l'exécution de votre application.
Pour déployer votre application, procédez comme suit :
gradleappengineDeploy
La tâche appengineDeploy et toutes les autres tâches Gradle sont associées à des propriétés que vous pouvez utiliser. Pour obtenir la liste complète des tâches et des propriétés disponibles, consultez la page Tâches et propriétés Gradle pour App Engine.
Utiliser le wrapper Gradle
Gradle fournit un mécanisme permettant de télécharger et exécuter la version requise de Gradle sans l'installer :
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\u003eGradle is an open-source build automation tool, and App Engine offers a Gradle plugin for building and deploying apps.\u003c/p\u003e\n"],["\u003cp\u003eSetting up a new Gradle project involves initializing a project, creating necessary folders, removing stub files, and adding App Engine Gradle tasks and dependencies to the \u003ccode\u003ebuild.gradle\u003c/code\u003e file.\u003c/p\u003e\n"],["\u003cp\u003eTesting an application during development can be done using the development server by running \u003ccode\u003egradle jettyRun\u003c/code\u003e, which will make the app available at \u003ccode\u003ehttp://localhost:8080\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eDeploying the application to App Engine is accomplished by using the command \u003ccode\u003egradle appengineDeploy\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe Gradle wrapper allows you to run Gradle without a manual installation, using \u003ccode\u003e./gradlew\u003c/code\u003e on Linux/macOS or \u003ccode\u003e.\\gradlew.bat\u003c/code\u003e on Windows.\u003c/p\u003e\n"]]],[],null,["# Use Gradle and the App Engine plugin\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\n[Gradle](https://docs.gradle.org/current/userguide/userguide.html)\nis an open-source build automation tool focused on flexibility and performance.\nApp Engine provides a Gradle plugin that you can use to build and deploy\nyour app to App Engine.\n\n| **Note:** For information about other ways to build and deploy your app, see [Testing and deploying your app](/appengine/docs/standard/testing-and-deploying-your-app).\n\nBefore you begin\n----------------\n\n1. [Install Java](http://www.oracle.com/technetwork/java/javase/downloads/index.html)\n\n2. [Install Gradle](https://gradle.org/gradle-download/)\n\n3. [Install git](https://git-scm.com/)\n\n4. If you haven't already done so, follow the steps in this\n [guide](/appengine/docs/standard/setting-up-environment)\n to download the gcloud CLI, create your Google Cloud project, and\n initialize your App Engine app.\n\n5. Install the gcloud CLI `app-engine-java` component:\n\n gcloud components install app-engine-java\n\nCreate a new Gradle project\n---------------------------\n\nYou can create a new Gradle project from scratch using the shell. Alternatively,\nto try out the plugin, you can download, run locally, and deploy\n[the hello world project](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/main/appengine-java21/helloworld/README.md).\n\nTo create a new project:\n\n1. Create a new directory and navigate to it.\n\n2. To initialize a new project:\n\n 1. Run `gradle init`:\n\n gradle init --type java-application\n\n You'll be asked to answer questions:\n - Enter target Java version (min: 7, default: 21): 21\n - Select application structure: 1 for Single application project\n - Select build script DSL: 2 (for Groovy)\n - Select test framework: 1 (for JUnit 4)\n - Generate build using new APIs and behavior: no\n 2. Create the `WEB-INF` folder:\n\n mkdir -p app/src/main/webapp/WEB-INF\n\n 3. Create the `appengine` folder:\n\n mkdir -p app/src/main/appengine\n\n3. Remove the stub files generated by `gradle init`:\n\n rm ./app/src/main/java/org/example/App.java ./app/src/test/java/org/example/AppTest.java\n\n4. Add the following to your `app/build.gradle` file to add App Engine\n Gradle tasks, Maven repositories, the App Engine Gradle plugin,\n dependencies, and task configuration:\n\n\n apply plugin: 'java'\n apply plugin: 'war'\n\n buildscript {\n repositories {\n // gretty plugin is in Maven Central\n mavenCentral()\n }\n dependencies {\n classpath 'com.google.cloud.tools:appengine-gradle-plugin:2.8.1'\n classpath 'org.gretty:gretty:4.1.5'\n }\n }\n apply plugin: 'org.gretty'\n apply plugin: 'com.google.cloud.tools.appengine'\n\n repositories {\n mavenCentral()\n }\n\n appengine {\n deploy { // deploy configuration\n stopPreviousVersion = true // default - stop the current version\n promote = true // default - & make this the current version\n projectId = 'GCLOUD_CONFIG'\n version = 'GCLOUD_CONFIG'\n }\n }\n\n sourceSets {\n // In Gradle 8, the default location is app/src/java, which does not match\n // Maven's directory structure.\n main.java.srcDirs = ['src/main/java']\n main.resources.srcDirs = ['src/main/resources', 'src/main/webapp']\n test.java.srcDirs = ['src/test/java']\n }\n\n dependencies {\n implementation 'com.google.appengine:appengine-api-1.0-sdk:2.0.30'\n implementation 'jakarta.servlet:jakarta.servlet-api:6.1.0'\n\n // Test Dependencies\n testImplementation 'com.google.appengine:appengine-testing:2.0.30'\n testImplementation 'com.google.appengine:appengine-api-stubs:2.0.30'\n testImplementation 'com.google.appengine:appengine-tools-sdk:2.0.30'\n\n testImplementation 'com.google.truth:truth:1.1.5'\n testImplementation 'junit:junit:4.13.2'\n testImplementation 'org.mockito:mockito-core:4.11.0'\n }\n\n5. You also need to add the following files to your project, using a text\n editor or integrated development environment (IDE):\n\n\n - [`app/src/main/appengine/app.yaml`](/appengine/docs/standard/java-gen2/configuring-your-app-with-app-yaml)\n\n - app/src/main/docker/Dockerfile (optional for [custom runtimes](/appengine/docs/flexible/custom-runtimes/build))\n - Java classes\n - Java [testing classes](/appengine/docs/legacy/standard/java/tools/localunittesting)\n\nSee [Set up your development environment](/appengine/docs/standard/setting-up-environment) for an\noverview of a Java App Engine project.\n\nTesting your application with the development server\n----------------------------------------------------\n\n1. To access Google resources from your project when running locally,\n set the application default credentials by running:\n\n gcloud auth application-default login\n\n2. Change to the root of your application directory.\n\n3. During the development phase, you can run and test your application at any\n time in the development server by invoking Gradle:\n\n gradle jettyRun\n\n Alternatively, you can run Gradle without installing it by [using the Gradle wrapper](#using_the_gradle_wrapper).\n4. Wait for the server to start. The server is started with your\n application running when you see a message similar to this:\n\n ```\n :prepareInplaceWebAppFolder\n :createInplaceWebAppFolder\n :compileJava\n :processResources UP-TO-DATE\n :classes\n :prepareInplaceWebAppClasses\n :prepareInplaceWebApp\n :jettyRun\n 17:40:05 INFO Jetty 9.2.15.v20160210 started and listening on port 8080\n 17:40:05 INFO runs at:\n 17:40:05 INFO http://localhost:8080/\n ```\n5. See your app running at [`http://localhost:8080`](http://localhost:8080).\n\nTo learn more about the [Gretty plugin](https://github.com/akhikhl/gretty),\nsee [Gretty Configuration](http://akhikhl.github.io/gretty-doc/Gretty-configuration.html)\nand [Gretty tasks](http://akhikhl.github.io/gretty-doc/Gretty-tasks).\n\nDeploy your application\n-----------------------\n\nTo deploy your application: \n\n gradle appengineDeploy\n\nThe `appengineDeploy` task and all other Gradle tasks have associated properties\nthat you can use. For a complete list of tasks and properties, refer to\n\n[App Engine Gradle Tasks and Properties](/appengine/docs/standard/java-gen2/gradle-reference).\n\n\nUse the Gradle wrapper\n----------------------\n\nGradle provides a mechanism to download and run the required version of Gradle\nwithout installation: \n\n### Linux/macOS\n\n1. Change to the sample code directory.\n\n2. Run gradlew:\n\n ./gradlew jettyRun\n\n### Windows\n\n1. Change to the sample code directory.\n\n2. Run gradlew:\n\n ./gradlew.bat jettyRun\n\nAdditional information on Gradle can be found in\n\n[App Engine Gradle Tasks and Properties](/appengine/docs/standard/java-gen2/gradle-reference).\n\n\nWhat's next\n-----------\n\n- Explore the plugin code and report issues on [GitHub](https://github.com/GoogleCloudPlatform/app-gradle-plugin).\n- Learn how to specify properties for tasks by referring to [App Engine Gradle Tasks and Properties](/appengine/docs/standard/java-gen2/gradle-reference)."]]