Windows detailed installation and usage tutorial of GraalVM of SpringBoot3

Configure Maven environment variables

I directly use maven under the IDEA plugins folder
to create a new MAVEN_HOME environment variable
insert image description here
Path environment variable append

%MAVEN_HOME%\bin

insert image description here

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.
insert image description here

DownloadVisual Studio Community

"Visual Studio Community official website download"
insert image description here

After downloading, install it directly

insert image description here

Choose desktop applications and mobile applications (the most important thing is actually the MSVC environment)

insert image description here

Choose English as the language pack, not Chinese.

insert image description here

Installation path (you can not modify it, it is recommended not to change it, but you must remember it!)

insert image description here
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;

insert image description here
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;

insert image description here

Install GraalVM

"GraalVM Official Website Download"
insert image description here
After clicking download, pull down to find GraalVM Community Edition and click it
insert image description here
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)
insert image description here
Configure Path
insert image description here

Save, Win+R input CMD to execute java -version test to see if the JDK configuration is successful
insert image description here

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.
insert image description here
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
insert image description here

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
insert image description here
maven
Then execute native:build to package into exe executable file
insert image description here

Start the XXX.exe file in the target directory (this file is a bit big to be honest~)

insert image description here
Visit localhost:8080/hello to output Hello World.
insert image description here

-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
insert image description here

mvn clean
mvn compile
mvn spring-boot:process-aot
mvn -Pnative native:build -f pom.xml

insert image description here

Guess you like

Origin blog.csdn.net/weixin_43933728/article/details/131479472