踩坑Spring Native

基础环境

  • jvm: graalvm-ce-java11-21.3.0
  1. 和普通jvm一样配置环境
  2. 安装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配置不一致

  • 父模块


    4.0.0
    xyz.dava.study
    study-spring
    1.0.0
    study-spring
    pom
    study-spring

    
        demo
    

    
        17
        2.5.6
        9.0.54
    

    
        
            
                org.springframework.boot
                spring-boot-starter-parent
                ${spring.boot.version}
                import
                pom
            
        
    

  • 子模块


    4.0.0
    
        xyz.dava.study
        study-spring
        1.0.0
    
    demo
    1.0.0

    demo
    demo

    
        
            org.springframework.boot
            spring-boot-starter-web
            
                
                    org.apache.tomcat.embed
                    tomcat-embed-core
                
                
                    org.apache.tomcat.embed
                    tomcat-embed-websocket
                
            
        
        
            org.apache.tomcat.experimental
            tomcat-embed-programmatic
            ${tomcat.version}
        
        
            org.springframework.boot
            spring-boot-starter-actuator
            
                
                    io.micrometer
                    micrometer-core
                
            
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            org.springframework.experimental
            spring-native
            0.10.5
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                ${spring.boot.version}
            

            
                org.springframework.experimental
                spring-aot-maven-plugin
                0.10.5
                
                    
                        test-generate
                        
                            test-generate
                        
                    
                    
                        generate
                        
                            generate
                        
                    
                
            
        
    

    
        
            native
            
                
                    
                        org.graalvm.buildtools
                        native-maven-plugin
                        0.9.4
                        
                            
                                test-native
                                
                                    test
                                
                                test
                            
                            
                                build-native
                                
                                    build
                                
                                package
                            
                        
                        
                            
                        
                    
                    
                    
                        org.springframework.boot
                        spring-boot-maven-plugin
                        
                            exec
                        
                    
                
            
        
    

打包

  • 命令: mvn -Pnative -DskipTests package

  • 可能出现的问题

  1. 类似如下错误
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"
  1. 如果出现类似找不到cl.exe的时候,可以手动将cl.exe所在目录放到path环境变量中
你的vs的安装文件夹\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.16.27023\bin\HostX64\x64
  1. 注意每次修改了环境变量之后都要重新打开一下命令行,也要重新执行一下第一步的代码,再进行打包。
  2. 不建议使用idea内置的maven进行打包,因为idea的环境变量不会因为系统的更改而自动修改,非常麻烦,想要彻底修改还需要重启电脑(好像注销也行)

最后祝君一切顺利

足足跑了23分钟啊

打包结果

原创不易,点个赞再走吧

原创不易,点个赞再走吧

原创不易,点个赞再走吧

你可能感兴趣的:(踩坑Spring Native)