java springboot3.5 使用graalvm 打包成本地二进制可执行文件
参考链接https://graalvm.github.io/native-build-tools/latest/end-to-end-maven-guide.html
1.在pom.xml 下面加入如下
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<!-- <version>${native.maven.plugin.version}</version>-->
<extensions>true</extensions>
<executions>
<execution>
<id>build-native</id>
<goals>
<goal>compile-no-fork</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
<configuration>
<imageName>toTraditional</imageName>
<mainClass>org.example.graam.GraamApplication</mainClass>
</configuration>
</plugin>
<profiles>
<profile>
<id>native</id>
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<!-- <version>${native.maven.plugin.version}</version>-->
<extensions>true</extensions>
<executions>
<execution>
<id>build-native</id>
<goals>
<goal>compile-no-fork</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
<configuration>
<mainClass>org.example.graam.GraamApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
2.刷新maven 勾选native

3.插件直接打包无效,运行会报aot mode 启动失败,找不到启动类org.springframework.boot.AotInitializerNotFoundException: Startup with AOT mode enabled failed: AOT initializer org.example.convertfont.ConvertFontApplication__ApplicationContextInitializer could not be found


4.使用命令行进行打包 mvn -Pnative package


5.native 对于使用了反射或者动态代理等包需要特殊处理,
java -agentlib:native-image-agent=config-output-dir=./agentlib,config-write-period-secs=60,config-write-initial-delay-secs=5 -jar .\target\graam-0.0.1-SNAPSHOT.jar
访问一下相关功能
会在agentlib下面生成用到的反射类,动态代理类等

把json 文件放到 target/classes/meta-int/native-imagex下面 ,重新运行 mvn -Pnative package 生成可执行文件

运行文件如下


浙公网安备 33010602011771号