前言
若要将一 Spring Boot 项目对不同架构的平台进行适配,是一件相当麻烦的事,需要在不同架构的平台中进行编译测试。近几年,ARM 架构的终端逐渐盛行,特别是华为推出了移动端的麒麟处理器、桌面端的鲲鹏处理器之后,国内从业者对于 ARM 架构下的软件开发正在迅速发展。但是目前终究还是 x86_64 的天下,不少 Spring Boot 项目并未提供对于 ARM 架构平台的支持。
但是在 ARM 平台下进行环境的搭建也比 x86_64 繁琐,可以采用 Docker 容器来进行 ARM 平台下环境的搭建。
拉取 Maven 镜像
在 Docker Hub Official Image 中可以看到类似如下信息。
TAG | ||
---|---|---|
3.8.1-openjdk-17 | docker pull maven:3.8.1-openjdk-17 |
|
Last pushed 8 days ago by doijanky | ||
DIGEST | OS/ARCH | COMPRESSED SIZE |
9c9cadc63144 | linux/amd64 | 399.55 MB |
fc8ec2ca29f9 | linux/arm64/v8 | 378.87 MB |
可以看到 Maven 的 Docker IMAGE 是支持 ARMv8 架构的,通过这个镜像我们就可以进行 ARMv8 平台下的编译。
docker pull maven:3.8.1-openjdk-17
启动容器
docker run -itd --name maven-armv8 maven:[TAG] /bin/bash
[GOAL PATH]
为主机 Spring Boot 项目路径, [CONTAINER TARGET]
容器中数据卷的映射地址。
示例:
bash-5.0# docker run -itd --name maven-armv8 maven /bin/bash
3e144c67f2690ad33f1134cd17040b415362ad75d921a64b7d1162a9520a6cd9
bash-5.0# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3e144c67f269 maven "/usr/local/bin/mvn-…" 28 seconds ago Exited (2) 18 seconds ago maven-armv8
bash-5.0# docker exec -it maven-armv8 /bin/bash
bash-4.4# java --version
openjdk 16.0.1 2021-04-20
OpenJDK Runtime Environment (build 16.0.1+9-24)
OpenJDK 64-Bit Server VM (build 16.0.1+9-24, mixed mode, sharing)
bash-4.4# mvn -V
Apache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d)
Maven home: /usr/share/maven
Java version: 16.0.1, vendor: Oracle Corporation, runtime: /usr/java/openjdk-16
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "5.10.0-4.17.0.8.oe1.raspi.aarch64", arch: "aarch64", family: "unix"
复制项目进容器
docker cp /home/Yogile/docker/Maven/project/nginxWebUI/ maven-armv8:/root/project
编译打包
进入 /root/project/nginxWebUI
目录进行编译打包。
mvn clean package
示例:
bash-4.4# cd /root/project/nginxWebUI
bash-4.4# ls
Dockerfile LICENSE README.md README_EN.md jre.tar.gz nginxWebUI.sh pom.xml src
bash-4.4# mvn clean package
[INFO] Scanning for projects...
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter-parent/2.4.6/spring-boot-starter-parent-2.4.6.pom
......
[INFO]
[INFO] -------------------------< com.cym:nginxWebUI >-------------------------
[INFO] Building nginxWebUI 2.6.4
[INFO] --------------------------------[ jar ]---------------------------------
......
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ nginxWebUI ---
......
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ nginxWebUI ---
......
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] Copying 620 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ nginxWebUI ---
......
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 117 source files to /root/project/nginxWebUI/target/classes
[INFO] /root/project/nginxWebUI/src/main/java/com/cym/service/MonitorService.java: /root/project/nginxWebUI/src/main/java/com/cym/service/MonitorService.java uses or overrides a deprecated API.
[INFO] /root/project/nginxWebUI/src/main/java/com/cym/service/MonitorService.java: Recompile with -Xlint:deprecation for details.
[INFO] /root/project/nginxWebUI/src/main/java/com/cym/utils/BaseController.java: Some input files use unchecked or unsafe operations.
[INFO] /root/project/nginxWebUI/src/main/java/com/cym/utils/BaseController.java: Recompile with -Xlint:unchecked for details.
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ nginxWebUI ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory /root/project/nginxWebUI/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ nginxWebUI ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /root/project/nginxWebUI/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ nginxWebUI ---
......
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ nginxWebUI ---
......
[INFO] Building jar: /root/project/nginxWebUI/target/nginxWebUI-2.6.4.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.4.6:repackage (repackage) @ nginxWebUI ---
......
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 06:15 min
[INFO] Finished at: 2021-07-11T05:21:20Z
[INFO] ------------------------------------------------------------------------
至此,项目编译打包完成。
复制 jar 包出容器
docker cp maven-armv8:/root/project/nginxWebUI/target/*.jar /home/Yogile/docker/Maven/project/ARMv8/