Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

How to solve reducing maven download dependencies time when using docker to build a maven project?

I have built common mavne dependencies as a base docker image, but when I build a project Dockerfile, it still will download dependencies which will take a long time to build.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Combine go-offline goal of maven-dependency-plugin, maven's offline mode with docker multistage builds.

A reference Dockerfile could be :

# Step : Test and package
FROM maven:3.5.3-jdk-8-alpine as builder
WORKDIR /build
COPY pom.xml .
RUN mvn dependency:go-offline

COPY src/ /build/src/
# -o flag will instruct maven to build on offline mode
RUN mvn -o package

# Step : Package image
FROM openjdk:8-jre-alpine
EXPOSE 4567
CMD exec java $JAVA_OPTS -jar /app/my-app.jar
COPY --from=builder /build/target/*jar-with-dependencies.jar /app/my-app.jar

Invoking dependency:go-offline will fetch required artifacts on container's local repository. Thanks to docker layer caching, this step will be cached so it will be skipped in a new build attempt.

An important note is that copying pom.xml should precede source code copying as a change on pom.xml must trigger a new pull of maven artifacts as project's dependencies may have changed.

Reference

EDIT: Note that depending on your pom.xml, you may face an open Maven Dependency plugin issue on which some dependencies are not fetched from go-offline goal as they should, resulting in build failure. As a workaround, you can try Romain's answer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...