Languages & Build

Maven

Java project build automation and dependency management

🐧 Linux🍎 Mac🪟 Windows
Reviewed: Tested on: Kubernetes 1.29 · Terraform 1.8 · Ubuntu 22.04

What is this?

Maven builds Java projects — compiles code, downloads libraries, and packages apps for deployment.

📥

Step 01

Install Maven

(01)Install

Linux
1sudo apt-get install -y maven
2# Or SDKMAN:
3curl -s "https://get.sdkman.io" | bash && sdk install maven

Step 02

Verify

(01)Version

Linux
1mvn -version

Step 03

Manage Maven Projects

(01)Build lifecycle

Linux
1mvn clean install # compile, test, package
2mvn clean package -DskipTests
3mvn spring-boot:run # run Spring Boot
4mvn dependency:tree # show dependencies

📋Config templates

1 YAML template for Maven. Copy and deploy after setup.

1 ready-to-copy template. Expand one, copy the YAML, then run the deploy commands.

service-a-deployment.yml

yaml
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4 name: service-a
5spec:
6 replicas: 1
7 selector:
8 matchLabels:
9 app: service-a
10 template:
11 metadata:
12 labels:
13 app: service-a
14 spec:
15 containers:
16 - name: service-a
17 image: spring-api-a:local
18 imagePullPolicy: IfNotPresent
19 ports:
20 - containerPort: 8080
21 resources:
22 requests:
23 memory: "256Mi"
24 cpu: "250m"
25 limits:
26 memory: "512Mi"
27 cpu: "500m"

service-a-svc.yml

yaml
1apiVersion: v1
2kind: Service
3metadata:
4 name: service-a
5spec:
6 type: ClusterIP
7 selector:
8 app: service-a
9 ports:
10 - port: 8080
11 targetPort: 8080

service-b-deployment.yml

yaml
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4 name: service-b
5spec:
6 replicas: 1
7 selector:
8 matchLabels:
9 app: service-b
10 template:
11 metadata:
12 labels:
13 app: service-b
14 spec:
15 containers:
16 - name: service-b
17 image: spring-api-b:local
18 imagePullPolicy: IfNotPresent
19 ports:
20 - containerPort: 8080
21 env:
22 - name: SERVICE_A_URL
23 value: "http://service-a:8080"
24 resources:
25 requests:
26 memory: "256Mi"
27 cpu: "250m"
28 limits:
29 memory: "512Mi"
30 cpu: "500m"

service-b-svc.yml

yaml
1apiVersion: v1
2kind: Service
3metadata:
4 name: service-b
5spec:
6 type: NodePort
7 selector:
8 app: service-b
9 ports:
10 - port: 8080
11 targetPort: 8080
12 nodePort: 32080
📄

Step 01

Deploy Spring Microservices

(01)Build and load images

Linux
1# Build Spring JARs, then Docker images
2docker build -t spring-api-a:local ./service-a
3docker build -t spring-api-b:local ./service-b
4minikube image load spring-api-a:local
5minikube image load spring-api-b:local

(02)Apply all manifests

Linux
1kubectl apply -f service-a-deployment.yml
2kubectl apply -f service-a-svc.yml
3kubectl apply -f service-b-deployment.yml
4kubectl apply -f service-b-svc.yml
5kubectl get pods,svc