Ready to deploy your first Java app on Google Cloud Platform? In this tutorial, we’ll guide you through deploying a basic “Hello World” web app using two powerful GCP services: Cloud Run and App Engine. Both options are serverless and ideal for beginners.
1. Prerequisites
- A GCP project with billing enabled
gcloud
CLI installed and initialized- Docker installed (for Cloud Run)
- Java 17+ and Maven or Gradle installed
- A simple Spring Boot app
2. Hello World with Cloud Run
Step 1: Create Your Spring Boot App
// MainApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;
@SpringBootApplication
@RestController
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
@GetMapping("/")
public String hello() {
return "Hello from Cloud Run!";
}
}
<!-- pom.xml -->
<project xmlns="http://maven.apache.org/POM/4.0.0" ...>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<java.version>17</java.version>
<spring.boot.version>3.2.0</spring.boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Step 2: Create a Dockerfile
# Dockerfile
FROM eclipse-temurin:17-jdk-alpine
WORKDIR /app
COPY target/demo-0.0.1-SNAPSHOT.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]
Step 3: Build and Deploy
mvn clean package
gcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/hello-app
gcloud run deploy hello-app \
--image gcr.io/YOUR_PROJECT_ID/hello-app \
--platform managed \
--region us-central1 \
--allow-unauthenticated
Once deployed, Cloud Run will return a public HTTPS URL where your app is live!
3. Hello World with App Engine
Step 1: Use the Same App
You can reuse the same Spring Boot app and built JAR. Add an App Engine config:
# app.yaml
runtime: java17
entrypoint: java -jar demo-0.0.1-SNAPSHOT.jar
handlers:
- url: /.*
script: auto
Step 2: Deploy to App Engine
gcloud app create --region=us-central
gcloud app deploy
After deployment, your app will be live at:
https://PROJECT_ID.REGION_ID.r.appspot.com
4. Summary
Both Cloud Run and App Engine make deploying Java apps simple and fast.
Feature | Cloud Run | App Engine |
---|---|---|
Uses Containers | Yes | Optional (uses JARs) |
Flexibility | High (custom Docker) | Moderate (managed JRE) |
Best for | Microservices & custom stack | Simple Java web apps |
Next Steps
- Add configuration via environment variables
- Connect a custom domain
- Add authentication with Firebase or IAM
- Automate deployments using Cloud Build or GitHub Actions
Now that your app is live, try modifying the message, adding new routes, or connecting it to a database like Firestore or Cloud SQL. The cloud is your playground!
0 Comments