Maven docker缓存依赖项 [英] Maven docker cache dependencies

查看:206
本文介绍了Maven docker缓存依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用docker自动化maven构建。我想要构建的项目花了将近20分钟来下载所有依赖项,所以我尝试构建一个可以缓存这些依赖项的docker镜像,但它似乎并没有保存它。我的Dockerfile是

I'm trying to use docker to automate maven builds. The project I want to build takes nearly 20 minutes to download all the dependencies, so I tried to build a docker image that would cache these dependencies, but it doesn't seem to save it. My Dockerfile is

FROM maven:alpine
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
ADD pom.xml /usr/src/app
RUN mvn dependency:go-offline

图像构建,它确实下载了所有内容。但是,生成的图像与基本 maven:alpine 图像的大小相同,因此它似乎没有缓存图像中的依赖项。当我尝试将图像用于 mvn compile 时,它将重新下载所有内容20分钟。

The image builds, and it does download everything. However, the resulting image is the same size as the base maven:alpine image, so it doesn't seem to have cached the dependencies in the image. When I try to use the image to mvn compile it goes through the full 20 minutes of redownloading everything.

是有可能构建一个缓存我的依赖项的maven图像,这样每次我使用图像执行构建时都不必下载吗?

Is it possible to build a maven image that caches my dependencies so they don't have to download everytime I use the image to perform a build?

我正在运行以下命令命令:

I'm running the following commands:

docker build -t my-maven .

docker run -it --rm --name my-maven-project -v "$PWD":/usr/src/mymaven -w /usr/src/mymaven my-maven mvn compile

我的理解是,无论 RUN 在docker构建过程成为结果图像的一部分。

My understanding is that whatever RUN does during the docker build process becomes part of the resulting image.

推荐答案

通常 pom.xml中没有变化文件,但是当您尝试启动docker映像构建时,其他src代码会更改。在这种情况下,您可以这样做:

Usually there's no change in pom.xml file but other src code changes when you're attempting to start docker image build. In such circumstance you can do this:

仅供参考:

FROM registry.cn-hangzhou.aliyuncs.com/acs/maven:3-jdk-8

MAINTAINER Eric Kim <kimjuny@foxmail.com>

ENV PITBULL_EUREKA=/usr/src/app

RUN mkdir -p $PITBULL_EUREKA

WORKDIR $PITBULL_EUREKA

# add pom.xml only here, and download dependency

ADD pom.xml $PITBULL_EUREKA

RUN ["/usr/local/bin/mvn-entrypoint.sh", "mvn", "verify", "clean", "--fail-never"]

# now we can add all source code and start compiling

ADD . $PITBULL_EUREKA

RUN ["mvn", "package"]

EXPOSE 8888

CMD ["java", "-jar", "./target/eureka-0.0.1-SNAPSHOT.jar"]

所以关键是:


  1. 添加 pom.xml 文件。

然后 mvn verify --fail-never 它,它将下载maven依赖项。

then mvn verify --fail-never it, it will download maven dependencies.

然后添加所有源文件,然后开始编译( mvn package )。

add all your source file then, and start your compilation(mvn package).

当您的 pom.xml 发生变化时,或者您是第一次运行此脚本时, docker将执行1 - > 2 - > 3.当 pom.xml 文件中没有更改时,docker将跳过步骤1,2并直接执行3。

When there are changes in your pom.xml or you are running this script for the first time, docker will do 1 -> 2 -> 3. When there are no changes in pom.xml file, docker will skip step 1、2 and do 3 directly.

这个简单的技巧可用于许多其他包管理环境。

This simple trick can be used in many other package management circumstances.

这篇关于Maven docker缓存依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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