Install and use GraalVM+SpringBoot3 tutorial on Windows 11
Spring's official GraalVm related documents: GraalVM Native Image Support
Install GraalVM
download zip file
Open Releases graalvm/graalvm-ce-builds GitHub download the corresponding compressed package of GraalVM according to the JDK version
Configure environment variables
Decompress directly, and replace the value of JAVA_HOME in the environment variable with the installation directory of graalvm
Install Visual Studio Build Tools
You need to use the C language environment, so you need to install Visual Studio Build Tools
打开 Thank you for downloading Visual Studio - Visual Studio
To download, the language pack only needs to check English.
test
Run X64 Native Tools Command Propmt as administrator
If you can't find this, you can start it directly in the installer
cli.exe
# 如果显示的是中文的话,需要修改成英文,通过Visual Studio Installer来修改,语言包,把原来打勾的中文选项去掉,只勾选英文选项
The execution of cl.exe is in Chinese and needs to be switched to English.
Create a springboot3 project
The springboot project can be quickly generated at http://start.spring.io
POM.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot3-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot3-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>19</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
Generate exe executable file
1. Open the X64 Native Tools Command Propmt window and execute:
#先进入到对应的项目目录
cd C:\Users\tanhh\Desktop\springboot3\code\springboot3-demo
# 生成exe命令
mvn -Pnative native:compile
Error one:
Cannot find cl.exe, this may be because Visual Studio Build Tools is not installed, or it may not be executed under the X64 Native Tools Command Propmt window.
Error two:
The following error message appears during execution:
[ERROR] Failed to execute goal org.graalvm.buildtools:native-maven-plugin:0.9.20:compile (default-cli) on project springboot3-demo: Execution of D:\develop\graalvm-ce-java19-22.3.1\bin\native-image.cmd @target\tmp\native-image-9198299026073687163.args returned non-zero result -> [Help 1]
According to the error, I found the file @target\tmp\native-image-9198299026073687163.args, which is the temporary parameter file under the target package
In fact, this is because the language pack was not set to English when installing Visual Studio Build Tools. It can be seen that actually generating and executing the exe is divided into two steps to execute. First, parameters need to be generated. After passing native- image operation execution parameters
native-image @target\tmp\native-image-9198299026073687163.args
Mistake three:
Check the above switch language pack to English
test, start
Build executables Docker with Buildpacks
You can directly build a local executable file into an image file, so that it can run across operating systems. Currently, it only supports 17. If jdk is set to 19, an error will appear.
Buildpacks, similar to Dockerfile image construction technology
Need to install docker and start docker
This method does not need to install GGraalVM on the machine, and the springboot plug-in will use /paketo-buildpacks/native-image to generate executable files and enter them into the container
Related Tutorial: GraalVM Native Image Support
The name of the Docker image cannot appear in capital letters, so we need to set the name of the image
<properties>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-boot-build-image.imageName>springboot3-demo</spring-boot-build-image.imageName>
</properties>
Then execute the command to generate a Docker image.
# 生成docker镜像
mvn -Pnative spring-boot:build-image
# 运行docker镜像,秒启
docker run --rm -p 8080:8080 springboot3-demo:0.0.1-SNAPSHOT
Note: The jdk environment switches to the default jdk17
Build localized jar packages
Spring Boot applications typically use Cloud Native Buildpacks through Maven ( mvn spring-boot:build-image
) or Gradle ( ) integrations. gradle bootBuildImage
However, you can also use pack to convert an AOT-processed Spring Boot executable jar into a native container image.
First, make sure the Docker daemon is available (see Getting Docker for details). If you are using Linux, configure it to allow non-root users.
You'll also need pack
to follow the installation guide on buildpacks.io to install it.
myproject-0.0.1-SNAPSHOT.jar
Assuming an AOT-processed Spring Boot executable jar is built in the directory target
, run:
pack build --builder paketobuildpacks/builder:tiny \
--path target/myproject-0.0.1-SNAPSHOT.jar \
--env 'BP_NATIVE_IMAGE=true' \
my-application:0.0.1-SNAPSHOT