How to use Gitlab CI to build a Java Maven project?

I have been experimenting with no success whatsoever, I am running a Gitlab hosted on Linux, and trying to get my head around the CI functionality.

According to the Gitlab documentation you only need to create a .gitlab-ci.yml file, the Gitlab implementation of Travis-CI. Now from the looks of it you can accomplish a lot with the .gitlab-ci.yml, but a lot of the documentation is referencing Ruby and other languages. Nothing is said about how to build Java Maven projects.

How can I build a simple application in Java? Can I use the shared runner, or should I be using a specific runner, in that case what or which runner implementation should I choose: ssh, docker, or shell? Then, what should I put in the .gitlab-ci.yml file at least to build the project with Maven?


Solution 1:

Register a Docker runner and use one of the official Maven Docker images, e.g., maven:3-jdk-11 in your .gitlab-ci.yml file:

image: maven:3-jdk-11

build:
  script: "mvn install -B"

Note the -B flag, which is recommended for non-interactive use.

As far as I understand, it does not matter whether the runner is shared or specific.

Solution 2:

I would like to add bit of information here guys. First let's clear out some confusion regarding shared and specific runner.

Shared Runner: As its name sound, shared runners are the build process flow instances which can be used to execute jobs of every single project in your installed gitlab instance having Allowed Shared runners option enabled. To do that of-course you would need administrative rights. As per current gitlab documentation, only use with administrative rights is able to define shared runner.

specific runner This kind of runner executes jobs of only one project.

Also, these are few important points to keep in mind when choosing runner for your projects.

  1. Shared Runners are useful for jobs that have similar requirements, between multiple projects. Rather than having multiple runners idling for many projects, you can have a single or a small number of runners that handle multiple projects. This makes it easier to maintain and update runners for common set of projects.
  2. Specific runners are useful for jobs that have special requirements or for projects with a specific demand. If a job has certain requirements, you can set up the specific runner with this in mind, while not having to do this for all runners. For example, if you want to deploy a certain project, you can setup a specific runner to have the right credentials for this.

Now to select right executor for project, its very important that we have bird view on all the available executors for gitlab runner. Gitlab has made this job easy for us by providing nice documentation over here explaining what are the different options you will get with different executors.

If you would like to know more about runners and different executors, I would suggest you start with this article, Gitlab Runner