package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
publicclassDemoApplication {
publicstaticvoidmain(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@RestController
publicclassHelloController {
@GetMapping("/hello")
public Stringhello() {
return "Hello, Native Image!";
}
}
}
在 pom.xml 中添加 Native Image 插件配置:
<build>
<plugins>
<plugin>
<groupId>org.graalvm.nativeimage</groupId>
<artifactId>native-image-maven-plugin</artifactId>
<version>${graalvm.version}</version>
<executions>
<execution>
<goals>
<goal>native-image</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
运行以下 Maven 命令来生成原生镜像:
mvn package -Pnative
生成的可执行文件位于 target 目录下,可以直接运行:
./target/demo