一、windows环境下
1.下载graalvm的jdk
https://injdk.cn/
下载windows版本
配置java环境变量,配置过程略
2.下载visual Studio Build Tools
下载地址:https://aka.ms/vs/17/release/vs_BuildTools.exe
安装后选择组件:
其中windows SDK根据实际情况选择
3.编写springboot代码
3.1 maven代码
pox.xml
<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.3</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>native-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>native-demo</name>
<description>native-demo</description>
<properties>
<java.version>21</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>
<repositories><repository>
<id>aliyun</id>
<name>aliyun Repository</name>
<url>https://maven.aliyun.com/repository/public/</url>
</repository>
<repository>
<id>central-repos</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>aliyun</id>
<name>aliyun Repository</name>
<url>https://maven.aliyun.com/repository/public/</url>
</pluginRepository>
<pluginRepository>
<id>central-repos</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2/</url>
</pluginRepository>
</pluginRepositories>
<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>
</plugin>
</plugins>
</build>
</project>
启动类:
package org.example.nativedemo;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
@RestController
public class NativeDemoApplication {@GetMappingpublic String test() {return "success";}public static void main(String[] args) {SpringApplication.run(NativeDemoApplication.class, args);}}
3.2 gradle代码
gradle.build:
plugins {id 'java'id 'org.springframework.boot' version '3.2.3'id 'io.spring.dependency-management' version '1.1.4'id 'org.graalvm.buildtools.native' version '0.9.28'
}group = 'org.example'
version = '0.0.1-SNAPSHOT'java {sourceCompatibility = '21'
}repositories {mavenCentral()
}dependencies {implementation 'org.springframework.boot:spring-boot-starter-web'testImplementation 'org.springframework.boot:spring-boot-starter-test'
}tasks.named('test') {useJUnitPlatform()
}
启动类
package org.example.demonativegradle;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
@RestController
public class DemoNativeGradleApplication {@GetMappingpublic String test() {return "success";}public static void main(String[] args) {SpringApplication.run(DemoNativeGradleApplication.class, args);}}
4.打包
4.1 maven打包
配置maven环境变量(略)
打包:
使用x64 native tools打开命令行
进入项目目录在,执行命令
mvn -Pnative native:compile
打包的二进制包在target下
4.2 gradle打包
使用x64 native tools打开命令行
进入项目目录在,执行命令
gradle nativeCompile
打包的二进制包在build/native/nativeComplie下
二、linux环境下
1.下载graalvm的jdk
https://injdk.cn/
下载linux以及对应架构的版本
配置java环境变量,配置过程略
2.安装依赖库
yum install -y gcc glibc-devel zlib-devel
3.编写springboot代码
与windows下的测试代码一致
4.打包
4.1 maven打包
配置maven环境变量
进入项目目录在,执行命令
mvn -Pnative native:compile
打包的二进制包在target下
4.2 gradle打包
进入项目目录在,执行命令
gradle nativeCompile
打包的二进制包在build/native/nativeComplie下
三、反射、序列化等问题处理
工程中如果有反射、序列化等,打的二进制包运行时会出问题,需要进行处理。
首先将springboot项目打jar包(gradle中bootJar,maven中repackage或install等其他方式)
配置graalvm 的jdk环境变量运行jar包
java -Dspring.aot.enabled=true -agentlib:native-image-agent=config-output-dir=./config -jar target/<jar包名>
jar包启动后,尽量在页面或其他测试手段测试所有可能得功能,control+c停止执行后会在jar包同级目录的config重生成一些配置文件,将这些配置文件拷贝到项目的resource/META-INF/native-image下
再执行native打包即可