开发环境
名称 | 版本 |
---|---|
操作系统 | Windows 11 X64 |
Visual Studio | Communty 2022 |
GraalVM | graalvm-community-jdk-17.0.9_windows-x64_bin.zip |
Native-Image | native-image-installable-svm-java17-windows-amd64-22.3.2.jar |
概述
/**
* 打包成本地镜像
* 1.打成 Jar 包:注意修改 jar 包内的 MANIFEST.MF 文件,指定 Main-Class 的全类名
* - java -jar xxx.jar 就可以执行。
* - 切换机器,安装 java 环境,默认解释执行,启动速度慢,运行速度慢。
* 2.打成本地镜像(可执行文件):
* - Windows: exe
* - native-image cp 你的jar包/class路径 主类 -o 输出的文件名
* - native-image -cp boot3-15-aot-common-1.0-SNAPSHOT.jar com.atguigu.MainApp -o Haha
* 并不是所有的 Java 代码都能支持本地打包: SpringBoot 保证 Spring 应用的所有程序都能在 AOT 的时候提前告知 GraalVM 怎么处理?
* - 动态能力损失: 反射的代码-动态获取够朝气,反射创建对象、反射调用方法都不可以。
* 解决方案: 额外处理(SpringBoot 提供的了一些注解): 提前告知 GraalVM 会用到的反射的方法、构造器
* - 配置文件损失:
* 解决方案: 额外处理(从相对路径读取、配置中心): 提前告知 GraalVM 配置文件怎么处理
*
* 二进制里面不能包含的,不能动态的,都得提前处理
*
* 不是所有框架都适配了 AOT 特性: Spring 全系列适配 OK
*/
新建 SpringBoot 项目-boot3-16-aot-springboot
新建模块
boot3-16-aot-springboot
创建模块
项 | 值 |
---|---|
名称 | boot3-16-aot-springboot |
位置 | D:\Study-Java\2023\SpringBoot3-Study |
语言 | Java |
类型 | Maven |
组 | com.atguigu |
工件 | boot3-16-aot-springboot |
软件包名称 | com.atguigu.boot3.aot |
JDK | graalvm-17 GraalVM version 17.0.9 |
Java | 17 |
- SpringBoot
3.2.0
- 依赖项目
Developer Tools
-- GraalVM Native Support
Web
-- Spring Web
pom.xml
- 此依赖会自动导入
<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>
新建测试控制器-HelloController
package com.atguigu.boot3.aot.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("hello")
public String hello() {
return "Hello,SpringBoot AOT!";
}
}
生成 Native-Image
使用 Maven 插件生成
- 点击
Maven
-boot3-16-aot-springboot
-生命周期
-clear
- 点击
Maven
-boot3-16-aot-springboot
-生命周期
-compile
- 点击
Maven
-boot3-16-aot-springboot
-插件
-spring-boot
-spring-boot:process-aot
- 生成了下面两个文件夹
- 点击
Maven
-boot3-16-aot-springboot
-native
-native:build
运行报错-Error: Please specify class
'native:build' goal is deprecated. Use 'native:compile-no-fork' instead.
[INFO]
[INFO] ----------------< com.atguigu:boot3-16-aot-springboot >-----------------
[INFO] Building boot3-16-aot-springboot 0.0.1-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- native:0.9.28:build (default-cli) @ boot3-16-aot-springboot ---
[WARNING] 'native:build' goal is deprecated. Use 'native:compile-no-fork' instead.
[INFO] Found GraalVM installation from JAVA_HOME variable.
[INFO] Executing: C:\Program Files\Java\graalvm-community-openjdk-17.0.9+9.1\bin\native-image.cmd @target\tmp\native-image-8889678704731965610.args
Error: Please specify class (or <module>/<mainclass>) containing the main entry point method. (see --help)
配置环境变量
- 配置环境变量后,重启 IDEA
环境变量 | 值 |
---|---|
Path | # 这里的路径和 VS2022 安装路径有关,需要根据自己的情况进行修改 C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.38.33130\bin\Hostx64\x64 |
INCLUDE | C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.38.33130\include;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\shared;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\um;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\winrt # 这里的路径和 VS2022 安装路径有关,需要根据自己的情况进行修改 C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.38.33130\include; # 这里的路径和 Windows 不同版本有关,需要根据自己的情况进行修改 C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\shared; C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt; C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\um; C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\winrt |
lib | C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.38.33130\lib\x64;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\um\x64;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\ucrt\x64 # 这里的路径和 VS2022 安装路径有关,需要根据自己的情况进行修改 C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.38.33130\lib\x64; # 这里的路径和 Windows 不同版本有关,需要根据自己的情况进行修改 C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\um\x64; C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\ucrt\x64 |
再次运行报错-args returned non-zero result
点击 Maven
-boot3-16-aot-springboot
-native
-native:build
报错
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.652 s
[INFO] Finished at: 2024-01-12T09:51:34+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.graalvm.buildtools:native-maven-plugin:0.9.28:build (default-cli) on project boot3-16-aot-springboot: Execution of C:\Program Files\Java\graalvm-community-openjdk-17.0.9+9.1\bin\native-image.cmd @target\tmp\native-image-4544108742480805183.args returned non-zero result -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
这个报错的原因是我的项目目录
D:\luoma\学习\Code\2023\Java\SpringBoot3-Study\boot3-16-aot-springboot
带有中文把项目
SpringBoot3-Study
复制到D:\data\SpringBoot3-Study
运行成功
- 点击
Maven
-boot3-16-aot-springboot
-native
-native:build
运行成功
Produced artifacts:
D:\data\SpringBoot3-Study\boot3-16-aot-springboot\target\boot3-16-aot-springboot.exe (executable)
========================================================================================================================
Finished generating 'boot3-16-aot-springboot' in 1m 28s.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:33 min
[INFO] Finished at: 2024-01-12T10:13:44+08:00
[INFO] ------------------------------------------------------------------------
测试
cmd
进入目录D:\data\SpringBoot3-Study\boot3-16-aot-springboot\target
输入
boot3-16-aot-springboot.exe
D:\data\SpringBoot3-Study\boot3-16-aot-springboot\target>boot3-16-aot-springboot.exe
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.2.1)
%PARSER_ERROR[d] %PARSER_ERROR[p] 23960 --- [%PARSER_ERROR[t]] %PARSER_ERROR[logger] : %PARSER_ERROR[m]%PARSER_ERROR[n]%PARSER_ERROR[d] %PARSER_ERROR[p] 23960 --- [%PARSER_ERROR[t]] %PARSER_ERROR[logger] : %PARSER_ERROR[m]%PARSER_ERROR[n]%PARSER_ERROR[d] %PARSER_ERROR[p] 23960 --- [%PARSER_ERROR[t]] %PARSER_ERROR[logger] : %PARSER_ERROR[m]%PARSER_ERROR[n]%PARSER_ERROR[d] %PARSER_ERROR[p] 23960 --- [%PARSER_ERROR[t]] %PARSER_ERROR[logger] : %PARSER_ERROR[m]%PARSER_ERROR[n]%PARSER_ERROR[d] %PARSER_ERROR[p] 23960 --- [%PARSER_ERROR[t]] %PARSER_ERROR[logger] : %PARSER_ERROR[m]%PARSER_ERROR[n]%PARSER_ERROR[d] %PARSER_ERROR[p] 23960 --- [%PARSER_ERROR[t]] %PARSER_ERROR[logger] : %PARSER_ERROR[m]%PARSER_ERROR[n]%PARSER_ERROR[d] %PARSER_ERROR[p] 23960 --- [%PARSER_ERROR[t]] %PARSER_ERROR[logger] : %PARSER_ERROR[m]%PARSER_ERROR[n]%PARSER_ERROR[d] %PARSER_ERROR[p] 23960 --- [%PARSER_ERROR[t]] %PARSER_ERROR[logger] : %PARSER_ERROR[m]%PARSER_ERROR[n]%PARSER_ERROR[d] %PARSER_ERROR[p] 23960 --- [%PARSER_ERROR[t]] %PARSER_ERROR[logger] : %PARSER_ERROR[m]%PARSER_ERROR[n]
- 访问 http://localhost:8080/hello , 输出
Hello,SpringBoot AOT!
使用 Maven 命令生成
- 点击
Maven
-boot3-16-aot-springboot
-生命周期
-clear
- 点击
Maven
-boot3-16-aot-springboot
-生命周期
-compile
- 点击
Maven
-boot3-16-aot-springboot
-插件
-spring-boot
-spring-boot:process-aot
- 生成了下面两个文件夹
- 打开工具
x64 Native Tools Command Prompt for VS 2022
进行项目目录
D:\data\SpringBoot3-Study\boot3-16-aot-springboot>
运行 native 打包
# 推荐加上 -Pnative
mvn -Pnative native:build -f pom.xml
或
mvn native:build -f pom.xml
打包成功
Produced artifacts:
D:\data\SpringBoot3-Study\boot3-16-aot-springboot\target\boot3-16-aot-springboot.exe (executable)
========================================================================================================================
Finished generating 'boot3-16-aot-springboot' in 1m 29s.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:34 min
[INFO] Finished at: 2024-01-12T10:23:29+08:00
[INFO] ------------------------------------------------------------------------
测试
cmd
进入目录D:\data\SpringBoot3-Study\boot3-16-aot-springboot\target
输入
boot3-16-aot-springboot.exe
D:\data\SpringBoot3-Study\boot3-16-aot-springboot\target>boot3-16-aot-springboot.exe
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.2.1)
2024-01-12T10:24:02.354+08:00 INFO 1756 --- [ main] c.a.b.a.Boot316AotSpringbootApplication : Starting AOT-processed Boot316AotSpringbootApplication using Java 17.0.9 with PID 1756 (D:\data\SpringBoot3-Study\boot3-16-aot-springboot\target\boot3-16-aot-springboot.exe started by v_hwhao in D:\data\SpringBoot3-Study\boot3-16-aot-springboot\target)
2024-01-12T10:24:02.355+08:00 INFO 1756 --- [ main] c.a.b.a.Boot316AotSpringbootApplication : No active profile set, falling back to 1 default profile: "default"
2024-01-12T10:24:02.429+08:00 INFO 1756 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http)
2024-01-12T10:24:02.430+08:00 INFO 1756 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2024-01-12T10:24:02.431+08:00 INFO 1756 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.17]
2024-01-12T10:24:02.445+08:00 INFO 1756 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2024-01-12T10:24:02.445+08:00 INFO 1756 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 90 ms
2024-01-12T10:24:02.476+08:00 INFO 1756 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path ''
2024-01-12T10:24:02.476+08:00 INFO 1756 --- [ main] c.a.b.a.Boot316AotSpringbootApplication : Started Boot316AotSpringbootApplication in 0.133 seconds (process running for 0.139)
- 访问 http://localhost:8080/hello , 输出
Hello,SpringBoot AOT!