Skip to content

Commit 9bbadae

Browse files
authored
Java 8 app migrated to Java 11 (#1435)
* [WIP] Java 8 app migrated to Java 11 * Change to servlet * update comment * Add region tags and ReadMe * Update region tags and ReadMe of jetty main * fix styling * Fix ReadMe link * Add App Engine staging * Respond to PR comments * Update dependencies * fix dep
1 parent 5e9bf25 commit 9bbadae

File tree

21 files changed

+491
-118
lines changed

21 files changed

+491
-118
lines changed

appengine-java11/README.md

+24-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
This is a repository that contains Java code samples for [Google App Engine][ae-docs]
77
standard Java 11 environment.
88

9-
[ae-docs]: https://cloud.google.com/appengine/docs/java/
9+
[ae-docs]: https://cloud.google.com/appengine/docs/standard/java11/
1010

1111
## Prerequisites
1212

@@ -46,11 +46,31 @@ To switch to an Open JDK 11 in a Cloud shell session, you can use:
4646
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
4747
```
4848

49-
### appengine-simple-jetty-main
49+
## Java 11 runtime
5050

51-
[appengine-simple-jetty-main](appengine-simple-jetty-main) is a shared artifact
51+
### Servlet Runtime
52+
53+
To migrate to the Java 11 runtime, your application must have a
54+
`Main` class that starts a web server.
55+
[`appengine-simple-jetty-main`](appengine-simple-jetty-main) is a shared artifact
5256
that provides a Jetty Web Server for the servlet based runtime. Packaged as a
5357
jar, the Main Class will load a war file, passed as an argument, as the
5458
context root of the web application listening to port 8080.
5559
Some samples create a `<sample-name>.war` which is used as an argument in the
56-
App Engine app.yaml entrypoint field.
60+
App Engine `app.yaml` entrypoint field.
61+
62+
### App Engine Staging Directory
63+
64+
The App Engine Plugin will stage all the files to upload into App Engine
65+
runtime in `${build.directory}/appengine-staging`. When deploying an
66+
[Uber JAR][uber-jar], the JAR is automatically copied into this staging
67+
directory and uploaded. It's possible to copy other files into this staging
68+
directory (such as additional JVM Agents) and having them available in the
69+
deployed App Engine runtime directory.
70+
71+
- To stage the files to be uploaded:
72+
```
73+
mvn appengine:stage
74+
```
75+
76+
[uber-jar]: https://stackoverflow.com/questions/11947037/what-is-an-uber-jar
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,76 @@
1-
# Embedded Jetty Server for running WAR files on Google App Engine Standard with Java 11
1+
# Embedded Jetty Server for Google App Engine Standard with Java 11
22

3-
This sample demonstrates an embedded Jetty server which instantiates an HTTP server to run a WAR file.
3+
To migrate to the Java 11 runtime, your application must have a
4+
`Main` class that starts a web server. This sample is a shared artifact that
5+
provides a `Main` class to instantiate an HTTP server to run an embedded web
6+
application `WAR` file.
47

5-
For more information on the Java 11 runtime, see [Migrating your App Engine app from Java 8 to Java 11](https://cloud.google.com/appengine/docs/standard/java11/java-differences).
8+
For more information on the Java 11 runtime, see
9+
[Migrating your App Engine app from Java 8 to Java 11](https://cloud.google.com/appengine/docs/standard/java11/java-differences).
10+
11+
## Install the dependency
12+
This sample is used as a dependency and must be install locally:
13+
```
14+
mvn install
15+
```
16+
17+
## Using the dependency
18+
See [`helloworld-servlet`](../helloworld-servlet) to see a complete example.
19+
20+
Your project's `pom.xml` needs to be updated accordingly:
21+
22+
- Add the `appengine-simple-jetty-main` dependency:
23+
```
24+
<dependency>
25+
<groupId>com.example.appengine.demo</groupId>
26+
<artifactId>simple-jetty-main</artifactId>
27+
<version>1</version>
28+
<scope>provided</scope>
29+
</dependency>
30+
```
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+
To use the dependency add the entrypoint to your `app.yaml` file. The
58+
entrypoint field will start the Jetty server and load your `WAR` file.
59+
```
60+
runtime: java11
61+
instance_class: F2
62+
entrypoint: 'java -cp * com.example.appengine.demo.jettymain.Main helloworld.war'
63+
```
64+
65+
## Running locally
66+
The [Exec Maven Plugin][exec-plugin] has been added so you can run your
67+
application locally. It is possible to use the [Jetty Maven Plugin][jetty-plugin]
68+
for rapid development and testing, but using the Exec Maven Plugin will ensure
69+
the provided server is running your application as expected.
70+
71+
- Start the server with your `WAR` file as an argument:
72+
```
73+
mvn exec:java -Dexec.args="<path/to/your/war/file"
74+
```
75+
76+
[jetty-plugin]: https://www.eclipse.org/jetty/documentation/9.4.x/jetty-maven-plugin.html

appengine-java11/appengine-simple-jetty-main/pom.xml

+28-5
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,57 @@
2323
<maven.compiler.target>11</maven.compiler.target>
2424
</properties>
2525

26+
<!-- [START gae_java11_server_dependencies] -->
2627
<dependencies>
28+
<!-- Embedded Jetty dependencies -->
2729
<dependency>
2830
<groupId>org.eclipse.jetty</groupId>
2931
<artifactId>jetty-server</artifactId>
30-
<version>9.4.14.v20181114</version>
32+
<version>9.4.18.v20190429</version>
3133
</dependency>
3234
<dependency>
3335
<groupId>org.eclipse.jetty</groupId>
3436
<artifactId>jetty-webapp</artifactId>
35-
<version>9.4.14.v20181114</version>
37+
<version>9.4.18.v20190429</version>
3638
<type>jar</type>
3739
</dependency>
3840
<dependency>
3941
<groupId>org.eclipse.jetty</groupId>
4042
<artifactId>jetty-util</artifactId>
41-
<version>9.4.14.v20181114</version>
43+
<version>9.4.18.v20190429</version>
4244
</dependency>
4345
<dependency>
4446
<groupId>org.eclipse.jetty</groupId>
4547
<artifactId>jetty-annotations</artifactId>
46-
<version>9.4.14.v20181114</version>
48+
<version>9.4.18.v20190429</version>
4749
<type>jar</type>
4850
</dependency>
4951
<!-- extra explicit dependency needed because there is a JSP in the sample-->
5052
<dependency>
5153
<groupId>org.eclipse.jetty</groupId>
5254
<artifactId>apache-jsp</artifactId>
53-
<version>9.4.14.v20181114</version>
55+
<version>9.4.18.v20190429</version>
5456
</dependency>
5557
</dependencies>
58+
<!-- [END gae_java11_server_dependencies] -->
59+
60+
<build>
61+
<plugins>
62+
<!-- Exec Maven Plugin provides goals to help execute Main class locally -->
63+
<!-- [START gae_java11_exec_plugin] -->
64+
<plugin>
65+
<groupId>org.codehaus.mojo</groupId>
66+
<artifactId>exec-maven-plugin</artifactId>
67+
<version>1.6.0</version>
68+
<executions>
69+
<execution><goals><goal>java</goal></goals></execution>
70+
</executions>
71+
<configuration>
72+
<mainClass>com.example.appengine.demo.jettymain.Main</mainClass>
73+
</configuration>
74+
</plugin>
75+
<!-- [END gae_java11_exec_plugin] -->
76+
77+
</plugins>
78+
</build>
5679
</project>

appengine-java11/cloudsql/pom.xml

+3-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@
4242
Dependent-jars are created and added to
4343
${project.build.directory}/appengine-staging -->
4444
<dependencies>
45-
<!-- Dependency from folder `../appengine-simple-jetty-main` -->
46-
<!-- See README for instructions for more information -->
45+
<!-- Dependency needs to be locally install from directory -->
46+
<!-- `java-docs-samples/appengine-java11/appengine-simple-jetty-main' -->
47+
<!-- See the README for more information -->
4748
<dependency>
4849
<groupId>com.example.appengine.demo</groupId>
4950
<artifactId>simple-jetty-main</artifactId>

appengine-java11/custom-entrypoint/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ To view your app, use command:
1717
```
1818
gcloud app browse
1919
```
20-
Or navigate to http://<project-id>.appspot.com URL.
20+
Or navigate to `https://<your-project-id>.appspot.com`.

appengine-java11/gaeinfo/pom.xml

+3-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ Copyright 2019 Google LLC
3939
Dependent-jars are created and added to
4040
${project.build.directory}/appengine-staging -->
4141
<dependencies>
42-
<!-- Dependency from folder `../appengine-simple-jetty-main` -->
43-
<!-- See README for instructions for more information -->
42+
<!-- Dependency needs to be locally install from directory -->
43+
<!-- `java-docs-samples/appengine-java11/appengine-simple-jetty-main' -->
44+
<!-- See the README for more information -->
4445
<dependency>
4546
<groupId>com.example.appengine.demo</groupId>
4647
<artifactId>simple-jetty-main</artifactId>

appengine-java11/guestbook-cloud-firestore/pom.xml

+3-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@
3838
Dependent-jars are created and added to
3939
${project.build.directory}/appengine-staging -->
4040
<dependencies>
41-
<!-- Dependency from folder `../appengine-simple-jetty-main` -->
42-
<!-- See README for instructions for more information -->
41+
<!-- Dependency needs to be locally install from directory -->
42+
<!-- `java-docs-samples/appengine-java11/appengine-simple-jetty-main' -->
43+
<!-- See the README for more information -->
4344
<dependency>
4445
<groupId>com.example.appengine.demo</groupId>
4546
<artifactId>simple-jetty-main</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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

Comments
 (0)