There is a common pattern emerging which is to use Docker to build the software artefact. This approach has become popular since your machine or CI agent is not required to have anything installed but Docker. However, if you have tried this before with Java/Maven, you are most likely to know how slow it can be to see Maven download half of the internet inside the container in every single run. Here I show you how to address that issue with a simple trick.
What we want to do is to copy the pom.xml and the app sources in two separate steps.
The two most important things in this example are running Maven with the pom.xml in isolation and once the sources are added, running Maven again with the –offline option set.
This is only possible thanks to Docker’s built-in cache. Before you ask, yes, if you make any changes to the pom.xml you will invalidate the subsequent cached instructions and you will download half of the internet again. It only works under the assumption that application code is changed more often than the dependency management configuration.
The previous example alone doesn’t provide much context. After all, what do you do with that Dockerfile?
I hope you have heard about Docker multi-stage builds. It has been introduced to encourage the use of docker as a builder engine.
The idea is to show how easy Docker can be used to both build and run your software. Ultimately, entire pipelines could be created with just a Dockerfile and multi-stage builds. That would be a very comfortable position for the developer who would be able to execute all these stages locally exactly the same as the CI agent.
More information at https://github.com/juliaaano/java-starter