1+ #! /bin/bash
2+
3+ # Spring Boot Native Compilation Build Script
4+ # This script demonstrates the various native compilation options available
5+
6+ set -e
7+
8+ echo " 🚀 Spring Boot Native Compilation Build Script"
9+ echo " ================================================"
10+
11+ # Function to print colored output
12+ print_step () {
13+ echo -e " \n\033[1;34m$1 \033[0m"
14+ }
15+
16+ print_success () {
17+ echo -e " \033[1;32m✅ $1 \033[0m"
18+ }
19+
20+ print_error () {
21+ echo -e " \033[1;31m❌ $1 \033[0m"
22+ }
23+
24+ # Check if GraalVM is available
25+ check_graalvm () {
26+ print_step " Checking GraalVM installation..."
27+ if command -v native-image & > /dev/null; then
28+ print_success " GraalVM native-image found: $( native-image --version | head -n1) "
29+ else
30+ print_error " GraalVM native-image not found. Please install GraalVM."
31+ echo " Install GraalVM: https://www.graalvm.org/downloads/"
32+ echo " Or use SDKMAN: sdk install java 21.0.1-graal"
33+ exit 1
34+ fi
35+ }
36+
37+ # Clean previous builds
38+ clean_build () {
39+ print_step " Cleaning previous builds..."
40+ ./mvnw clean
41+ print_success " Clean completed"
42+ }
43+
44+ # Run tests first
45+ run_tests () {
46+ print_step " Running tests to ensure application works..."
47+ ./mvnw test
48+ print_success " Tests passed"
49+ }
50+
51+ # Compile native executable
52+ compile_native () {
53+ print_step " Compiling native executable with AOT processing..."
54+ echo " This may take several minutes..."
55+
56+ # Set SPRING_PROFILES_ACTIVE for native compilation
57+ export SPRING_PROFILES_ACTIVE=native
58+
59+ ./mvnw -Pnative clean native:compile
60+ print_success " Native executable compiled successfully"
61+
62+ if [ -f target/sakila-api ]; then
63+ echo " 📁 Native executable location: target/sakila-api"
64+ echo " 📊 Executable size: $( du -h target/sakila-api | cut -f1) "
65+ fi
66+ }
67+
68+ # Test native executable
69+ test_native () {
70+ print_step " Testing native executable..."
71+ if [ -f target/sakila-api ]; then
72+ echo " Starting native executable for quick test..."
73+ timeout 10s ./target/sakila-api --server.port=8081 --spring.profiles.active=native,test > /dev/null 2>&1 &
74+ PID=$!
75+ sleep 5
76+
77+ if kill -0 $PID 2> /dev/null; then
78+ print_success " Native executable started successfully"
79+ kill $PID 2> /dev/null || true
80+ else
81+ print_error " Native executable failed to start"
82+ fi
83+ else
84+ print_error " Native executable not found"
85+ fi
86+ }
87+
88+ # Build container image with native binary
89+ build_container () {
90+ print_step " Building container image with native binary..."
91+ echo " This will create a lightweight container with the native executable..."
92+
93+ ./mvnw spring-boot:build-image -Pnative
94+ print_success " Container image built successfully"
95+
96+ echo " 🐳 Container image: sakila-api:0.0.1-SNAPSHOT"
97+ echo " Run with: docker run -p 8080:8080 sakila-api:0.0.1-SNAPSHOT"
98+ }
99+
100+ # Run native tests (if available)
101+ run_native_tests () {
102+ print_step " Running native tests..."
103+ ./mvnw -Pnative test
104+ print_success " Native tests completed"
105+ }
106+
107+ # Display performance comparison
108+ show_performance_info () {
109+ print_step " Performance Characteristics:"
110+ echo "
111+ 🚀 Startup Time: 10-100x faster than JVM
112+ 💾 Memory Usage: Reduced footprint (especially for small apps)
113+ ⚡ Build Time: Longer compilation (2-10 minutes)
114+ 📦 Image Size: Larger executable, but no JVM needed
115+ 🎯 Runtime Perf: Different characteristics vs JIT compilation
116+
117+ 📋 Commands Summary:
118+ ./mvnw -Pnative clean native:compile # Build native executable
119+ ./mvnw -Pnative test # Run native tests
120+ ./mvnw spring-boot:build-image -Pnative # Build container image
121+ ./target/sakila-api # Run native executable
122+ "
123+ }
124+
125+ # Main execution
126+ main () {
127+ case " ${1:- help} " in
128+ " check" )
129+ check_graalvm
130+ ;;
131+ " clean" )
132+ clean_build
133+ ;;
134+ " test" )
135+ run_tests
136+ ;;
137+ " compile" )
138+ check_graalvm
139+ clean_build
140+ compile_native
141+ test_native
142+ ;;
143+ " container" )
144+ check_graalvm
145+ build_container
146+ ;;
147+ " native-test" )
148+ check_graalvm
149+ run_native_tests
150+ ;;
151+ " full" )
152+ check_graalvm
153+ clean_build
154+ run_tests
155+ compile_native
156+ test_native
157+ show_performance_info
158+ ;;
159+ " help" |* )
160+ echo " Usage: $0 [command]"
161+ echo " "
162+ echo " Commands:"
163+ echo " check Check GraalVM installation"
164+ echo " clean Clean previous builds"
165+ echo " test Run application tests"
166+ echo " compile Compile native executable"
167+ echo " container Build container with native image"
168+ echo " native-test Run native-specific tests"
169+ echo " full Complete native build process"
170+ echo " help Show this help message"
171+ echo " "
172+ show_performance_info
173+ ;;
174+ esac
175+ }
176+
177+ main " $@ "
0 commit comments