如何在 Docker 多阶段构建层中缓存 Maven 依赖项和插件? [英] How can I cache Maven dependencies and plugins in a Docker Multi Stage Build Layer?

查看:138
本文介绍了如何在 Docker 多阶段构建层中缓存 Maven 依赖项和插件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 Docker 多阶段构建.

我的 Dockerfile 如下所示:

My Dockerfile looks as follows:

FROM maven:3-jdk-8 as mvnbuild
RUN mkdir -p /opt/workspace
WORKDIR /opt/workspace
COPY pom.xml .
RUN mvn -B -s /usr/share/maven/ref/settings-docker.xml dependency:resolve
COPY . .
RUN mvn -B -s /usr/share/maven/ref/settings-docker.xml package

FROM openjdk:8-jre-alpine
...

```

我基于 Docker 多阶段构建博客中提供的示例创建了这个 Dockerfile发布(也可以在Github).

当我运行构建时,我没有看到 dependency:resolve 下载一次然后被 package 重新使用的依赖项,而是看到两者都下载了依赖项步骤.

When I run the build, instead of seeing the dependencies downloaded once by dependency:resolve and then re-used by package, I am seeing the dependencies downloaded by both steps.

有人搞定这个吗?我在这里做错了什么?

Has anyone got this working? What am I doing wrong here?

推荐答案

我遇到了同样的问题.我发现这是由于 Maven 目标之间的差异(例如 dependency:resolvedependency:resolve-plugin).基本上,dependency:resolve 用于应用程序库,dependency:resolve-plugin 用于插件库.因此,库会在两个 RUN 步骤中下载.

I came across the same question. I found out it's due to differences between Maven targets (e.g. dependency:resolve vs dependency:resolve-plugin). Basically, dependency:resolve is for application libraries, dependency:resolve-plugin is for plugin libraries. Hence, libraries are downloaded in both RUN steps.

dependency:resolve 告诉 Maven 解决所有依赖项和显示版本.JAVA 9 注意:将显示模块名称时使用 Java 9 运行.

dependency:resolve tells Maven to resolve all dependencies and displays the version. JAVA 9 NOTE: will display the module name when running with Java 9.

dependency:resolve-plugins 告诉 Maven 解析插件及其依赖项.

dependency:resolve-plugins tells Maven to resolve plugins and their dependencies.

https://maven.apache.org/plugins/maven-依赖插件/index.html

即使使用 dependency:resolve-plugins,Maven 也不会下载所有必需的库,因为 package 是一个内置目标并且需要额外的库,这些库 dependency:resolve-plugin 在第一次运行时不知道要解析.我也试过 dependency:go-offline 没有成功.

Even with dependency:resolve-plugins, Maven will not download all required libraries as package is a built-in target and requires additional libraries which dependency:resolve-plugin won't know to resolve in the first RUN. I also tried dependency:go-offline without success.

一种解决方案是在将代码添加到构建映像之前和之后运行构建目标.这会将所有插件依赖项拉入较低层,从而允许它们被重用.

One solution is to run your build targets before and after adding your code to the build image. This will pull all the plugin dependencies into the lower layer allowing them to be re-used.

将此解决方案应用于上述示例如下:

Applying this solution to your example above is as follows:

FROM maven:3-jdk-8 as mvnbuild
RUN mkdir -p /opt/workspace
WORKDIR /opt/workspace
COPY pom.xml .
RUN mvn -B -s /usr/share/maven/ref/settings-docker.xml dependency:resolve-plugins dependency:resolve clean package
COPY . .
RUN mvn -B -s /usr/share/maven/ref/settings-docker.xml clean package

FROM openjdk:8-jre-alpine

这篇关于如何在 Docker 多阶段构建层中缓存 Maven 依赖项和插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆