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

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

问题描述

我想在我的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
...

```

我将此Dockerfile基于 Docker多阶段构建博客中提供的示例中帖子(也可以在Github ).

I based this Dockerfile on the example provided in the Docker Multi Stage Build blog post (also available on 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- dependency-plugin/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 Multi Stage Build Layer中缓存Maven依赖项和插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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