9

I'm running tests in a Docker container that uses the following Dockerfile:

FROM maven:3.5.3-jdk-8
ENV APP_DIR=/app
RUN mkdir -p $APP_DIR
WORKDIR $APP_DIR
COPY pom.xml .
RUN mvn -B dependency:resolve -Dclassifier=test
COPY . $APP_DIR
RUN mvn -B test-compile compile
ENTRYPOINT ["mvn", "test"]

It does download some dependencies during build process. The problem is when mvn test is running in the Docker container, it downloads some dependencies related to the Surefire plugin. What Maven command could be used to download them during the build? The project is just a basic app created with a wizard with the following pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-app</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
rolve
  • 10,083
  • 4
  • 55
  • 75
synapse
  • 5,588
  • 6
  • 35
  • 65
  • Related question: https://stackoverflow.com/questions/47969389/how-can-i-cache-maven-dependencies-and-plugins-in-a-docker-multi-stage-build-lay – Nikolaos Georgiou Apr 02 '18 at 05:17
  • I'm having the same issue and the "Related question" doesn't seem to solve it. – Phil Ninan Nov 16 '19 at 00:21
  • @PhilNinan : how did you guys solve it eventually? – 500865 Mar 09 '21 at 10:59
  • I did not get this fully working. but you can create a docker image from a container. this would be a bit of a manual process to create the image, run commands so all dependencies are downloaded, and create another image from the container that now has all the dependencies. – Phil Ninan Aug 04 '21 at 16:49

1 Answers1

1

Here's a simple suggestion: Instead of doing

RUN mvn -B dependency:resolve -Dclassifier=test

you could simply do

RUN mvn -B test

Because no source or test files have been added to the container at this point, no actual test will be executed, but most of the required Surefire dependencies will be downloaded. Note: most, not all.

When I tested this with the mentioned JUnit 3 dependency, the number of downloaded dependencies during mvn test was reduced from 22 to 3:

Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit3/2.12.4/surefire-junit3-2.12.4.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-providers/2.12.4/surefire-providers-2.12.4.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit3/2.12.4/surefire-junit3-2.12.4.jar

This is not so bad, maybe.

rolve
  • 10,083
  • 4
  • 55
  • 75