• GreaalVM编译springboot编译springboot


    GreaalVM编译springboot编译springboot

    原文转自: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

    下载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
    
    • 1

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xF1oqwjl-1668900865620)(/upload/2022/11/image-1668900176637.png)]
    我已经安装好了
    在这里插入图片描述

    创建springboot项目

    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>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157

    编写一个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());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    运行项目无任何报错

    GreaalVM打包

    在window下,打包钱需要配置MSVC。
    我电脑中已经下载了 Visual Studio 2022 并且已经安装了最新的MSVC
    在这里插入图片描述

    接下来配置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;
    
    • 1
    • 2

    你可以复制替换系统版本和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
    
    • 1
    • 2

    你可以复制替换系统版本和MSVC位置

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2P8702MF-1668900865623)(/upload/2022/11/image-1668899880117.png)]
    在这里插入图片描述

    编译打包springboot项目

    执行打包命令

    mvn -Pnative -DskipTests package
    
    • 1

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(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)]
    在这里插入图片描述

  • 相关阅读:
    java url编码 解码
    HarmonyOS 自定义页面请求与前端页面调试
    植物大战 string——C++
    iOS-设置指定边圆角(左上、左下等)
    java惠生活网站计算机毕业设计MyBatis+系统+LW文档+源码+调试部署
    力扣 1480. 一维数组的动态和 383. 赎金信412. Fizz Buzz
    pageoffice打开excel文件变成了打开本地~tempXXXX-Excel文件
    MySQL知识详细汇总
    协程Part1-boost.Coroutine.md
    ETX 高性能远程桌面访问方案在医疗保健保健行业的应用案例
  • 原文地址:https://blog.csdn.net/weixin_44480167/article/details/127944838