0
# Native Compilation
1
2
GraalVM native image compilation support for creating native executables with fast startup times and low memory footprint, ideal for cloud-native deployments and serverless environments.
3
4
## Capabilities
5
6
### Native Profile
7
8
Maven profile that enables GraalVM native compilation with required plugin configurations.
9
10
```xml { .api }
11
/**
12
* Native compilation profile for GraalVM native image creation
13
* Activates additional plugin configurations for AOT processing
14
*/
15
<profile>
16
<id>native</id>
17
<build>
18
<plugins>
19
<!-- Native-specific plugin configurations -->
20
</plugins>
21
</build>
22
</profile>
23
```
24
25
**Usage Examples:**
26
27
```bash
28
# Build native executable
29
mvn -Pnative native:compile
30
31
# Build and test native executable
32
mvn -Pnative package
33
34
# Create native executable with specific name
35
mvn -Pnative native:compile -Dapp.name=my-native-app
36
```
37
38
### JAR Plugin Native Configuration
39
40
Enhanced JAR plugin configuration for native compilation marker.
41
42
```xml { .api }
43
/**
44
* Maven JAR plugin native configuration
45
* Adds manifest entry indicating native processing
46
*/
47
<plugin>
48
<groupId>org.apache.maven.plugins</groupId>
49
<artifactId>maven-jar-plugin</artifactId>
50
<configuration>
51
<archive>
52
<manifestEntries>
53
<Spring-Boot-Native-Processed>true</Spring-Boot-Native-Processed>
54
</manifestEntries>
55
</archive>
56
</configuration>
57
</plugin>
58
```
59
60
### Spring Boot AOT Processing
61
62
Spring Boot Maven plugin configuration for Ahead-of-Time compilation.
63
64
```xml { .api }
65
/**
66
* Spring Boot Maven plugin AOT processing
67
* Generates optimized code and configuration for native compilation
68
*/
69
<plugin>
70
<groupId>org.springframework.boot</groupId>
71
<artifactId>spring-boot-maven-plugin</artifactId>
72
<executions>
73
<execution>
74
<id>process-aot</id>
75
<goals>
76
<goal>process-aot</goal>
77
</goals>
78
</execution>
79
</executions>
80
</plugin>
81
```
82
83
### GraalVM Native Maven Plugin
84
85
Core GraalVM native image plugin configuration for native executable creation.
86
87
```xml { .api }
88
/**
89
* GraalVM native Maven plugin for native image compilation
90
* Requires GraalVM 22.3 or later for Spring Boot native support
91
*/
92
<plugin>
93
<groupId>org.graalvm.buildtools</groupId>
94
<artifactId>native-maven-plugin</artifactId>
95
<extensions>true</extensions>
96
<configuration>
97
<classesDirectory>${project.build.outputDirectory}</classesDirectory>
98
<requiredVersion>22.3</requiredVersion>
99
</configuration>
100
<executions>
101
<execution>
102
<id>add-reachability-metadata</id>
103
<goals>
104
<goal>add-reachability-metadata</goal>
105
</goals>
106
</execution>
107
</executions>
108
</plugin>
109
```
110
111
### Native Test Profile
112
113
Profile for running native tests to verify native compilation compatibility.
114
115
```xml { .api }
116
/**
117
* Native test profile for testing native compilation
118
* Includes JUnit Platform Launcher for native test execution
119
*/
120
<profile>
121
<id>nativeTest</id>
122
<dependencies>
123
<dependency>
124
<groupId>org.junit.platform</groupId>
125
<artifactId>junit-platform-launcher</artifactId>
126
<scope>test</scope>
127
</dependency>
128
</dependencies>
129
<build>
130
<plugins>
131
<!-- Native test plugin configurations -->
132
</plugins>
133
</build>
134
</profile>
135
```
136
137
### Spring Boot Native Test AOT
138
139
Spring Boot plugin configuration for native test AOT processing.
140
141
```xml { .api }
142
/**
143
* Spring Boot Maven plugin for native test AOT processing
144
* Optimizes test code for native test execution
145
*/
146
<plugin>
147
<groupId>org.springframework.boot</groupId>
148
<artifactId>spring-boot-maven-plugin</artifactId>
149
<executions>
150
<execution>
151
<id>process-test-aot</id>
152
<goals>
153
<goal>process-test-aot</goal>
154
</goals>
155
</execution>
156
</executions>
157
</plugin>
158
```
159
160
### Native Test Execution
161
162
GraalVM plugin configuration for executing tests in native mode.
163
164
```xml { .api }
165
/**
166
* GraalVM native plugin for native test execution
167
* Runs tests as native images to verify native compatibility
168
*/
169
<plugin>
170
<groupId>org.graalvm.buildtools</groupId>
171
<artifactId>native-maven-plugin</artifactId>
172
<configuration>
173
<classesDirectory>${project.build.outputDirectory}</classesDirectory>
174
<requiredVersion>22.3</requiredVersion>
175
</configuration>
176
<executions>
177
<execution>
178
<id>native-test</id>
179
<goals>
180
<goal>test</goal>
181
</goals>
182
</execution>
183
</executions>
184
</plugin>
185
```
186
187
## Native Compilation Workflow
188
189
### Development Workflow
190
191
```bash
192
# Regular Spring Boot development
193
mvn spring-boot:run
194
195
# Test for native compatibility
196
mvn -PnativeTest test
197
198
# Build native executable
199
mvn -Pnative native:compile
200
201
# Run native executable
202
./target/my-app
203
```
204
205
### Build Pipeline Integration
206
207
```bash
208
# Full native build with tests
209
mvn clean -Pnative package
210
211
# Native compilation only
212
mvn -Pnative clean compile native:compile
213
214
# Native test execution
215
mvn -PnativeTest clean test
216
217
# Build native executable with custom JVM arguments
218
mvn -Pnative native:compile -Dnative.build.args="--enable-preview,-H:+UnlockExperimentalVMOptions"
219
```
220
221
### Docker Integration
222
223
```dockerfile
224
# Multi-stage build for native executable
225
FROM ghcr.io/graalvm/graalvm-ce:java17 AS builder
226
227
WORKDIR /app
228
COPY pom.xml .
229
COPY src src
230
231
# Build native executable
232
RUN mvn -Pnative clean package
233
234
# Runtime stage
235
FROM scratch
236
COPY --from=builder /app/target/my-app /app
237
ENTRYPOINT ["/app"]
238
```
239
240
## Native Configuration Hints
241
242
### Application Properties for Native
243
244
```properties
245
# Native-specific configuration
246
spring.native.remove-unused-autoconfig=true
247
spring.aot.enabled=true
248
249
# GraalVM native image options
250
spring.graalvm.reachability-metadata.enabled=true
251
```
252
253
### Native Hints in Code
254
255
```java
256
@Configuration
257
@RegisterReflectionForBinding({User.class, Product.class})
258
public class NativeConfiguration {
259
260
@Bean
261
@RegisterReflectionForBinding(DatabaseConfig.class)
262
public DataSource dataSource() {
263
return new HikariDataSource();
264
}
265
}
266
```
267
268
### Runtime Hints
269
270
```java
271
@Component
272
public class MyRuntimeHints implements RuntimeHintsRegistrar {
273
274
@Override
275
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
276
hints.reflection().registerType(MyClass.class,
277
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
278
MemberCategory.INVOKE_DECLARED_METHODS);
279
}
280
}
281
```
282
283
## Performance Characteristics
284
285
### Native vs JVM Comparison
286
287
| Metric | JVM | Native |
288
|--------|-----|--------|
289
| Startup Time | 2-5 seconds | 50-200ms |
290
| Memory Usage | 100-500MB | 20-100MB |
291
| Peak Performance | Higher | Lower |
292
| Build Time | Fast | Slow |
293
294
### Optimization Tips
295
296
```xml
297
<!-- Optimize for size -->
298
<plugin>
299
<groupId>org.graalvm.buildtools</groupId>
300
<artifactId>native-maven-plugin</artifactId>
301
<configuration>
302
<buildArgs>
303
<buildArg>--no-fallback</buildArg>
304
<buildArg>-H:+ReportExceptionStackTraces</buildArg>
305
<buildArg>-H:+PrintGCDetails</buildArg>
306
<buildArg>--enable-preview</buildArg>
307
</buildArgs>
308
</configuration>
309
</plugin>
310
```