|
| 1 | +# Hello World Servlet on Google App Engine Standard with Java 11 |
| 2 | + |
| 3 | +This sample demonstrates migrating a `WAR` packaged servlet |
| 4 | +to Java 11. To migrate to the Java 11 runtime, your application must have a |
| 5 | +`Main` class that starts a web server. This sample is dependent on artifact |
| 6 | +[`appengine-simple-jetty-main`](../appengine-simple-jetty-main) to provide a |
| 7 | +`Main` class that starts an embedded Jetty server. |
| 8 | + |
| 9 | +## Setup |
| 10 | + |
| 11 | +The `pom.xml` has been updated accordingly: |
| 12 | +- Update maven compiler to use Java version 11: |
| 13 | +``` |
| 14 | +<properties> |
| 15 | + <maven.compiler.source>11</maven.compiler.source> |
| 16 | + <maven.compiler.target>11</maven.compiler.target> |
| 17 | + <failOnMissingWebXml>false</failOnMissingWebXml> |
| 18 | +</properties> |
| 19 | +``` |
| 20 | + |
| 21 | +- Add the `appengine-simple-jetty-main` dependency: |
| 22 | +``` |
| 23 | +<dependency> |
| 24 | + <groupId>com.example.appengine.demo</groupId> |
| 25 | + <artifactId>simple-jetty-main</artifactId> |
| 26 | + <version>1</version> |
| 27 | + <scope>provided</scope> |
| 28 | +</dependency> |
| 29 | +``` |
| 30 | +**Note: this dependency needs to be installed locally.** |
| 31 | + |
| 32 | +- On deployment, the App Engine runtime uploads files located in |
| 33 | +`${build.directory}/appengine-staging`. Add the `maven-dependency-plugin` to |
| 34 | +the build in order to copy dependencies to the correct folder: |
| 35 | +``` |
| 36 | +<plugin> |
| 37 | + <groupId>org.apache.maven.plugins</groupId> |
| 38 | + <artifactId>maven-dependency-plugin</artifactId> |
| 39 | + <version>3.1.1</version> |
| 40 | + <executions> |
| 41 | + <execution> |
| 42 | + <id>copy</id> |
| 43 | + <phase>prepare-package</phase> |
| 44 | + <goals> |
| 45 | + <goal>copy-dependencies</goal> |
| 46 | + </goals> |
| 47 | + <configuration> |
| 48 | + <outputDirectory> |
| 49 | + ${project.build.directory}/appengine-staging |
| 50 | + </outputDirectory> |
| 51 | + </configuration> |
| 52 | + </execution> |
| 53 | + </executions> |
| 54 | +</plugin> |
| 55 | +``` |
| 56 | + |
| 57 | +The `appengine-web.xml` file has been removed and an |
| 58 | +[`app.yaml`](src/main/appengine/app.yaml) has been added to manage your |
| 59 | +application settings: |
| 60 | +- The entrypoint field will start the Jetty server and load your `WAR` file. |
| 61 | +``` |
| 62 | +runtime: java11 |
| 63 | +instance_class: F2 |
| 64 | +entrypoint: 'java -cp * com.example.appengine.demo.jettymain.Main helloworld.war' |
| 65 | +``` |
| 66 | + |
| 67 | +## Running locally |
| 68 | +The `exec-maven-plugin` has been added to `appengine-simple-jetty-main` so you |
| 69 | +can run your application locally. |
| 70 | + |
| 71 | +- Package your app: |
| 72 | +``` |
| 73 | +mvn clean package |
| 74 | +``` |
| 75 | + |
| 76 | +- Move into the directory: |
| 77 | +``` |
| 78 | +cd ../appengine-simple-jetty-main |
| 79 | +``` |
| 80 | + |
| 81 | +- Install the dependency: |
| 82 | +``` |
| 83 | +mvn install |
| 84 | +``` |
| 85 | + |
| 86 | +- Start the server with your `WAR` file as an argument: |
| 87 | +``` |
| 88 | +mvn exec:java -Dexec.args="../helloworld-java8/target/helloworld.war" |
| 89 | +``` |
| 90 | + |
| 91 | +Then visit: http://localhost:8080/hello |
| 92 | + |
| 93 | +## Deploying |
| 94 | +While in the `helloworld-servlet` directory, use the `appengine-maven-plugin` to |
| 95 | +deploy your app: |
| 96 | +``` |
| 97 | +mvn clean package appengine:deploy -Dapp.deploy.projectId=<your-project-id> |
| 98 | +``` |
| 99 | +Then visit: https://YOUR-PROJECT-ID.appspot.com/hello |
0 commit comments