1. 程式人生 > 其它 >Docker 搭建 ARM 下 Maven 環境

Docker 搭建 ARM 下 Maven 環境

前言

若要將一 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/