Using Gradle and the App Engine Plugin

You can use Gradle, a flexible build automation system that is declarative and is used to automate build, testing, publishing, and deployment. This page explains how to use Gradle with an App Engine project in the standard environment.

Before you begin

Installing the App Engine Java SDK

To use App Engine Gradle tasks, you must install the App Engine Java SDK:

gcloud components install app-engine-java

Configuring Google Cloud SDK

Initialize gcloud and log in:

gcloud init

If you've already initialized gcloud:

  • To authorize the user account without performing setup steps, run gcloud auth login.

  • To access Google resources from your project when running locally, set the application default credentials by running gcloud beta auth application-default login.

Creating a new 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.

  1. Create and enter a new directory.

  2. To initialize a new project:

    gradle init --type java-library;
    mkdir -p src/main/webapp/WEB-INF;
    rm src/main/java/Libary.java src/test/java/LibraryTest.java
    
  3. 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
        mavenCentral()
      }
      dependencies {
        classpath 'com.google.cloud.tools:appengine-gradle-plugin:+'    // latest App Engine Gradle tasks
      }
    }
    
    repositories {   // repositories for Jar's you access in your code
      maven {
        url 'https://maven-central.storage.googleapis.com'             // Google's mirror of Maven Central
    //   url 'https://oss.sonatype.org/content/repositories/snapshots' // SNAPSHOT Repository (if needed)
      }
      jcenter()
      mavenCentral()
    }
    
    apply plugin: 'java'                              // standard Java tasks
    apply plugin: 'war'                               // standard Web Archive plugin
    apply plugin: 'com.google.cloud.tools.appengine'  // App Engine tasks
    
    dependencies {
      providedCompile group: 'javax.servlet', name: 'servlet-api', version:'2.5'
      compile 'com.google.appengine:appengine:+'
    // Add your dependencies here.
    
    }
    
    appengine {  // App Engine tasks configuration
      run {      // local (dev_appserver) configuration (standard environments only)
        port = 8080                 // default
      }
    
      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.7  // App Engine Standard uses Java 7
    targetCompatibility = 1.7  // App Engine Standard uses Java 7

  4. 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 https://github.com/GoogleCloudPlatform/java-docs-samples.git
    

    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. Change to the root of your application directory.

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

    gradle appengineRun
    

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

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

    :compileJava
    :processResources UP-TO-DATE
    :classes
    :war
    :explodeWar
    :assemble
    :appengineRun
    INFO     2016-08-16 19:44:52,131 devappserver2.py:769] Skipping SDK update check.
    INFO     2016-08-16 19:44:52,174 api_server.py:205] Starting API server at: http://localhost:62837
    INFO     2016-08-16 19:44:52,176 dispatcher.py:197] Starting module "default" running at: http://127.0.0.1:8080
    INFO     2016-08-16 19:44:52,178 admin_server.py:116] Starting admin server at: http://localhost:8000
    

  4. See your app running at http://127.0.0.1:8080.

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:

Mac OS X and Linux

./gradlew appengineRun

Windows

gradlew.bat appengineRun

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

What's next