Configure Maven environment variables
I directly use maven under the IDEA plugins folder
to create a new MAVEN_HOME environment variable
Path environment variable append
%MAVEN_HOME%\bin
Install Visual Studio Community
Because GraalVM needs to call the underlying tool of the operating system, and the underlying tool of Windows is VisualStudio, so we need to download and install VisualStudio first.
DownloadVisual Studio Community
"Visual Studio Community official website download"
After downloading, install it directly
Choose desktop applications and mobile applications (the most important thing is actually the MSVC environment)
Choose English as the language pack, not Chinese.
Installation path (you can not modify it, it is recommended not to change it, but you must remember it!)
then click install
Configure Visual Studio environment variables (important)
Path environment variable addition: Visual Studio installation path
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\bin\Hostx64\x64
Create a new INCLUDE environment variable:
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\include;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22000.0\shared;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22000.0\ucrt;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22000.0\um;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22000.0\winrt;
Create a new lib environment variable:
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\lib\x64;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22000.0\um\x64;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22000.0\ucrt\x64;
Install GraalVM
"GraalVM Official Website Download"
After clicking download, pull down to find GraalVM Community Edition and click it
to find the nearestGraalVM Community Edition XXX Click on Assets(Because mine is a SpringBoot3 project, the initial JDK requires 17, so I downloaded 17) If the download speed is slow, you can use a third-party download tool, such as: Xunlei, etc. Select the following 2 content to
download
graalvm-ce-java17-windows-amd64-X.X.X.zip
native-image-installable-svm-java17-windows-amd64-X.X.X.jar
Configure the JDK environment variable as the environment variable of GraalVM, because GraalVM is JDK
If there are Java environment variables before, replace them with GraalVM
decompressiongraalvm-ce-java17-windows-amd64-X.X.X.zip(GraalVM JDK)
Configure Path
Save, Win+R input CMD to execute java -version test to see if the JDK configuration is successful
Install native-image
Enternative-image-installable-svm-java17-windows-amd64-X.X.X.jarEnter cmd in the address bar for the download directory, and try not to have Chinese in the folder directory.
Make sure to enter the native-image-installable-svm-java17-windows-amd64-XXXjar folder, or enter the path under this folder through CD
gu install --file native-image-installable-svm-java17-windows-amd64-X.X.X.jar
Determine whether the installation is successful
native-image
If successful, the following will be output
Please specify options for native-image building or use --help for more info.
Spring Boot 3
Add spring boot3 project and select JDK as GraalVM JDK17
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.fu</groupId>
<artifactId>spring-boot3-aot-graalvm-native-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 没有跟demo-maven作为父子项目,而是以SpringBoot作为父项目 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.5</version>
<relativePath/>
</parent>
<properties>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- AOT Graalvm native maven 插件 -->
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Build a HelloController.java and SpringBootApplication startup class
@RestController
public class HelloController {
@GetMapping("hello")
public String hello() {
return "Hello World.";
}
}
build exe file
Check native in the configuration file, otherwise the generated exe file will start without problem, but the console will output ERROR information
Then execute native:build to package into exe executable file
Start the XXX.exe file in the target directory (this file is a bit big to be honest~)
Visit localhost:8080/hello to output Hello World.
-P activate Native
It is equivalent to selecting the Native environment for the idea. If the Native environment is not selected for packaging, the console output log will output ERROR information, but the program can run normally.
mvn -Pnative native:build -f pom.xml
Pure command packaging
Go to the command line in the pom.xml file directory and execute cmd
mvn clean
mvn compile
mvn spring-boot:process-aot
mvn -Pnative native:build -f pom.xml