Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
Gradle es una herramienta de automatización de compilación de código abierto centrada en la flexibilidad y el rendimiento.
App Engine proporciona un complemento de Gradle que puedes usar para compilar e implementar tu app en App Engine.
En la muestra de código, se muestra cómo configurar el archivo build.gradle para agregar tareas de Gradle de App Engine, repositorios de Maven, complemento de Gradle para App Engine y dependencias.
Si aún no lo hiciste, sigue los pasos de esta guía para descargar gcloud CLI, crear tu proyecto de Google Cloud e inicializar tu app de App Engine.
Instala el componente app-engine-java de la CLI de gcloud:
gcloudcomponentsinstallapp-engine-java
Crea un proyecto nuevo de Gradle
Puedes crear un proyecto de Gradle nuevo desde cero mediante la shell. O bien, si quieres probar el complemento, puedes descargar, ejecutar de forma local e implementar el proyecto de Hello World.
Para crear un proyecto nuevo, realiza los siguientes pasos:
Crea un directorio nuevo y navega hasta él.
Para inicializar un proyecto nuevo, sigue estos pasos:
Ejecuta gradle init:
gradleinit--typejava-application
Se te pedirá que respondas preguntas sobre lo siguiente:
Ingresa la versión de Java de destino (mín.: 7, predeterminada: 21): 21
Selecciona la estructura de la aplicación: 1 para el proyecto de aplicación única
Selecciona el DSL de la secuencia de comandos de compilación: 2 (para Groovy)
Selecciona el framework de prueba: 1 (para JUnit 4)
Genera compilación con APIs y comportamiento nuevos: no
Agrega el siguiente comando a tu archivo app/build.gradle para agregar las tareas y el complemento de App Engine Gradle, los repositorios de Maven, las dependencias y la configuración de las tareas.
buildscript{// Configuration for buildingrepositories{jcenter()// Bintray's repository - a fast Maven Central mirror & moremavenCentral()}dependencies{classpath'com.google.cloud.tools:appengine-gradle-plugin:2.5.0'classpath'org.akhikhl.gretty:gretty:+'}}repositories{// repositories for Jar's you access in your codejcenter()mavenCentral()}applyplugin:'java'applyplugin:'war'applyplugin:'org.akhikhl.gretty'applyplugin:'com.google.cloud.tools.appengine'dependencies{providedCompile'javax.servlet:javax.servlet-api:3.1.0'providedCompile'com.google.appengine:appengine:+'// Add your dependencies here.}gretty{servletContainer='jetty9'// What App Engine Flexible uses}appengine{deploy{// deploy configurationstopPreviousVersion=true// default - stop the current versionpromote=true// default - & make this the current version}}group='com.example.appengine'// Generated output GroupIdversion='1.0-SNAPSHOT'// Version in generated outputsourceCompatibility=1.8targetCompatibility=1.8
También debes agregar los siguientes archivos al proyecto, mediante un editor de texto o un entorno de desarrollo integrado (IDE):
Prueba tu aplicación con el servidor de desarrollo
Si deseas acceder a los recursos de Google desde tu proyecto cuando realizas una ejecución local, configura las credenciales predeterminadas de la aplicación mediante el siguiente comando:
gcloudauthapplication-defaultlogin
Cambia a la raíz del directorio de la aplicación.
Durante la fase de desarrollo, puedes invocar Gradle para ejecutar y probar la aplicación en cualquier momento en el servidor de desarrollo:
gradlejettyRun
También tienes la opción de ejecutar Gradle sin instalarlo si usas el wrapper de Gradle.
Espera a que se inicie el servidor. El servidor se inicia mientras la aplicación está en ejecución, y ves un mensaje como el siguiente:
: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/
La tarea appengineDeploy, al igual que las otras tareas de Gradle, tienen propiedades asociadas que puedes usar. Si deseas obtener una lista completa de tareas y propiedades, consulta Tareas y propiedades de Gradle para App Engine.
Usa el wrapper de Gradle
Gradle proporciona un mecanismo para descargar y ejecutar la versión necesaria de Gradle sin tener que instalarla:
[[["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-09-04 (UTC)"],[[["\u003cp\u003eGradle, an open-source build automation tool, is used with an App Engine plugin to build and deploy apps to App Engine.\u003c/p\u003e\n"],["\u003cp\u003eSetting up a new Gradle project involves initializing the project, creating necessary folders (\u003ccode\u003eWEB-INF\u003c/code\u003e, \u003ccode\u003eappengine\u003c/code\u003e), and modifying the \u003ccode\u003ebuild.gradle\u003c/code\u003e file to include App Engine configurations, repositories, and dependencies.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003ebuild.gradle\u003c/code\u003e file configures settings like build repositories, plugin dependencies, Java version compatibility, Gretty plugin details, and App Engine deployment settings.\u003c/p\u003e\n"],["\u003cp\u003eLocal testing of the application can be done by running \u003ccode\u003egradle jettyRun\u003c/code\u003e in the project's root directory, and the app can then be viewed at \u003ccode\u003ehttp://localhost:8080\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eDeploying the application to App Engine involves running the command \u003ccode\u003egradle appengineDeploy\u003c/code\u003e, which utilizes the configured settings within the \u003ccode\u003ebuild.gradle\u003c/code\u003e file.\u003c/p\u003e\n"]]],[],null,["# Using Gradle and the App Engine Plugin\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\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\nThe code sample demonstrates how to configure the `build.gradle` file to add App Engine\nGradle tasks, Maven repositories, App Engine Gradle plugin, and dependencies.\n\n| **Note:** For information about other ways to build and deploy your app, see [Testing and deploying your app](/appengine/docs/flexible/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/flexible/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 buildscript { // Configuration for building\n repositories {\n jcenter() // Bintray's repository - a fast Maven Central mirror & more\n mavenCentral()\n }\n dependencies {\n classpath 'com.google.cloud.tools:appengine-gradle-plugin:2.5.0'\n classpath 'org.akhikhl.gretty:gretty:+'\n }\n }\n\n repositories { // repositories for Jar's you access in your code\n jcenter()\n mavenCentral()\n }\n\n apply plugin: 'java'\n apply plugin: 'war'\n apply plugin: 'org.akhikhl.gretty'\n apply plugin: 'com.google.cloud.tools.appengine'\n\n dependencies {\n providedCompile 'javax.servlet:javax.servlet-api:3.1.0'\n providedCompile 'com.google.appengine:appengine:+'\n // Add your dependencies here.\n\n }\n\n gretty {\n servletContainer = 'jetty9' // What App Engine Flexible uses\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 }\n }\n\n group = 'com.example.appengine' // Generated output GroupId\n version = '1.0-SNAPSHOT' // Version in generated output\n\n sourceCompatibility = 1.8\n targetCompatibility = 1.8\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/flexible/java/configuring-your-app-with-app-yaml)\n - [`app/src/main/webapp/WEB-INF/web.xml`](/appengine/docs/flexible/java/configuring-the-web-xml-deployment-descriptor) (unless your app uses [Servlet 3.1 annotations](https://www.javadoc.io/static/javax.servlet/javax.servlet-api/3.1.0/javax/servlet/annotation/package-summary.html))\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/flexible/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/flexible/java/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/flexible/java/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/flexible/java/gradle-reference)."]]