Skip to content

Commit 726dbd2

Browse files
committed
feat: add run samples
1 parent 39962cb commit 726dbd2

File tree

6 files changed

+221
-2
lines changed

6 files changed

+221
-2
lines changed

run/helloworld-source/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Cloud Run Hello World Sample
2+
3+
This sample shows how to deploy a Hello World [Spring Boot](https://spring.io/projects/spring-boot)
4+
application to Cloud Run using source deploy.
5+
6+
For more details on how to work with this sample read the [Google Cloud Run Java Samples README](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/main/run).
7+
8+
[![Run in Google Cloud][run_img]][run_link]
9+
10+
[run_img]: https://storage.googleapis.com/cloudrun/button.svg
11+
[run_link]: https://deploy.cloud.run/?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&dir=run/helloworld
12+
13+
## Dependencies
14+
15+
* **Spring Boot**: Web server framework.
16+
* **Junit**: [development] Test running framework.

run/helloworld-source/pom.xml

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2019 Google LLC
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+
http://www.apache.org/licenses/LICENSE-2.0
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
-->
14+
<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">
15+
<modelVersion>4.0.0</modelVersion>
16+
<groupId>com.example.run</groupId>
17+
<artifactId>helloworld-source</artifactId>
18+
<version>0.0.1-SNAPSHOT</version>
19+
<packaging>jar</packaging>
20+
21+
<!-- The parent pom defines common style checks and testing strategies for our samples.
22+
Removing or replacing it should not affect the execution of the samples in anyway. -->
23+
24+
<parent>
25+
<groupId>com.google.cloud.samples</groupId>
26+
<artifactId>shared-configuration</artifactId>
27+
<version>1.2.0</version>
28+
</parent>
29+
<dependencyManagement>
30+
<dependencies>
31+
<dependency>
32+
<!-- Import dependency management from Spring Boot -->
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-dependencies</artifactId>
35+
<version>${spring-boot.version}</version>
36+
<type>pom</type>
37+
<scope>import</scope>
38+
</dependency>
39+
</dependencies>
40+
</dependencyManagement>
41+
<properties>
42+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
43+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
44+
<maven.compiler.target>25</maven.compiler.target>
45+
<maven.compiler.source>25</maven.compiler.source>
46+
<spring-boot.version>3.5.6</spring-boot.version>
47+
</properties>
48+
<dependencies>
49+
<dependency>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-starter-web</artifactId>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-starter-test</artifactId>
56+
<scope>test</scope>
57+
</dependency>
58+
<dependency>
59+
<groupId>org.junit.vintage</groupId>
60+
<artifactId>junit-vintage-engine</artifactId>
61+
<scope>test</scope>
62+
</dependency>
63+
<dependency>
64+
<groupId>junit</groupId>
65+
<artifactId>junit</artifactId>
66+
<scope>test</scope>
67+
</dependency>
68+
</dependencies>
69+
<build>
70+
<plugins>
71+
<plugin>
72+
<groupId>org.springframework.boot</groupId>
73+
<artifactId>spring-boot-maven-plugin</artifactId>
74+
<version>${spring-boot.version}</version>
75+
<executions>
76+
<execution>
77+
<goals>
78+
<goal>repackage</goal>
79+
</goals>
80+
</execution>
81+
</executions>
82+
</plugin>
83+
<!-- [START cloudrun_helloworld_jib] -->
84+
<plugin>
85+
<groupId>com.google.cloud.tools</groupId>
86+
<artifactId>jib-maven-plugin</artifactId>
87+
<version>3.4.0</version>
88+
<configuration>
89+
<to>
90+
<image>gcr.io/PROJECT_ID/helloworld</image>
91+
</to>
92+
</configuration>
93+
</plugin>
94+
<!-- [END cloudrun_helloworld_jib] -->
95+
</plugins>
96+
</build>
97+
</project>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2020 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+
// [START cloudrun_helloworld_service]
18+
19+
package com.example.helloworld;
20+
21+
import org.springframework.beans.factory.annotation.Value;
22+
import org.springframework.boot.SpringApplication;
23+
import org.springframework.boot.autoconfigure.SpringBootApplication;
24+
import org.springframework.web.bind.annotation.GetMapping;
25+
import org.springframework.web.bind.annotation.RestController;
26+
27+
@SpringBootApplication
28+
public class HelloworldApplication {
29+
30+
@Value("${NAME:World}")
31+
String name;
32+
33+
@RestController
34+
class HelloworldController {
35+
@GetMapping("/")
36+
String hello() {
37+
return "Hello " + name + "!";
38+
}
39+
}
40+
41+
public static void main(String[] args) {
42+
SpringApplication.run(HelloworldApplication.class, args);
43+
}
44+
}
45+
// [END cloudrun_helloworld_service]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright 2020 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 cloudrun_helloworld_properties]
15+
server.port=${PORT:8080}
16+
# [END cloudrun_helloworld_properties]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2020 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.helloworld;
18+
19+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
20+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
21+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
22+
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
27+
import org.springframework.boot.test.context.SpringBootTest;
28+
import org.springframework.test.context.junit4.SpringRunner;
29+
import org.springframework.test.web.servlet.MockMvc;
30+
31+
@RunWith(SpringRunner.class)
32+
@SpringBootTest
33+
@AutoConfigureMockMvc
34+
public class HelloworldApplicationTests {
35+
36+
@Autowired private MockMvc mockMvc;
37+
38+
@Test
39+
public void returnsHelloWorld() throws Exception {
40+
mockMvc
41+
.perform(get("/"))
42+
.andExpect(status().isOk())
43+
.andExpect(content().string("Hello World!"));
44+
}
45+
}

run/helloworld/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ limitations under the License.
4141
<properties>
4242
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
4343
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
44-
<maven.compiler.target>17</maven.compiler.target>
45-
<maven.compiler.source>17</maven.compiler.source>
44+
<maven.compiler.target>25</maven.compiler.target>
45+
<maven.compiler.source>25</maven.compiler.source>
4646
<spring-boot.version>3.2.2</spring-boot.version>
4747
</properties>
4848
<dependencies>

0 commit comments

Comments
 (0)