Using Gradle and the App Engine Plugin

Learn how to manage an App Engine flexible environment project using Gradle, a declarative build system used to automate build, testing, and deployment.

Before you begin

Install and configure the following prerequisites:

Setting up and validating your GCP project

You need to set up your GCP project and install the Cloud SDK:

  1. Use the Google Cloud Platform Console to create and set up your GCP project:

    Go to App Engine

    1. Select or create a new GCP project.
    2. Follow the prompts to ensure that an App Engine application exists and billing is enabled:
      1. If you need to create an App Engine application for your project, you are prompted to select the region where you want your App Engine application located.
      2. If you need to enable billing for your project, you are prompted to create a new billing account or select an existing account.
    3. The Dashboard opens after your App Engine application has been created and billing has been enabled in your project.
  2. Install and initialize the Cloud SDK.

    If you already have the gcloud tool installed and want to configure it to use a GCP project ID other than the one that you initialized it to, see Managing Cloud SDK Configurations.

  3. Install the App Engine component for Java:

    gcloud components install app-engine-java

Creating a new Gradle project

You can create a new Gradle project from scratch using the shell. Alternatively, to try out the plugin, you can download, run locally, and deploy the hello world project.

To create a new project:

  1. Create a new directory and navigate to it.

  2. To initialize a new project:

    1. Run gradle init:

      gradle init --type java-library
    2. Create the WEB-INF folder:

      mkdir -p src/main/webapp/WEB-INF
    3. Create the appengine folder:

      mkdir -p src/main/appengine
  3. Remove the stub files generated by gradle init:

    rm src/main/java/ src/test/java/
  4. Add the following to your build.gradle file to add App Engine Gradle tasks, Maven repositories, the App Engine Gradle plugin, dependencies, and task configuration:

    buildscript {      // Configuration for building
      repositories {
        jcenter()      // Bintray's repository - a fast Maven Central mirror & more
      dependencies {
        classpath '' // Latest 1.x.x release
        classpath 'org.akhikhl.gretty:gretty:+'
    repositories {   // repositories for JARs you access in your code
      maven {
        url ''             // Google's mirror of Maven Central
    //maven {
    //  url '' // SNAPSHOT repository if needed
    apply plugin: 'java'
    apply plugin: 'war'
    apply plugin: 'org.akhikhl.gretty'
    apply plugin: ''
    dependencies {
      providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
      providedCompile ''
    // Add your dependencies here.
    gretty {
        httpPort = 8080
        contextPath = '/'
        servletContainer = 'jetty9'  // What App Engine Flexible uses
    appengine {
      deploy {   // deploy configuration
        stopPreviousVersion = true  // default - stop the current version
        promote = true              // default - & make this the current version
    group = 'com.example.appengine'   // Generated output GroupId
    version = '1.0-SNAPSHOT'          // Version in generated output
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
  5. You also need to add the following files to your project, using a text editor or integrated development environment (IDE):

See the quickstart for an overview of a Java App Engine project.

Downloading the Hello World app

  1. Clone the Hello World sample app repository to your local machine:

    git clone

    Alternatively, you can download the sample as a zip file and extract it.

  2. Change to the directory that contains the sample code:

    cd java-docs-samples/appengine/helloworld-new-plugins

Testing your application with the development server

  1. To access Google resources from your project when running locally, set the application default credentials by running:

    gcloud auth application-default login
  2. Change to the root of your application directory.

  3. During the development phase, you can run and test your application at any time in the development server by invoking Gradle:

    gradle jettyRun

    Alternatively, you can run Gradle without installing it by using the Gradle wrapper.

  4. Wait for the server to start. The server is started with your application running when you see a message similar to this:

    :processResources UP-TO-DATE
    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/
  5. See your app running at http://localhost:8080.

To learn more about the Gretty plugin, see Gretty Configuration and Gretty tasks.

Deploying your application

To deploy your application:

gradle appengineDeploy

The appengineDeploy task and all other Gradle tasks have associated properties that you can use. For a complete list of tasks and properties, refer to App Engine Gradle Tasks and Properties.

Using the Gradle wrapper

Gradle provides a mechanism to download and run the required version of Gradle without installation:


  1. Change to the sample code directory.

  2. Run gradlew:

    ./gradlew jettyRun


  1. Change to the sample code directory.

  2. Run gradlew:

    ./gradlew.bat jettyRun

Additional information on Gradle can be found in App Engine Gradle Tasks and Properties.

What's next

¿Te ha resultado útil esta página? Enviar comentarios:

Enviar comentarios sobre...

App Engine flexible environment for Java docs