ubuntu22.04 本地编译运行 spring-native 项目

记录一下问题项目创建和解决问题并成功运行的过程


热乎的系统

准备环境

国内环境问题,先换源

╰─$ cat /etc/apt/sources.list                                                           130 ↵
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse

执行更新命令并安装 gcc

╰─$ sudo apt update && sudo apt upgrade -y
╰─$ sudo apt install build-essential -y
╰─$ gcc -v                                
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 11.2.0-19ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-11 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-11-gBFGDP/gcc-11-11.2.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-11-gBFGDP/gcc-11-11.2.0/debian/tmp-gcn/usr --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-serialization=2
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.2.0 (Ubuntu 11.2.0-19ubuntu1) 

使用 sdk 工具安装 jdk 和 maven https://sdkman.io/

╰─$ sdk install java 22.1.0.r11-grl
╰─$ sdk version                                                                         130 ↵
==== BROADCAST =================================================================
* 2022-04-30: jbang 0.93.0 available on SDKMAN! https://github.com/jbangdev/jbang/releases/tag/v0.93.0
* 2022-04-29: micronaut 3.4.3 available on SDKMAN!
* 2022-04-27: kscript 4.0.0 available on SDKMAN!
================================================================================

SDKMAN 5.15.0

╰─$ sdk install java 22.1.0.r11-grl
╰─$ sdk install maven 3.6.3
╰─$ mvn -v                                
Running `/home/stg/hub/native-demo-web/mvnw`...
Apache Maven 3.8.4 (9b656c72d54e5bacbed989b64718c159fe39b537)
Maven home: /home/stg/.m2/wrapper/dists/apache-maven-3.8.4-bin/52ccbt68d252mdldqsfsn03jlf/apache-maven-3.8.4
Java version: 11.0.15, vendor: GraalVM Community, runtime: /home/stg/.sdkman/candidates/java/22.1.0.r11-grl
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "5.15.0-27-generic", arch: "amd64", family: "unix"

# 安装原生镜像打包的工具包
╰─$ gu install native-image

准备项目

去 Spring 官方创建一个 https://start.spring.io/

运行

先普通运行一下


image.png

然后试试打包原生二进制包

╰─$ mvn clean package -DskipTests -Pnative                                                              130 ↵

报错

/usr/bin/ld: 找不到 -lz: 没有那个文件或目录
collect2: error: ld returned 1 exit status
    at com.oracle.svm.hosted.image.NativeImageViaCC.handleLinkerFailure(NativeImageViaCC.java:505)
    at com.oracle.svm.hosted.image.NativeImageViaCC.write(NativeImageViaCC.java:452)
    at com.oracle.svm.hosted.NativeImageGenerator.doRun(NativeImageGenerator.java:695)
    at com.oracle.svm.hosted.NativeImageGenerator.run(NativeImageGenerator.java:515)
    at com.oracle.svm.hosted.NativeImageGeneratorRunner.buildImage(NativeImageGeneratorRunner.java:407)
    at com.oracle.svm.hosted.NativeImageGeneratorRunner.build(NativeImageGeneratorRunner.java:585)
    at com.oracle.svm.hosted.NativeImageGeneratorRunner.main(NativeImageGeneratorRunner.java:128)
    at com.oracle.svm.hosted.NativeImageGeneratorRunner$JDK9Plus.main(NativeImageGeneratorRunner.java:615)
------------------------------------------------------------------------------------------------------------------------
                       9.9s (15.7% of total time) in 50 GCs | Peak RSS: 7.04GB | CPU load: 10.43

搜了下安装一个包可以解决

╰─$ sudo apt-get install zlib1g-dev 

再次执行打包

╰─$ mvn clean package -DskipTests -Pnative   
******中间省略很长的过程******
                       10.8s (16.5% of total time) in 51 GCs | Peak RSS: 7.17GB | CPU load: 10.60
------------------------------------------------------------------------------------------------------------------------
Produced artifacts:
 /home/stg/hub/native-demo-web/target/native-demo-web (executable)
 /home/stg/hub/native-demo-web/target/native-demo-web.build_artifacts.txt
========================================================================================================================
Finished generating 'native-demo-web' in 1m 4s.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  01:14 min
[INFO] Finished at: 2022-05-02T03:12:31+08:00
[INFO] ------------------------------------------------------------------------
╰─$ ll -h target                          
总用量 92M
drwxrwxr-x 5 stg stg 4.0K  5月  2 03:40 classes
drwxrwxr-x 3 stg stg 4.0K  5月  2 03:40 generated-runtime-sources
drwxrwxr-x 3 stg stg 4.0K  5月  2 03:40 generated-sources
drwxrwxr-x 3 stg stg 4.0K  5月  2 03:40 generated-test-sources
drwxrwxr-x 2 stg stg 4.0K  5月  2 03:40 maven-archiver
drwxrwxr-x 3 stg stg 4.0K  5月  2 03:40 maven-status
-rwxrwxr-x 1 stg stg  70M  5月  2 03:41 native-demo-web
-rw-rw-r-- 1 stg stg  22M  5月  2 03:40 native-demo-web-0.0.1-SNAPSHOT-exec.jar
-rw-rw-r-- 1 stg stg  89K  5月  2 03:40 native-demo-web-0.0.1-SNAPSHOT.jar
-rw-rw-r-- 1 stg stg   30  5月  2 03:41 native-demo-web.build_artifacts.txt
drwxrwxr-x 4 stg stg 4.0K  5月  2 03:40 test-classes
╰─$ ./target/native-demo-web                                                                              1 ↵
2022-05-02 03:43:05.540  INFO 35565 --- [           main] o.s.nativex.NativeListener               : AOT mode enabled

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.6.7)

2022-05-02 03:43:05.541  INFO 35565 --- [           main] o.s.boot.SpringApplication               : Starting application using Java 11.0.15 on Z3Air with PID 35565 (started by stg in /home/stg/hub/native-demo-web)
2022-05-02 03:43:05.541  INFO 35565 --- [           main] o.s.boot.SpringApplication               : No active profile set, falling back to 1 default profile: "default"
2022-05-02 03:43:05.552  INFO 35565 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-05-02 03:43:05.552  INFO 35565 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-05-02 03:43:05.552  INFO 35565 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.62]
2022-05-02 03:43:05.556  INFO 35565 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-05-02 03:43:05.556  INFO 35565 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 15 ms
2022-05-02 03:43:05.569  INFO 35565 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-05-02 03:43:05.569  INFO 35565 --- [           main] o.s.boot.SpringApplication               : Started application in 0.034 seconds (JVM running for 0.035)

从 1.433s 降低到 0.035s 确实有点东西

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容