0%

GraalVM初体验

安装

# 下载GraalVM
https://www.graalvm.org/downloads/#

# 解压

# 配置PATH,这里需要将 GraalVM\bin 目录配置到PATH
setx /M PATH "C:\Progra~1\Java\<graalvm>\bin;%PATH%"
# 同时,JAVA_HOME 也需要配置成 GraalVM\bin ,这样才能使用 GraalVM 提供的JDK,否则编译不通过。
setx /M JAVA_HOME "C:\Progra~1\Java\<graalvm>"

# 安装 Visual Studio ,在官网下载最新的Visual Studio 并参考 GraalVM 的说明安装必要的组件。
https://visualstudio.microsoft.com/zh-hans/vs/older-downloads/
https://www.graalvm.org/jdk17/docs/getting-started/windows/#install-visual-studio-build-tools-and-windows-sdk

# 重启机器

# 测试
PS D:\gitee\demo> gu.cmd list
ComponentId              Version             Component name                Stability                     Origin 
---------------------------------------------------------------------------------------------------------------------------------
graalvm                  23.0.5              GraalVM Core                  Supported
native-image             23.0.5              Native Image                  Early adopter

# 如果没有可以在线安装
gu.cmd install native-image

native-image.cmd --version

springboot3 集成 GraalVM

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</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-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- 添加graalvm插件 -->
            <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>

</project>

手动 maven 编译命令,编译成功后在 target 目录下就会生成可执行文件。

mvn clean -Pnative native:compile -DskipTests