Maven/Docker: cache all dependencies

2020-03-30 03:07发布

I'm trying to build/deploy a spring boot in a docker container.

FROM maven:3.5.3-jdk-8-slim AS build
COPY ./pom.xml /app/pom.xml
RUN cd /app
RUN mvn -f /app/pom.xml -s /usr/share/maven/ref/settings-docker.xml dependency:go-offline dependency:resolve-plugins -B
COPY . /app
RUN mvn -f /app/pom.xml -s /usr/share/maven/ref/settings-docker.xml --batch-mode package -DskipTests

As you can see, I'm caching all dependencies with the first mvn command so that every change in my code app will not trigger a new bulk of dependencies downloads. It works for most of the dependencies but some are still downloaded (even if cached). This is the log of the second mvn command (package):

[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------------< xxx:xxx >----------------------
[INFO] Building xxxx 0.0.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- apt-maven-plugin:1.1.3:process (default) @ vsol-java ---
[INFO] Downloading from spring-releases: https://repo.spring.io/libs-release/org/apache/commons/commons-io/1.3.2/commons-io-1.3.2.pom
[INFO] Downloaded from spring-releases: https://repo.spring.io/libs-release/org/apache/commons/commons-io/1.3.2/commons-io-1.3.2.pom (0 B at 0 B/s)
[INFO] Downloading from spring-releases: https://repo.spring.io/libs-release/commons-io/commons-io/1.3.2/commons-io-1.3.2.pom
[INFO] Downloaded from spring-releases: https://repo.spring.io/libs-release/commons-io/commons-io/1.3.2/commons-io-1.3.2.pom (0 B at 0 B/s)
[INFO] Downloading from spring-releases: https://repo.spring.io/libs-release/org/apache/commons/commons-parent/3/commons-parent-3.pom
[INFO] Downloaded from spring-releases: https://repo.spring.io/libs-release/org/apache/commons/commons-parent/3/commons-parent-3.pom (0 B at 0 B/s)
[INFO] Downloading from spring-releases: https://repo.spring.io/libs-release/org/sonatype/plexus/plexus-build-api/0.0.7/plexus-build-api-0.0.7.jar
...

(BTW, the (0 B at 0 B/s) is a little bit strange...just a check?)

If I launch a container based on the step after the first maven command (mvn dependency:...) (the one that should cached all dependencies)

root@3281a837a236:/usr/share/maven/ref/repository# ls -lh org/codehaus/plexus/plexus-utils/1.5.15
total 244K
-rw-r--r-- 1 root root  202 Oct 19 12:07 _remote.repositories
-rw-r--r-- 1 root root 223K Oct 19 12:07 plexus-utils-1.5.15.jar
-rw-r--r-- 1 root root   40 Oct 19 12:07 plexus-utils-1.5.15.jar.sha1
-rw-r--r-- 1 root root 6.7K Oct 19 12:07 plexus-utils-1.5.15.pom
-rw-r--r-- 1 root root   40 Oct 19 12:07 plexus-utils-1.5.15.pom.sha1

The lib seems to be there but, I can see this in the log from mvn package:

[INFO] Downloading from spring-releases: https://repo.spring.io/libs-release/org/codehaus/plexus/plexus-utils/1.5.15/plexus-utils-1.5.15.jar

If I run mvn package in offline mode, it fails because it can't reach https://repo.spring.io/libs-release.

So it looks like it is cached but maven still try to download this file. I've tried this in my pom.xml

        <pluginRepository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>never</updatePolicy>
            </snapshots>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>never</updatePolicy>
            </releases>
        </pluginRepository>

But no effect.

Any idea? Thanks!

1条回答
Anthone
2楼-- · 2020-03-30 03:46

I finally find the solution by using go-offline-maven-plugin.

         <plugin>
            <groupId>de.qaware.maven</groupId>
            <artifactId>go-offline-maven-plugin</artifactId>
            <version>1.0.0</version>
            <configuration>
                <dynamicDependencies>
                    <DynamicDependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit4</artifactId>
                        <version>2.20.1</version>
                        <repositoryType>PLUGIN</repositoryType>
                    </DynamicDependency>
                </dynamicDependencies>
            </configuration>
        </plugin>

And trying to get all dependencies using:

mvn de.qaware.maven:go-offline-maven-plugin:resolve-dependencies
查看更多
登录 后发表回答