8000 Move Java11 samples (#1359) · hrodriguezv/java-docs-samples@663a40b · GitHub
[go: up one dir, main page]

Skip to content

Commit 663a40b

Browse files
authored
Move Java11 samples (GoogleCloudPlatform#1359)
* Add simple jetty web server for other applications * biquery sample * gaeinfo sample * update bigquery war file name * cloudsql sample * Cloud Postgresql sample * update license * spanner sample * bigtable sample * Add directory readme and pom * Update gaeinfo sample resource copying * update pom files * Update sample not to use com.google.* * Update Readmes * remove region tags * Add parent back to pom.xml * linting * Update pom.xml * Update parent README * remove parent pom.xml * linting * Kokoro - add packaging for jetty server * Add check for java 11 install * Update name for test instance * Update parent config * comment out test * Update test name. * Fix null pointer. * Delete big table sample
1 parent f7ab04f commit 663a40b

File tree

15 files changed

+1463
-0
lines changed

15 files changed

+1463
-0
lines changed

.kokoro/tests/run_tests.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ if [[ "$SCRIPT_DEBUG" != "true" ]]; then
6464
cd github/java-docs-samples
6565
fi
6666

67+
# Package local jetty dependency for Java11 samples
68+
if [[ "$JAVA_VERSION" == "11" ]]; then
69+
cd appengine-java11/appengine-simple-jetty-main/
70+
mvn package
71+
cd ../../
72+
fi
73+
6774
echo -e "\n******************** TESTING PROJECTS ********************"
6875
# Switch to 'fail at end' to allow all tests to complete before exiting.
6976
set +e

appengine-java11/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Google App Engine Standard Environment Samples for Java 11
2+
3+
<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/ludoch/samples&page=editor&open_in_editor=appengine-java11/README.md">
4+
<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a>
5+
6+
This is a repository that contains Java code samples for [Google App Engine][ae-docs]
7+
standard Java 11 environment.
8+
9+
[ae-docs]: https://cloud.google.com/appengine/docs/java/
10+
11+
## Prerequisites
12+
13+
**Private Alpha**
14+
To access the Java 11 Google App Engine standard runtime, you'll need to apply
15+
to become part of the App Engine [Alpha program](https://docs.google.com/forms/d/e/1FAIpQLSf5uE5eknJjFEmcVBI6sMitBU0QQ1LX_J7VrA_OTQabo6EEEw/viewform).
16+
17+
18+
### Download Maven
19+
20+
These samples use the [Apache Maven][maven] build system. Before getting
21+
started, be sure to [download][maven-download] and [install][maven-install] it.
22+
When you use Maven as described here, it will automatically download the needed
23+
client libraries.
24+
25+
[maven]: https://maven.apache.org
26+
[maven-download]: https://maven.apache.org/download.cgi
27+
[maven-install]: https://maven.apache.org/install.html
28+
29+
### Create a Project in the Google Cloud Platform Console
30+
31+
If you haven't already created a project, create one now. Projects enable you to
32+
manage all Google Cloud Platform resources for your app, including deployment,
33+
access control, billing, and services.
34+
35+
1. Open the [Cloud Platform Console][cloud-console].
36+
1. In the drop-down menu at the top, select **Create a project**.
37+
1. Give your project a name.
38+
1. Make a note of the project ID, which might be different from the project
39+
name. The project ID is used in commands and in configurations.
40+
41+
[cloud-console]: https://console.cloud.google.com/
42+
43+
### Google Cloud Shell, Open JDK 11 setup:
44+
45+
To switch to an Open JDK 11 in a Cloud shell session, you can use:
46+
47+
```
48+
sudo update-alternatives --config java
49+
# And select the usr/lib/jvm/java-11-openjdk-amd64/bin/java version.
50+
# Also, set the JAVA_HOME variable for Maven to pick the correct JDK:
51+
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
52+
```
53+
54+
### appengine-simple-jetty-main
55+
56+
[appengine-simple-jetty-main](appengine-simple-jetty-main) is a shared artifact
57+
that provides a Jetty Web Server for the servlet based runtime. Packaged as a
58+
jar, the Main Class will load a war file, passed as an argument, as the
59+
context root of the web application listening to port 8080.
60+
Some samples create a `<sample-name>.war` which is used as an argument in the
61+
App Engine app.yaml entrypoint field.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.example.appengine.demo</groupId>
5+
<artifactId>simple-jetty-main</artifactId>
6+
<name>simplejettymain-j11</name>
7+
<version>1</version>
8+
<packaging>jar</packaging>
9+
10+
<!--
11+
The parent pom defines common style checks and testing strategies for our samples.
12+
Removing or replacing it should not effect the execution of the samples in anyway.
13+
-->
14+
<parent>
15+
<groupId>com.google.cloud.samples</groupId>
16+
<artifactId>shared-configuration</artifactId>
17+
<version>1.0.11</version>
18+
</parent>
19+
20+
<properties>
21+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
22+
<maven.compiler.source>11</maven.compiler.source>
23+
<maven.compiler.target>11</maven.compiler.target>
24+
</properties>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.eclipse.jetty</groupId>
29+
<artifactId>jetty-server</artifactId>
30+
<version>9.4.14.v20181114</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.eclipse.jetty</groupId>
34+
<artifactId>jetty-webapp</artifactId>
35+
<version>9.4.14.v20181114</version>
36+
<type>jar</type>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.eclipse.jetty</groupId>
40+
<artifactId>jetty-util</artifactId>
41+
<version>9.4.14.v20181114</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.eclipse.jetty</groupId>
45+
<artifactId>jetty-annotations</artifactId>
46+
<version>9.4.14.v20181114</version>
47+
<type>jar</type>
48+
</dependency>
49+
<!-- extra explicit dependency needed because there is a JSP in the sample-->
50+
<dependency>
51+
<groupId>org.eclipse.jetty</groupId>
52+
<artifactId>apache-jsp</artifactId>
53+
<version>9.4.14.v20181114</version>
54+
</dependency>
55+
</dependencies>
56+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.demo.jettymain;
18+
19+
import org.eclipse.jetty.server.Server;
20+
import org.eclipse.jetty.webapp.Configuration.ClassList;
21+
import org.eclipse.jetty.webapp.WebAppContext;
22+
23+
/** Simple Jetty Main that can execute a WAR file when passed as an argument. */
24+
public class Main {
25+
26+
public static void main(String[] args) throws Exception {
27+
if (args.length != 1) {
28+
System.err.println("Usage: need a relative path to the war file to execute");
29+
System.exit(1);
30+
}
31+
System.setProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.StrErrLog");
32+
System.setProperty("org.eclipse.jetty.LEVEL", "INFO");
33+
34+
// Create a basic Jetty server object that will listen on port 8080.
35+
// Note: If you set this to port 0, a randomly available port will be
36+
// assigned. You can find the assigned port in the logs or programmatically
37+
// obtain it.
38+
Server server = new Server(8080);
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+
}

appengine-java11/gaeinfo/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Google App Engine Information App for Java11
2+
3+
<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/ludoch/samples&page=editor&open_in_editor==appengine-java11/gaeinfo/README.md">
4+
<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a>
5+
6+
This sample demonstrates how to show all system environment metadata and properties on Google App
7+
Engine standard app.
8+
9+
## Setup the Sample App
10+
11+
- Copy the sample apps to your local machine:
12+
```
13+
git clone https://github.com/GoogleCloudPlatform/java-docs-samples
14+
```
15+
16+
- Add the [appengine-simple-jetty-main](../README.md#appengine-simple-jetty-main)
17+
Main class to your classpath:
18+
```
19+
cd java-docs-samples/appengine-java11/appengine-simple-jetty-main
20+
mvn install
21+
```
22+
23+
- Move into the `appengine-java11/gaeinfo` directory and compile the app:
24+
```
25+
cd ../gaeinfo
26+
mvn package
27+
```
28+
29+
## Setup you Google Cloud Project
30+
31+
* If you haven't already, Download and initialize the [Cloud SDK](https://cloud.google.com/sdk/)
32+
33+
`gcloud init`
34+
35+
* If you haven't already, Create an App Engine app within the current Google Cloud Project
36+
37+
`gcloud app create`
38+
39+
* If you haven't already, Setup [Application Default Credentials](https://developers.google.com/identity/protocols/application-default-credentials)
40+
41+
`gcloud auth application-default login`
42+
43+
## Deploy
44+
45+
- Deploy to App Engine standard environment using the following Maven command.
46+
```
47+
mvn appengine:deploy -Dapp.deploy.projectId=<your-project-id>
48+
```
49+
- Direct your browser to `https://<your-project-id>.appspot.com`.
50+
- View more in-depth metrics data on the [StackDriver Monitoring Dashboard][dashboard]
51+
52+
Note: The first time the app is run (or after any metrics definitions have
53+
been deleted) it may take up to 5 minutes for the MetricDescriptors to sync
54+
with StackDriver before any results are shown. If you do not see results,
55+
please wait a few moments and try again.
56+
57+
[dashboard]: https://console.cloud.google.com/monitoring

appengine-java11/gaeinfo/pom.xml

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<!--
2+
Copyright 2019 Google Inc.
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+
<project>
17+
<modelVersion>4.0.0</modelVersion>
18+
<packaging>war</packaging>
19+
<version>1.0-SNAPSHOT</version>
20+
<groupId>com.example.appengine</groupId>
21+
<artifactId>appengine-gaeinfo-j11</artifactId>
22+
23+
<!--
24+
The parent pom defines common style checks and testing strategies for our samples.
25+
Removing or replacing it should not affect the execution of the samples in anyway.
26+
-->
27+
<parent>
28+
<groupId>com.google.cloud.samples</groupId>
29+
<artifactId>shared-configuration</artifactId>
30+
<version>1.0.11</version>
31+
</parent>
32+
33+
<properties>
34+
<maven.compiler.target>11</maven.compiler.target>
35+
<maven.compiler.source>11</maven.compiler.source>
36+
</properties>
37+
38+
<!-- All dependencies are scoped as provided.
39+
Dependent-jars are created and added to
40+
${project.build.directory}/appengine-staging -->
41+
<dependencies>
42+
<!-- Dependency from folder `../appengine-simple-jetty-main` -->
43+
<!-- See README for instructions for more information -->
44+
<dependency>
45+
<groupId>com.google.appengine.demo</groupId>
46+
<artifactId>simple-jetty-main</artifactId>
47+
<version>1</version>
48+
<scope>system</scope>
49+
<systemPath>${project.basedir}/../appengine-simple-jetty-main/target/simple-jetty-main-1.jar</systemPath>
50+
</dependency>
51+
52+
<dependency>
53+
<groupId>javax.servlet</groupId>
54+
<artifactId>javax.servlet-api</artifactId>
55+
<version>3.1.0</version>
56+
<scope>provided</scope>
57+
</dependency>
58+
59+
<dependency>
60+
<groupId>com.squareup.okhttp3</groupId>
61+
<artifactId>okhttp</artifactId>
62+
<version>3.12.1</version>
63+
<scope>provided</scope>
64+
</dependency>
65+
66+
<dependency>
67+
<groupId>com.google.code.gson</groupId>
68+
<artifactId>gson</artifactId>
69+
<version>2.8.5</version>
70+
<scope>provided</scope>
71+
</dependency>
72+
73+
<dependency>
74+
<groupId>org.thymeleaf</groupId>
75+
<artifactId>thymeleaf</artifactId>
76+
<version>3.0.11.RELEASE</version>
77+
<scope>provided</scope>
78+
</dependency>
79+
</dependencies>
80+
81+
<build>
82+
<finalName>gaeinfo</finalName>
83+
<plugins>
84+
<plugin>
85+
<groupId>org.apache.maven.plugins</groupId>
86+
<artifactId>maven-war-plugin</artifactId>
87+
<version>3.2.2</version>
88+
</plugin>
89+
<plugin>
90+
<groupId>org.apache.maven.plugins</groupId>
91+
<artifactId>maven-dependency-plugin</artifactId>
92+
<version>3.1.1</version>
93+
<executions>
94+
<execution>
95+
<id>copy</id>
96+
<phase>prepare-package</phase>
97+
<goals>
98+
<goal>copy-dependencies</goal>
99+
</goals>
100+
<configuration>
101+
<outputDirectory>
102+
${project.build.directory}/appengine-staging
103+
</outputDirectory>
104+
</configuration>
105+
</execution>
106+
</executions>
107+
</plugin>
108+
<plugin>
109+
<artifactId>maven-resources-plugin</artifactId>
110+
<version>2.7</version>
111+
<executions>
112+
<execution>
113+
<id>copy-resources</id>
114+
<phase>install</phase>
115+
<goals>
116+
<goal>copy-resources</goal>
117+
</goals>
118+
<configuration>
119+
<outputDirectory>${project.build.directory}/appengine-staging/cprof</outputDirectory>
120+
</configuration>
121+
</execution>
122+
</executions>
123+
</plugin>
124+
125+
<plugin>
126+
<groupId>com.google.cloud.tools</groupId>
127+
<artifactId>appengine-maven-plugin</artifactId>
128+
<version>2.0.0-rc5</version>
129+
<configuration>
130+
<version>gaeinfo</version>
131+
</configuration>
132+
</plugin>
133+
</plugins>
134+
135+
</build>
136+
</project>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
runtime: java11
2+
instance_class: F2
3+
entrypoint: 'java -cp * com.example.appengine.demo.jettymain.Main gaeinfo.war'

0 commit comments

Comments
 (0)
0