Installing Maven automatically to Jenkins when it is running in Docker

Default featured post

Installing Maven automatically to Jenkins when it is running in Docker. In the last article, we discussed how to add credentials to Jenkins when building it from a Docker file. In this article, we discuss Maven automatic installation. As we discuss in the previous article, when building Jenkins Docker container, we can automate our Jenkins configuration in two ways.

The first approach is using Jenkins Configuration as code plugin to do some configuration in a set of YAML file. The second approach is to write Groovy scripts and copying them over to the container while building it. So that Jenkins pick the scripts and executes them automatically.

In the ideal world, we would like to move away from the second approach and only rely on Jenkins Configuration as code plugin to do everything. However, the plugin still lacks in some area and automatic Maven installation is one of them. And to work around that, we have to rely on a Groovy script.

The process is straightforward. We just need to create a Groovy script under our /groovy directory or create one if doesn’t exist. And code the script like this: