Step 1 Initialize a Spring Boot App
Generate a new Spring Boot app with Spring Initializr.
curl https://start.spring.io/starter.tgz \
-d type=maven-project \
-d language=java \
-d dependencies=web \
-d baseDir=maven-cloud-run | tar -xzvf -
cd maven-cloud-run/
Change to the directory of the template app and build and run the app using Maven.
cd maven-cloud-run
./mvnw -DskipTests spring-boot:run
Step 2 Add a web controller
nano src/main/java/com/example/demo/DemoApplication.java
...
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@GetMapping("/")
public String saySomething() {
return "Java app on Cloud Run, containerized by Jib!";
}
}
...
Step 3 Rebuild and run the app.
./mvnw spring-boot:run
Step 4 Containerize your app and publish to Container Registry
Activate the Container Registry API.
gcloud services enable artifactregistry.googleapis.com
Step 5 Containerize your app and publish to Container Registry
Run Jib to build a Docker image and publish to Container Registry.
./mvnw com.google.cloud.tools:jib-maven-plugin:3.3.1:build \
-Dimage=gcr.io/$GOOGLE_CLOUD_PROJECT/kotlin-jib-cloud-run \
-Djib.from.image=eclipse-temurin:17-jre