5

I have multi module Maven project. Each module has its own Git repo.

According to GitLab documentation each module must have .m2/settings.xml file for CI/CD via .gitlab-ci.yml to work.

I would like to avoid having same .m2/settings.xml file in each module.

Is there a way to achieve that?

Lii
  • 11,553
  • 8
  • 64
  • 88
lapkritinis
  • 2,664
  • 4
  • 19
  • 29

3 Answers3

18

The most convenient solution I figured out for my Maven projects on GitLab is using GitLab Custom environment variables of type File introduced with GitLab 11.11.

If you store the maven settings.xml content into a GitLab environment variable e.g. named MAVEN_SETTINGS_XML and set the type to File, the settings will be available as file named $MAVEN_SETTINGS_XML within each build job having access to this environment.

The maven build can pointed to this settings by either using the the param -s (e.g mvn clean install -s $MAVEN_SETTINGS_XML or by copying the settings to the default location via cp $MAVEN_SETTINGS_XML ~/.m2/settings.xml before executing maven.
I'm doing the latter for now as maven sometimes struggles with the settings path.

By defining this environment variable in a GitLab Group above all maven projects, the same settings will be available to all maven projects as well.

Jens Vagts
  • 595
  • 4
  • 7
  • This works fantastically and the same type of thing works as a regular variable for the MAVEN_OPTS. Thank you. – Randy Aug 12 '22 at 15:21
12

There are several ways for achieving this

  • Hosting the settings.xml on a webserver
    You could simply serve the settings.xml via a web server and wget it during your CI/CD process.

  • Clone the file via GIT
    You can, as you already suggested, clone the file from one of the submodule repositories. There are ways for only retrieving parts of a repository, as discussed in this question

  • Using a custom docker container with the correct Maven configuration You could create your own docker container image to use during your CI/CD process. This image could use the current image as base image (i.e. via Docker's FROM instruction) and add your own custom settings.xml directly to the .m2 directory. Or to any directory you want.
    This would likely be the most elegant solution, however also the one that requires the most setup work. See this resource for how to use docker images from private container registries during GitLab CI/CD.

  • Obviously, you could also just copy the file to each of the repositories
Thomas Kainrad
  • 2,542
  • 21
  • 26
2

You can pass the settings.xml as parameter of the build.

mvn clean install --settings path/to/your/settings.xml
Raphael Alves
  • 169
  • 1
  • 3
  • 1
    I thought about it, but I think each gitlab repo doesnt have access to another repos. But this gives me idea - i can checkout settings.xml from another repo instead of copying it. – lapkritinis Feb 14 '19 at 12:18