Maven 下线 + 隔离 Docker 多阶段镜像构建
我的答案是针对本地构建或 Dockerized 环境,这与构建 docker 映像的性质不同。这使用 Maven 3.6.3-jdk-8
。
有了这个答案,您就可以确切地知道您的 CI 在下载、编译、测试、打包上花费了多少时间……
最后,还回答了关于 Jira 下线的旧问题https://issues.apache.org/jira/browse/MDEP-82?focusedCommentId=16997793&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment -tabpanel#comment-16997793
- 更新
pom.xml
依赖项
maven-dependency-plugin
surefire-junit-platform
- 调用
go-offline
解析依赖
mvn
使用开关调用任何命令--off-line
设置最新版本的插件
@@ -23,6 +23,9 @@
<junit-jupiter.version>5.5.2</junit-jupiter.version>
<common-io.version>2.6</common-io.version>
<jacoco-maven-plugin.version>0.8.4</jacoco-maven-plugin.version>
+ <!-- https://issues.apache.org/jira/browse/MDEP-82 -->
+ <maven-dependency-plugin.version>3.1.1</maven-dependency-plugin.version>
+ <surefire-junit-platform.version>2.22.2</surefire-junit-platform.version>
<maven-release-plugin.version>2.5.3</maven-release-plugin.version>
<maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version>
<maven-surefire-report-plugin.version>2.22.2</maven-surefire-report-plugin.version>
...
...
<build>
<plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-dependency-plugin</artifactId>
+ <version>${maven-dependency-plugin.version}</version>
+ </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
@@ -135,6 +143,11 @@
<target>${java.version}</target>
</configuration>
</plugin>
+ <plugin>
+ <groupId>org.apache.maven.surefire</groupId>
+ <artifactId>surefire-junit-platform</artifactId>
+ <version>${surefire-junit-platform.version}</version>
+ </plugin>
创建 Go-offline 缓存
FROM maven:3.6.3-jdk-8 AS dependencies-downloaded
...
...
COPY pom.xml /usr/src/app/pom.xml
COPY settings.xml /usr/src/app/settings.xml
WORKDIR /usr/src/app
RUN mvn -f pom.xml -s settings.xml dependency:resolve-plugins dependency:go-offline

使用 --offline 调用编译
- 我们可以重复使用相同的图像进行编译
- 只需要 7 秒,因为没有下载任何内容
FROM dependencies-downloaded AS compile
COPY app /usr/src/app
WORKDIR /usr/src/app
RUN mvn -f pom.xml -s settings.xml compile --offline

使用 --offline 调用测试
- 我们可以重复使用相同的图像进行测试
- 用 18 秒运行测试用例,无需任何下载
FROM compile AS tests
WORKDIR /usr/src/app
RUN mvn -f pom.xml -s settings.xml test --offline

使用 --offline 调用包
- 我们可以为最终的 jar 重用相同的图像
- 甚至跳过在之前的 docker 镜像中运行的测试
- 采取的方式比以前少
FROM tests AS package
WORKDIR /usr/src/app
RUN mvn -f pom.xml -s settings.xml package -Dmaven.test.skip=true --offline

最终的运行时映像是包中的 Docker 映像。
FROM JRE
COPY --from package /usr/src/app/target /bin
...
...