Reputation: 2814
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?
Upvotes: 6
Views: 14989
Reputation: 685
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.
Upvotes: 21
Reputation: 2830
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.
Upvotes: 12
Reputation: 189
You can pass the settings.xml as parameter of the build.
mvn clean install --settings path/to/your/settings.xml
Upvotes: 4