There is a tool by Google, called JIB (https://github.com/GoogleContainerTools/jib). JIB builds your Java application, containerizes it to a deployable image and pushes the image to a registry. It is fully configurable over Maven or Gradle.
There is one downside: It is not possible to run docker operations like “RUN”, “USER”, etc. Therefore, you need to setup up a base image and use this in your JIB configuration.
If you want to use JIB with Maven, you must configure the jib-maven-plugin:
Then start the containerization with the command: mvn compile jib:build and you get the Output:
The Maven plugin of JIB will first recompile the classes if needed. In the second build step, it generates the image and pushes it to the specified docker registry. In this example I have pushed the image to my Docker Hub repository: https://hub.docker.com/repository/docker/eberhardmayer/jib-example . Then you can use it from your registry in Kubernetes or other tools. I have set up the whole example at GitHub: https://github.com/eberhardmayer/jib-example
JIB gives you the possibility to create a Docker container via Gradle, Maven or a CLI without having a docker agent on your system. It also integrates well with tools like Maven and makes it easier to manage a CI/CD pipeline with it. Some scenarios will not fit with JIB, like running the key tool of Java on image creation. But it can help to simplify reproducible builds for your Java application.