Skip to content

Commit 6d0a235

Browse files
authored
feat(appengine): repackaging Java 8 application public doc update (#10064)
* Repackaging Java 8 application public doc update * header-checks added * chore(docker): add YAML markers to Dockerfile and cloudbuild.yaml for clarity Doc and code bug reference: b/409356548 and b/409357079 * chore(Dockerfile): update YAML markers for clarity and consistency * chore(Dockerfile): update YAML markers for appengine flexible custom runtime * chore(Dockerfile): update section markers for appengine flexible custom runtime * chore: remove README files for appengine-simple-jetty-main and custom-runtime * chore: update application to use Java 21 and adjust related configurations * chore: update app.yaml and pom.xml for Java 21 compatibility and environment adjustments * chore: update runtime_version in app.yaml to Java 21 * chore: update cloudbuild.yaml section markers for consistency * chore: format pom.xml files for improved readability * chore: enhance HelloServlet with detailed doGet method documentation and finalize class declaration * chore: update package-info.java with complete copyright and license information
1 parent a1d1fb5 commit 6d0a235

File tree

13 files changed

+499
-0
lines changed

13 files changed

+499
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START gae_flex_repackage_yaml]
16+
runtime: java
17+
env: flex
18+
runtime_config:
19+
operating_system: ubuntu22
20+
runtime_version: 21
21+
entrypoint: "java -jar jetty-jar-with-dependencies.jar sample.war"
22+
handlers:
23+
- url: /.*
24+
script: this field is required, but ignored
25+
26+
manual_scaling:
27+
instances: 1
28+
# [END gae_flex_repackage_yaml]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# This file specifies files that are *not* uploaded to Google Cloud
2+
# using gcloud. It follows the same syntax as .gitignore, with the addition of
3+
# "#!include" directives (which insert the entries of the given .gitignore-style
4+
# file at that point).
5+
#
6+
# For more information, run:
7+
# $ gcloud topic gcloudignore
8+
#
9+
.gcloudignore
10+
# If you would like to upload your .git directory, .gitignore file or files
11+
# from your .gitignore file, remove the corresponding line
12+
# below:
13+
.git
14+
.gitignore
15+
16+
# Target directory for maven builds
17+
target/
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
runtime: java
16+
env: flex
17+
runtime_config:
18+
operating_system: ubuntu22
19+
runtime_version: 21
20+
entrypoint: "java -jar jetty-jar-with-dependencies.jar sample.war"
21+
handlers:
22+
- url: /.*
23+
script: this field is required, but ignored
24+
25+
manual_scaling:
26+
instances: 1
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<!--
2+
Copyright 2025 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
17+
<project xmlns="http://maven.apache.org/POM/4.0.0"
18+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
<groupId>com.example.appengine</groupId>
22+
<artifactId>simple-jetty-main</artifactId>
23+
<name>simplejettymain-j21</name>
24+
<version>1</version>
25+
<packaging>jar</packaging>
26+
27+
<!--
28+
The parent pom defines common style checks and testing strategies for our samples.
29+
Removing or replacing it should not effect the execution of the samples in anyway.
30+
-->
31+
<parent>
32+
<groupId>com.google.cloud.samples</groupId>
33+
<artifactId>shared-configuration</artifactId>
34+
<version>1.2.0</version>
35+
</parent>
36+
<!-- [START gae_java21_properties] -->
37+
<properties>
38+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
39+
<maven.compiler.source>21</maven.compiler.source>
40+
<maven.compiler.target>21</maven.compiler.target>
41+
<jetty.version>9.4.56.v20240826</jetty.version>
42+
</properties>
43+
<!-- [END gae_java21_properties] -->
44+
45+
<!-- [START gae_java21_server_dependencies] -->
46+
<dependencies>
47+
<!-- Embedded Jetty dependencies -->
48+
<dependency>
49+
<groupId>org.eclipse.jetty</groupId>
50+
<artifactId>jetty-server</artifactId>
51+
<version>${jetty.version}</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.eclipse.jetty</groupId>
55+
<artifactId>jetty-webapp</artifactId>
56+
<version>${jetty.version}</version>
57+
<type>jar</type>
58+
</dependency>
59+
<dependency>
60+
<groupId>org.eclipse.jetty</groupId>
61+
<artifactId>jetty-util</artifactId>
62+
<version>${jetty.version}</version>
63+
</dependency>
64+
<dependency>
65+
<groupId>org.eclipse.jetty</groupId>
66+
<artifactId>jetty-annotations</artifactId>
67+
<version>${jetty.version}</version>
68+
</dependency>
69+
<!-- extra explicit dependency needed because there is a JSP in the sample-->
70+
<dependency>
71+
<groupId>org.eclipse.jetty</groupId>
72+
<artifactId>apache-jsp</artifactId>
73+
<version>${jetty.version}</version>
74+
</dependency>
75+
</dependencies>
76+
<!-- [END gae_java21_server_dependencies] -->
77+
78+
<build>
79+
<plugins>
80+
<!-- Exec Maven Plugin provides goals to help execute Main class locally -->
81+
<!-- [START gae_java21_exec_plugin] -->
82+
83+
<plugin>
84+
<groupId>org.apache.maven.plugins</groupId>
85+
<artifactId>maven-assembly-plugin</artifactId>
86+
<version>3.0.0</version>
87+
<configuration>
88+
<finalName>jetty</finalName>
89+
<descriptorRefs>
90+
<descriptorRef>jar-with-dependencies</descriptorRef>
91+
</descriptorRefs>
92+
<archive>
93+
<manifest>
94+
<mainClass>com.example.appengine.jetty.Main</mainClass>
95+
</manifest>
96+
</archive>
97+
</configuration>
98+
<executions>
99+
<execution>
100+
<id>make-assembly</id>
101+
<phase>package</phase>
102+
<goals>
103+
<goal>single</goal>
104+
</goals>
105+
</execution>
106+
</executions>
107+
</plugin>
108+
109+
<!-- [END gae_java21_exec_plugin] -->
110+
111+
</plugins>
112+
</build>
113+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.appengine.jetty;
18+
19+
// [START gae_java21_server]
20+
import org.eclipse.jetty.server.Server;
21+
import org.eclipse.jetty.webapp.Configuration.ClassList;
22+
import org.eclipse.jetty.webapp.WebAppContext;
23+
24+
/** Simple Jetty Main that can execute a WAR file when passed as an argument. */
25+
public class Main {
26+
27+
public static void main(String[] args) throws Exception {
28+
if (args.length != 1) {
29+
System.err.println("Usage: need a relative path to the war file to execute");
30+
System.exit(1);
31+
}
32+
System.setProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.StrErrLog");
33+
System.setProperty("org.eclipse.jetty.LEVEL", "INFO");
34+
35+
// Create a basic Jetty server object that will listen on port defined by
36+
// the PORT environment variable when present, otherwise on 8080.
37+
int port = Integer.parseInt(System.getenv().getOrDefault("PORT", "8080"));
38+
Server server = new Server(port);
39+
40+
// The WebAppContext is the interface to provide configuration for a web
41+
// application. In this example, the context path is being set to "/" so
42+
// it is suitable for serving root context requests.
43+
WebAppContext webapp = new WebAppContext();
44+
webapp.setContextPath("/");
45+
webapp.setWar(args[0]);
46+
ClassList classlist = ClassList.setServerDefault(server);
47+
48+
// Enable Annotation Scanning.
49+
classlist.addBefore(
50+
"org.eclipse.jetty.webapp.JettyWebXmlConfiguration",
51+
"org.eclipse.jetty.annotations.AnnotationConfiguration");
52+
53+
// Set the the WebAppContext as the ContextHandler for the server.
54+
server.setHandler(webapp);
55+
56+
// Start the server! By using the server.join() the server thread will
57+
// join with the current thread. See
58+
// "http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join()"
59+
// for more details.
60+
server.start();
61+
server.join();
62+
}
63+
}
64+
// [END gae_java21_server]
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START gae_flexible_custom_runtime]
16+
17+
# Use Maven to build the project with JDK 8
18+
FROM maven:3.8.6-openjdk-8 AS build
19+
20+
# Set working directory
21+
WORKDIR /app
22+
23+
# Copy the application source code
24+
COPY . .
25+
26+
# Build the application
27+
RUN mvn clean package
28+
29+
# Use Jetty as the runtime
30+
FROM jetty:9.4-jdk8
31+
32+
# Set Jetty working directory
33+
WORKDIR /var/lib/jetty/webapps
34+
35+
# Copy the built WAR file
36+
COPY --from=build /app/target/*.war ./ROOT.war
37+
38+
# Expose the default Jetty port
39+
EXPOSE 8080
40+
41+
# Start Jetty correctly
42+
CMD ["java", "-Djetty.base=/var/lib/jetty", "-jar", "/usr/local/jetty/start.jar"]
43+
# [END gae_flexible_custom_runtime]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START gae_flex_custom_yaml]
16+
runtime: custom
17+
env: flex
18+
instance_class: F1
19+
20+
handlers:
21+
- url: /.*
22+
script: auto
23+
# [END gae_flex_custom_yaml]
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# [START gae_cloudbuild_yaml]
15+
steps:
16+
# Step 1: Build the Docker image
17+
- name: "gcr.io/cloud-builders/docker"
18+
args:
19+
- "build"
20+
- "-t"
21+
- "${_LOCATION}-docker.pkg.dev/${_PROJECT}/${_REPOSITORY}/my-java-app:v1"
22+
- "."
23+
24+
# Step 2: Push the Docker image to Artifact Registry
25+
- name: "gcr.io/cloud-builders/docker"
26+
args:
27+
- "push"
28+
- "${_LOCATION}-docker.pkg.dev/${_PROJECT}/${_REPOSITORY}/my-java-app:v1"
29+
30+
substitutions:
31+
_LOCATION: "asia" # Change this based on your region (e.g., 'us', 'europe', 'asia')
32+
_REPOSITORY: "test-app" # Replace with your Artifact Registry repository name
33+
_PROJECT: "project-id" # Replace with your Google Cloud Project ID
34+
35+
images:
36+
- "${_LOCATION}-docker.pkg.dev/${_PROJECT}/${_REPOSITORY}/my-java-app:v1"
37+
# [END gae_cloudbuild_yaml]

0 commit comments

Comments
 (0)