Using Apache Maven and the App Engine Plugin

Apache Maven is a build automation tool for Java, which builds JAR files for deployment into App Engine.

For faster deployments, use the following Maven tools:

Setting up Maven

To use Maven:

  1. Determine whether Maven is installed and which version you have by running the following command:

    mvn -v

    If you have Maven installed, a long string of information beginning with Apache Maven and followed by a version number such as 3.5 will display.

  2. If you don't have Maven 3.5 or newer installed:

    1. Download Maven 3.5 or newer.
    2. Install Maven 3.5 or newer.

Setting up and validating your Cloud project

You need to set up your Cloud project and install Cloud SDK.

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

    Go to App Engine

    1. Select or create a new Cloud 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. Follow the prompts 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 Cloud project ID other than the one that you initialized it to, see Managing Cloud SDK Configurations.

  3. Install the Cloud SDK app-engine-java component:

    gcloud components install app-engine-java

Adding the App Engine Maven and Eclipse Jetty Maven plugins

To develop and deploy your application, use the App Engine Maven plugin.

To use the plugin in an existing Maven project, add the following into the plugins section in the project pom.xml file:


Compile and build your project using Maven

To build a Java App Engine app using Maven with its pom.xml file:

  1. Change to the main directory for your project.

    For example, if you downloaded the sample Hello World app, change into the following directory:

  2. Invoke Maven:

    mvn package
  3. Wait for the project to build. When the project successfully finishes you will see a message similar to this one:

     Total time: 10.724s
     Finished at: 2016-07-29T12:13:36-07:00
     Final Memory: 24M/213M

Deploying your app

After you add the App Engine Maven plugin to your project's pom.xml file, you can deploy your application:

mvn package appengine:deploy -Dapp.deploy.projectId=PROJECT_ID

Replace PROJECT_ID with the ID of your Cloud project. If your pom.xml file already specifies your project ID, you don't need to include the -Dapp.deploy.projectId property in the command you run.

The appengine:deploy goal and all other goals in the App Engine Maven plugin have associated parameters that you can use. For a complete list, see App Engine Maven Plugin Goals and Parameters.

What happens when you deploy

By default, the appengine:deploy goal does the following:

  1. Deploys a new version of your service to the Google Cloud project you configured the gcloud tool to use.

    Because each new version requires a unique ID, the appengine:deploy goal also generates a unique version ID.

  2. Routes all traffic to the new version.

Changing the deployment defaults

You can change the default deployment behavior by passing parameters in the appengine:deploy command. For example, the following command deploys the service defined in your pom.xml file to a specific Google Cloud project, assigns a custom version ID to the service, and turns off automatic traffic routing for the new version:

mvn appengine:deploy -Dapp.deploy.projectId=PROJECT_ID -Dapp.deploy.version=VERSION_ID -Dapp.deploy.promote=False

You can also use the appengine:deploy command to deploy configuration files, such as cron.yaml, dispatch.yaml, and index.yaml.

For more information, see the appengine:deploy reference.

What's next