From 185266113c7d623faa8bee79aeb8de4578719cc1 Mon Sep 17 00:00:00 2001 From: msavic Date: Sun, 13 Feb 2022 23:56:05 +0100 Subject: [PATCH] Caching Maven Dependencies with Docker --- docker/docker-caching/multi-module/Dockerfile | 21 ++++++++ .../multi-module/Dockerfile-Buildkit | 13 +++++ .../multi-module/application/pom.xml | 36 +++++++++++++ .../MavenCachingApplication.java | 15 ++++++ .../src/main/resources/application.properties | 1 + .../MavenCachingApplicationTests.java | 13 +++++ .../docker-caching/multi-module/core/pom.xml | 19 +++++++ .../com/baeldung/maven_caching/CoreClass.java | 8 +++ docker/docker-caching/multi-module/pom.xml | 38 +++++++++++++ .../docker-caching/single-module/Dockerfile | 15 ++++++ .../single-module/Dockerfile-Buildkit | 13 +++++ docker/docker-caching/single-module/pom.xml | 53 +++++++++++++++++++ .../maven_caching/MavenCachingMain.java | 11 ++++ 13 files changed, 256 insertions(+) create mode 100644 docker/docker-caching/multi-module/Dockerfile create mode 100644 docker/docker-caching/multi-module/Dockerfile-Buildkit create mode 100644 docker/docker-caching/multi-module/application/pom.xml create mode 100644 docker/docker-caching/multi-module/application/src/main/java/com/baeldung/maven_caching/MavenCachingApplication.java create mode 100644 docker/docker-caching/multi-module/application/src/main/resources/application.properties create mode 100644 docker/docker-caching/multi-module/application/src/test/java/com/baeldung/maven_caching/MavenCachingApplicationTests.java create mode 100644 docker/docker-caching/multi-module/core/pom.xml create mode 100644 docker/docker-caching/multi-module/core/src/main/java/com/baeldung/maven_caching/CoreClass.java create mode 100644 docker/docker-caching/multi-module/pom.xml create mode 100644 docker/docker-caching/single-module/Dockerfile create mode 100644 docker/docker-caching/single-module/Dockerfile-Buildkit create mode 100644 docker/docker-caching/single-module/pom.xml create mode 100644 docker/docker-caching/single-module/src/main/java/com/baeldung/maven_caching/MavenCachingMain.java diff --git a/docker/docker-caching/multi-module/Dockerfile b/docker/docker-caching/multi-module/Dockerfile new file mode 100644 index 0000000000..1f0cc85f7c --- /dev/null +++ b/docker/docker-caching/multi-module/Dockerfile @@ -0,0 +1,21 @@ +FROM maven:alpine as build +ENV HOME=/usr/app +RUN mkdir -p $HOME +WORKDIR $HOME + +ADD pom.xml $HOME +ADD core/pom.xml $HOME/core/pom.xml +ADD application/pom.xml $HOME/application/pom.xml + +RUN mvn -pl core verify --fail-never +ADD core $HOME/core +RUN mvn -pl core install +RUN mvn -pl application verify --fail-never +ADD application $HOME/application +RUN mvn -pl core,application package + +FROM openjdk:8-jdk-alpine + +COPY --from=build /usr/app/application/target/application-0.0.1-SNAPSHOT.jar /app/runner.jar + +ENTRYPOINT java -jar /app/runner.jar \ No newline at end of file diff --git a/docker/docker-caching/multi-module/Dockerfile-Buildkit b/docker/docker-caching/multi-module/Dockerfile-Buildkit new file mode 100644 index 0000000000..f51657c9ce --- /dev/null +++ b/docker/docker-caching/multi-module/Dockerfile-Buildkit @@ -0,0 +1,13 @@ +FROM maven:alpine as build +ENV HOME=/usr/app +RUN mkdir -p $HOME +WORKDIR $HOME + +ADD . $HOME +RUN --mount=type=cache,target=/root/.m2 mvn -f $HOME/pom.xml clean package + +FROM openjdk:8-jdk-alpine + +COPY --from=build /usr/app/application/target/application-0.0.1-SNAPSHOT.jar /app/runner.jar + +ENTRYPOINT java -jar /app/runner.jar \ No newline at end of file diff --git a/docker/docker-caching/multi-module/application/pom.xml b/docker/docker-caching/multi-module/application/pom.xml new file mode 100644 index 0000000000..49611e7fbd --- /dev/null +++ b/docker/docker-caching/multi-module/application/pom.xml @@ -0,0 +1,36 @@ + + + + maven-caching + com.baeldung + 0.0.1-SNAPSHOT + + 4.0.0 + + application + + + com.baeldung + core + 0.0.1-SNAPSHOT + + + + + 8 + 8 + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + \ No newline at end of file diff --git a/docker/docker-caching/multi-module/application/src/main/java/com/baeldung/maven_caching/MavenCachingApplication.java b/docker/docker-caching/multi-module/application/src/main/java/com/baeldung/maven_caching/MavenCachingApplication.java new file mode 100644 index 0000000000..dec5453dc9 --- /dev/null +++ b/docker/docker-caching/multi-module/application/src/main/java/com/baeldung/maven_caching/MavenCachingApplication.java @@ -0,0 +1,15 @@ +package com.baeldung.maven_caching; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class MavenCachingApplication { + + public static void main(String[] args) { + SpringApplication.run(MavenCachingApplication.class, args); + CoreClass cc = new CoreClass(); + System.out.println(cc.method()); + } + +} diff --git a/docker/docker-caching/multi-module/application/src/main/resources/application.properties b/docker/docker-caching/multi-module/application/src/main/resources/application.properties new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/docker/docker-caching/multi-module/application/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/docker/docker-caching/multi-module/application/src/test/java/com/baeldung/maven_caching/MavenCachingApplicationTests.java b/docker/docker-caching/multi-module/application/src/test/java/com/baeldung/maven_caching/MavenCachingApplicationTests.java new file mode 100644 index 0000000000..ec550fb990 --- /dev/null +++ b/docker/docker-caching/multi-module/application/src/test/java/com/baeldung/maven_caching/MavenCachingApplicationTests.java @@ -0,0 +1,13 @@ +package com.baeldung.maven_caching; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class MavenCachingApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/docker/docker-caching/multi-module/core/pom.xml b/docker/docker-caching/multi-module/core/pom.xml new file mode 100644 index 0000000000..5df8ea7dde --- /dev/null +++ b/docker/docker-caching/multi-module/core/pom.xml @@ -0,0 +1,19 @@ + + + + maven-caching + com.baeldung + 0.0.1-SNAPSHOT + + 4.0.0 + + core + + + 8 + 8 + + + \ No newline at end of file diff --git a/docker/docker-caching/multi-module/core/src/main/java/com/baeldung/maven_caching/CoreClass.java b/docker/docker-caching/multi-module/core/src/main/java/com/baeldung/maven_caching/CoreClass.java new file mode 100644 index 0000000000..a33624d4e7 --- /dev/null +++ b/docker/docker-caching/multi-module/core/src/main/java/com/baeldung/maven_caching/CoreClass.java @@ -0,0 +1,8 @@ +package com.baeldung.maven_caching; + +public class CoreClass { + + public String method(){ + return "Hello from core module!!"; + } +} diff --git a/docker/docker-caching/multi-module/pom.xml b/docker/docker-caching/multi-module/pom.xml new file mode 100644 index 0000000000..0cc309e2ad --- /dev/null +++ b/docker/docker-caching/multi-module/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + pom + + application + core + + + org.springframework.boot + spring-boot-starter-parent + 2.6.3 + + + com.baeldung + maven-caching + 0.0.1-SNAPSHOT + maven-caching + maven-caching + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + diff --git a/docker/docker-caching/single-module/Dockerfile b/docker/docker-caching/single-module/Dockerfile new file mode 100644 index 0000000000..309945440b --- /dev/null +++ b/docker/docker-caching/single-module/Dockerfile @@ -0,0 +1,15 @@ +FROM maven:alpine as build +ENV HOME=/usr/app +RUN mkdir -p $HOME +WORKDIR $HOME +ADD pom.xml $HOME +RUN mvn verify --fail-never + +ADD . $HOME +RUN mvn package + +FROM openjdk:8-jdk-alpine + +COPY --from=build /usr/app/target/maven-caching-1.0-SNAPSHOT-jar-with-dependencies.jar /app/runner.jar + +ENTRYPOINT java -jar /app/runner.jar \ No newline at end of file diff --git a/docker/docker-caching/single-module/Dockerfile-Buildkit b/docker/docker-caching/single-module/Dockerfile-Buildkit new file mode 100644 index 0000000000..d3d2aad90d --- /dev/null +++ b/docker/docker-caching/single-module/Dockerfile-Buildkit @@ -0,0 +1,13 @@ +FROM maven:alpine as build +ENV HOME=/usr/app +RUN mkdir -p $HOME +WORKDIR $HOME +ADD . $HOME + +RUN --mount=type=cache,target=/root/.m2 mvn -f $HOME/pom.xml clean package + +FROM openjdk:8-jdk-alpine + +COPY --from=build /usr/app/target/maven-caching-1.0-SNAPSHOT-jar-with-dependencies.jar /app/runner.jar + +ENTRYPOINT java -jar /app/runner.jar \ No newline at end of file diff --git a/docker/docker-caching/single-module/pom.xml b/docker/docker-caching/single-module/pom.xml new file mode 100644 index 0000000000..54337436c5 --- /dev/null +++ b/docker/docker-caching/single-module/pom.xml @@ -0,0 +1,53 @@ + + + 4.0.0 + + com.baeldung + single-maven-caching + 1.0-SNAPSHOT + + + 8 + 8 + + + + + com.google.guava + guava + 31.0.1-jre + + + + + + + org.apache.maven.plugins + maven-assembly-plugin + 3.3.0 + + + jar-with-dependencies + + + + true + com.baeldung.maven_caching.MavenCachingMain + + + + + + assemble-all + package + + single + + + + + + + \ No newline at end of file diff --git a/docker/docker-caching/single-module/src/main/java/com/baeldung/maven_caching/MavenCachingMain.java b/docker/docker-caching/single-module/src/main/java/com/baeldung/maven_caching/MavenCachingMain.java new file mode 100644 index 0000000000..81673fd9ac --- /dev/null +++ b/docker/docker-caching/single-module/src/main/java/com/baeldung/maven_caching/MavenCachingMain.java @@ -0,0 +1,11 @@ +package com.baeldung.maven_caching; + +import com.google.common.io.Files; + +public class MavenCachingMain { + + public static void main(String[] args) { + System.out.println("Hello from maven_caching app!!!"); + System.out.println(Files.simplifyPath("/home/app/test")); + } +}