原文转自:https://lingkang.top/archives/greaalvm%E7%BC%96%E8%AF%91springboot
https://lingkang.top/archives/greaalvm%E7%BC%96%E8%AF%91springboot
window下使用GreaalVM编译springboot存在很多坑,主要是配置MSVC
可以参考spring官网:https://docs.spring.io/spring-native/docs/0.12.1/reference/htmlsingle/#_build_the_native_application_2
下载GreaalVM地址:https://github.com/graalvm/graalvm-ce-builds/releases
2022年11月20日最新版22.3.0
,所属的openjdk版本是17.0.5
解压到C:\Program Files\graalvm-ce-java17-22.3.0
配置它到环境变量:(替换原jdk环境变量)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-k1jZs0aa-1668900865618)(/upload/2022/11/image-1668899567832.png)]
version
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZAoXRcJE-1668900865620)(/upload/2022/11/image-1668899599488.png)]
安装编译工具
gu install native-image
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xF1oqwjl-1668900865620)(/upload/2022/11/image-1668900176637.png)]
我已经安装好了
springboot项目是2.7.5,使用IDEA创建springboot项目,删除测试文件。注意某些依赖无法打包源生
Maven如下:
<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.7.5version>
<relativePath/>
parent>
<groupId>top.lingkanggroupId>
<artifactId>graalvm-demoartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>graalvm-demoname>
<description>graalvm-demodescription>
<properties>
<java.version>17java.version>
<repackage.classifier/>
<spring-native.version>0.12.1spring-native.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.experimentalgroupId>
<artifactId>spring-nativeartifactId>
<version>${spring-native.version}version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
exclude>
excludes>
<classifier>${repackage.classifier}classifier>
<image>
<builder>paketobuildpacks/builder:tinybuilder>
<env>
<BP_NATIVE_IMAGE>trueBP_NATIVE_IMAGE>
env>
image>
configuration>
plugin>
<plugin>
<groupId>org.springframework.experimentalgroupId>
<artifactId>spring-aot-maven-pluginartifactId>
<version>${spring-native.version}version>
<executions>
<execution>
<id>test-generateid>
<goals>
<goal>test-generategoal>
goals>
execution>
<execution>
<id>generateid>
<goals>
<goal>generategoal>
goals>
execution>
executions>
plugin>
plugins>
build>
<repositories>
<repository>
<id>spring-releasesid>
<name>Spring Releasesname>
<url>https://repo.spring.io/releaseurl>
<snapshots>
<enabled>falseenabled>
snapshots>
repository>
repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releasesid>
<name>Spring Releasesname>
<url>https://repo.spring.io/releaseurl>
<snapshots>
<enabled>falseenabled>
snapshots>
pluginRepository>
pluginRepositories>
<profiles>
<profile>
<id>nativeid>
<properties>
<repackage.classifier>execrepackage.classifier>
<native-buildtools.version>0.9.13native-buildtools.version>
properties>
<dependencies>
<dependency>
<groupId>org.junit.platformgroupId>
<artifactId>junit-platform-launcherartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtoolsgroupId>
<artifactId>native-maven-pluginartifactId>
<version>${native-buildtools.version}version>
<extensions>trueextensions>
<executions>
<execution>
<id>test-nativeid>
<phase>testphase>
<goals>
<goal>testgoal>
goals>
execution>
<execution>
<id>build-nativeid>
<phase>packagephase>
<goals>
<goal>buildgoal>
goals>
execution>
executions>
plugin>
plugins>
build>
profile>
profiles>
project>
编写一个controller
package top.lingkang.graalvmdemo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author lingkang
* Created by 2022/11/20
*/
@RestController
public class WebController {
private SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@GetMapping("/")
public Object index(){
return "hi, "+format.format(new Date());
}
}
运行项目无任何报错
在window下,打包钱需要配置MSVC。
我电脑中已经下载了 Visual Studio 2022 并且已经安装了最新的MSVC
接下来配置MSVC
找到MSVC的安装目录,我的被VS安装到了 C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.31.31103
配置LIB环境变量
C:\Users\Administrator>echo %LIB%
C:\Program Files (x86)\Windows Kits\10\Lib\10.0.19041.0\um\x64;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.19041.0\ucrt\x64;C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.31.31103\bin\Hostx64\x64;C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.31.31103\lib\x64;
你可以复制替换系统版本和MSVC位置
其中10.0.19041.0
为你的window版本,可以去到对应路径C:\Program Files (x86)\Windows Kits\10
中查找
配置INCLUDE环境变量
C:\Users\Administrator>echo %INCLUDE%
C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\ucrt;C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\um;C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\shared;C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.31.31103\include
你可以复制替换系统版本和MSVC位置
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2P8702MF-1668900865623)(/upload/2022/11/image-1668899880117.png)]
执行打包命令
mvn -Pnative -DskipTests package
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4OOMjRiD-1668900865623)(/upload/2022/11/image-1668899963838.png)]
打包期间发现电脑有些卡,等待几分钟
打包成功:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0mrPbazt-1668900865624)(/upload/2022/11/image-1668900016493.png)]
不过大小是不是太大了?
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OFc4EzUk-1668900865624)(/upload/2022/11/image-1668900067384.png)]
打包后运行
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fqI5U0Wf-1668900865624)(/upload/2022/11/image-1668900351467.png)]
对比未打包运行
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Tl5ZDSed-1668900865625)(/upload/2022/11/image-1668900453723.png)]