基础环境
- 和普通jvm一样配置环境
- 安装native-image(可能需要梯子)
gu install native-image
- spring boot: 2.5.6
截止写笔记的时间,spring官方说只支持这个版本 - Idea: 2021.2.3 Ultimate Edition
-
Visual Studio Community 2019
需要安装MSVC组件
代码准备
直接从spring 向导开始,我这里贴一下我的maven配置,略微和自动生成的springboot配置不一致
- 父模块
<?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>
<groupId>xyz.dava.study</groupId>
<artifactId>study-spring</artifactId>
<version>1.0.0</version>
<name>study-spring</name>
<packaging>pom</packaging>
<description>study-spring</description>
<modules>
<module>demo</module>
</modules>
<properties>
<java.version>17</java.version>
<spring.boot.version>2.5.6</spring.boot.version>
<tomcat.version>9.0.54</tomcat.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring.boot.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
</project>
- 子模块
<?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>xyz.dava.study</groupId>
<artifactId>study-spring</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>demo</artifactId>
<version>1.0.0</version>
<name>demo</name>
<description>demo</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-websocket</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.tomcat.experimental</groupId>
<artifactId>tomcat-embed-programmatic</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<exclusions>
<exclusion>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- spring native-->
<dependency>
<groupId>org.springframework.experimental</groupId>
<artifactId>spring-native</artifactId>
<version>0.10.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
</plugin>
<plugin>
<groupId>org.springframework.experimental</groupId>
<artifactId>spring-aot-maven-plugin</artifactId>
<version>0.10.5</version>
<executions>
<execution>
<id>test-generate</id>
<goals>
<goal>test-generate</goal>
</goals>
</execution>
<execution>
<id>generate</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>native</id>
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<version>0.9.4</version>
<executions>
<execution>
<id>test-native</id>
<goals>
<goal>test</goal>
</goals>
<phase>test</phase>
</execution>
<execution>
<id>build-native</id>
<goals>
<goal>build</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
<configuration>
<!-- ... -->
</configuration>
</plugin>
<!-- Avoid a clash between Spring Boot repackaging and native-maven-plugin -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
打包
命令:
mvn -Pnative -DskipTests package
可能出现的问题
- 类似如下错误
Error: Error compiling query code (in C:\Users\h6706\AppData\Local\Temp\SVM-5837852297530533986\JNIHeaderDirectives.c). Compiler command ''C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.16.27023\bin\HostX64\x64\cl.exe' /WX /W4 /wd4244 /wd4245 /wd4800 /wd4804 /wd4214 '-ID:\kaifa_environment\jdk\graalvm-ce-java11-21.0.0.2\include\win32' '/FeC:\Users\h6706\AppData\Local\Temp\SVM-5837852297530533986\JNIHeaderDirectives.exe' 'C:\Users\h6706\AppData\Local\Temp\SVM-5837852297530533986\JNIHeaderDirectives.c' ' output included error: [JNIHeaderDirectives.c, C:\Users\h6706\AppData\Local\Temp\SVM-5837852297530533986\JNIHeaderDirectives.c(1): fatal error C1083: �������ļ�: ��stdio.h��: No such file or directory]
缺少某个c源代码的时候,可以尝试先运行下面的代码再进行打包:
call "你的vs的安装文件夹\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat"
- 如果出现类似找不到cl.exe的时候,可以手动将cl.exe所在目录放到path环境变量中
你的vs的安装文件夹\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.16.27023\bin\HostX64\x64
- 注意每次修改了环境变量之后都要重新打开一下命令行,也要重新执行一下第一步的代码,再进行打包。
- 不建议使用idea内置的maven进行打包,因为idea的环境变量不会因为系统的更改而自动修改,非常麻烦,想要彻底修改还需要重启电脑(好像注销也行)
最后祝君一切顺利
足足跑了23分钟啊