Deploying Helidon Applications to Google App Engine
Google App Engine is a managed server platform for running applications. It supports a variety of runtimes and tooling including support for Java 11 applications. That means it supports Helidon applications.
If you haven’t used App Engine before then you should run through the App Engine Java 11 Quickstart. You can run through it all the way, or stop when you reach “Download the Hello World app” and return back here.
In this blog post we’re going to deploy the Helidon MP Quickstart example to Google App Engine. To do this we only need to make one modification to the example and add some App Engine metadata. Specifically:
- Modify the application so that it responds to the PORT environment variable.
- Create an app.yaml file for deployment meta-data
- Create a .gcloudignore file to control what files are uploaded
But before we do that, let’s talk about application packaging.
Application Packaging
By default Helidon applications are packaged as thin jars (Helidon also supports jlink images and native images, but those are outside the scope of this article). With a thin jar your full application and runtime consists of:
- The application jar (helidon-quickstart-mp.jar). This jar contains your application code, plus important information in
META-INF/MANIFEST.MF
:Main-Class
to define your application’s main andClass-Path
to identify your application’s runtime dependencies. - Your runtime dependencies in the
libs
directory (referenced byClass-Path).
- The Java 11 runtime itself. This will be provided by Google App Engine.
This packaging is friendly to container based deployment platforms like Docker and Google App Engine, because the file that changes the most — the application jar — is relatively small and can be uploaded independently of its runtime dependencies that rarely change.
Google App Engine supports thin jar packaging as described in App Engine’s Java 11 Runtime Environment.
Create Your Application
First we use the Helidon MP maven archetype to create the sample application:
mvn archetype:generate -DinteractiveMode=false \
-DarchetypeGroupId=io.helidon.archetypes \
-DarchetypeArtifactId=helidon-quickstart-mp \
-DarchetypeVersion=1.4.4 \
-DgroupId=io.helidon.examples \
-DartifactId=helidon-quickstart-mp \
-Dpackage=io.helidon.examples.quickstart.mp
Now cd into the application directory and build it:
cd helidon-quickstart-mp
mvn package
If you wish you can exercise the application as described in the example’s README file:
curl -X GET http://localhost:8080/greet/Joe
Modify Your Application
Next we need to modify the application so that it responds to the PORT
environment variable. Edit src/main/java/io/helidon/examples/quickstart/mp/Main.java
and change the startServer()
method so that it looks like this:
static Server startServer() {
Server.Builder builder = Server.builder();
// Override port with the PORT environment variable
String envPort = System.getenv().getOrDefault("PORT", "");
if (! envPort.isEmpty()) {
builder.port(Integer.parseInt(envPort));
}
return builder.build().start();
}
Now build and run the application and verify it responds to the PORT environment variable:
mvn package
export PORT=8888
java -jar target/helidon-quickstart-mp.jar
The application should report it is running on 8888: http://localhost:8888/greet . Quit the application (^C).
Create deployment file
Next create a file called helidon-mp-app.yaml
with the following contents:
runtime: java11
entrypoint: java -Xmx64m -jar helidon-quickstart-mp.jar
Then copy the file into the target directory (note that you should make this part of your build, but for now we’ll just copy it manually):
cp helidon-mp-app.yaml target/
Create .gcloudignore file
The gcloudignore file controls what files are uploaded to Google App Engine by the deploy command. So create a file .gcloudignore
that contains:
# Exclude everything. Then include just the app jar and runtime
# dependencies in libs/
*
*/
*/**
!helidon-quickstart-mp.jar
!libs/
!libs/**
And copy it to the target directory (again, this should be done as part of your build):
cp .gcloudignore target/.gcloudignore
Note the comment in this file. Since we only want the application jar and the libs directory uploaded, the file says to ignore everything except the jar file, libs and everything in libs.
Deploy
Finally we deploy our application to Google App Engine:
gcloud app deploy target/helidon-mp-app.yaml
This will upload the files and deploy your application. Note that if you later change your application code only the application jar is uploaded.
Access It
You can view the application in your browser by running:
gcloud app browse
It will initial say something like “No handler found for path: /”. That’s OK. Append /greet
to the URL in the browser and you should get a JSON response.
Cleanup
Make sure to cleanup your application as described in the Google App Engine Quickstart].
Completed Example
You can find this completed example, as well as one for Helidon SE here: https://github.com/barchetta/helidon-google-app-engine-example
Those examples also maintain the app.yaml and .gcloudignore files as source and use the Maven resources plugin to copy them into the target directory as part of the build.