In a microservices architecture, service health checks and a reliable Linux deployment process are key to operational stability. This article will share how to build a health check interface in Spring Boot, combined with version management, and how to deploy and manage a Spring Boot JAR in a Linux environment to achieve automated execution and monitoring.

This article will explain how to build a health check interface in Spring Boot, manage versions using Maven BuildProperties, and use a Makefile for streamlined operations, automating the process from development to deployment. Finally, it will cover running the JAR on Linux and managing it with Supervisor.

Designing the Spring Boot Health Check Interface

A health check interface should provide:

  • Service availability status
  • Current version or build time
  • Status of other dependent services (e.g., database, external APIs)

DTO Example

package auth.hoohoo.top.auth_service.dto;

public record HealthCheck(String version, String buildat, String info) {}

Maven Configuration to Generate BuildProperties

Configure the Spring Boot Maven Plugin in pom.xml:

<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <executions>
            <execution>
                <goals>
                    <goal>repackage</goal>
                    <goal>build-info</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
</plugins>

Example Controller

package auth.hoohoo.top.auth_service.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.info.BuildProperties;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import auth.hoohoo.top.auth_service.dto.HealthCheck;

@RestController
public class VersionController {
    private final BuildProperties buildProperties;

    @Autowired
    public VersionController(BuildProperties buildProperties) {
        this.buildProperties = buildProperties;
    }

    @GetMapping("/health")
    public HealthCheck health() {
        String versionInfo = buildProperties.getVersion();
        String buildAt = buildProperties.getTime().toString();
        return new HealthCheck(versionInfo, buildAt, "status ok");
    }
}

Packaging Command

mvn clean package -DskipTests

Confirm that META-INF/build-info.properties has been generated:

jar tf target/auth-service-0.0.1-SNAPSHOT.jar | grep build-info

Create Makefile

APP_NAME=auth-service
JAR_FILE=$(shell find target -name "$(APP_NAME)-*.jar" | head -n 1)
VERSION=$(shell mvn help:evaluate -Dexpression=project.version -q -DforceStdout)

.PHONY: all clean build run run-bg stop test coverage

# Default flow: clean -> build -> run
all: clean build run

# Clean
clean:
	mvn clean

# Compile and package JAR (skip tests)
build:
	mvn clean package -DskipTests

# Run in foreground
run:
	java -jar $(JAR_FILE)

# Run in background
run-bg:
	@sh -c 'nohup java -jar $(JAR_FILE) > app.log 2>&1 & echo $$! > app.pid'
	@echo "Started service in background (PID: $$(cat app.pid))"

# Stop background service
stop:
	@if [ -f app.pid ]; then \
		PID=`cat app.pid`; \
		if kill -0 $$PID > /dev/null 2>&1; then \
			kill -9 $$PID && rm -f app.pid && echo "Stopped service (PID: $$PID)"; \
		else \
			echo "Process $$PID not running"; \
			rm -f app.pid; \
		fi \
	else \
		echo "No PID file found"; \
	fi

# Run tests
test:
	mvn test

# Generate Jacoco coverage report
coverage:
	mvn clean test jacoco:report
	@echo "Coverage report generated at target/site/jacoco/index.html"

# Update BuildProperties (Spring Boot generates this automatically during packaging)
update-build-info:
	@echo "Spring Boot automatically generates BuildProperties during 'mvn package'"

Packaging and Running

Command Function
make build Package the JAR (skips tests)
make run Run the service in the foreground
make run-bg Run the service in the background, logs to app.log
make stop Stop the background service
make test Execute unit tests
make coverage Generate Jacoco coverage report
make update-build-info Update BuildProperties to ensure /health shows the latest version and build time

After running the application, the /health endpoint will now return the version and build time.

Subsequently, this can be integrated into various environments, such as Linux, CI/CD, or Kubernetes Liveness/Readiness Probes.